iPhone 6和iPhone 5s,4s的字体大小和UIView大小相同

时间:2017-02-14 07:31:27

标签: iphone swift uiview storyboard uifont

iPhone 7和iPhone 6上的文字大小和按钮看起来不错,但在iPhone 5s或iPhone 5上运行时,它们的大小保持不变。

如果我给左边空间约束15点,它在iPhone 7上看起来非常好,但在iPhone 5s上,这给了太多空间。

如何为所有设备系列实现动态视图和FontSize。

提前致谢

3 个答案:

答案 0 :(得分:0)

为此你可以使用AutoLayout。只需将您的视图限制在超级视图的边缘即可。即

yourView -> superView.Leading.Margin
yourView -> superView.Trailing.Margin

使用视图控制器的边距,可以确保它在所有设备上都能正常工作。

对于字体大小 - Apple为您提供了一种名为'prefferedFontStyles'的功能,您可以在几乎所有情况下使用它。因此,如果您希望您的按钮在所有设备上都具有正常的文本大小,只需执行以下操作:

yourLabel.font = UIFont.preferredBody 

UIFont.preferredTitle 

等等。

希望有助于掌握它

答案 1 :(得分:0)

如果说,UIFont.systemFont(ofSize: 16)在4.7" iPhone(屏幕宽度为375pt),但在较小的设备上太大,您可以尝试根据屏幕宽度缩放字体...

let fontSize = UIScreen.main.bounds.width / 375  * 16
let font = UIFont.systemFont(ofSize: fontSize)

对于插图,根据@FlorensvonBuchwaldt,使用自动布局将UILabel约束到其超级视图的边距,然后将layoutMargins设置为缩放插图...

let margin = UIScreen.main.bounds.width / 375  * 15
myView.layoutMargins = UIEdgeInsets(top: margin, left: margin, bottom: margin, right: margin)

答案 2 :(得分:0)

// create global width ratio constant and use it when your need  
let _widthRatio : CGFloat = {
    let ratio = _screenSize.width/375
    return ratio
}()

//use constant 
let font = UIFont.systemFont(ofSize: fontSize * _widthRatio)

// if you set font from storyboard then create subclass of your control 
class YMWidthLabel: UILabel {
    override func awakeFromNib() {
        super.awakeFromNib()
        font = font.withSize(font.pointSize * _widthRatio)
    }
}

// Now bind YMWidthLabel class in storyboard 
相关问题