我正在开发一个iOS应用程序,我在iPhone 5,6,6中观察到UI所有字体都是不同的设备,但我的要求是iPhone4s的按钮大小和字体大小必须相同,5和iPhone 6和6的不同如何在我的应用程序中实现这一目
我使用的是xcode7.2,swift2.
提前致谢..
我遇到了同样的问题并使用 Swifts Computed Properties解决了它.我创建了一个静态变量fontsize,由于屏幕的大小,它可以使用适当的fontsize进行动态初始化.
import UIKit
class ViewFunctions {
let screenSize = UIScreen.mainScreen().bounds.size
static var fontsize: CGFloat {
get {
if screenSize.height >= 1024 { // iPad Pro
return 16.0
} else if screenSize.height >= 768 { // iPad
return 16.0
} else if screenSize.height >= 414 { // iPhone 6Plus
return 15.0
} else if screenSize.height >= 375 { // iPhone 6/s
return 15.0
} else if screenSize.height >= 320 { // iPhone 5/s
return 14.0
} else if screenSize.height >= 319 { // iPhone 4/s
return 14.0
} else {
return 14.0
}
}
}
}
然后使用它来设置按钮标签的fontsize:
import UIKit
class TestClass {
var testButton: UIButton = UIButton(type: UIButtonType.System) as UIButton!
testButton.titleLabel!.font = UIFont(name: "Helvetica Neue", size: ViewFunctions.fontsize)
testButton.setTitle("Test", forState: .Normal)
// add button to view or sth like that...
}
