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

问题描述 投票:0回答:3

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

如果我给左侧空间限制为 15 点,它在 iPhone 7 上看起来确实不错,但在 iPhone 5s 上则给出了太多空间。

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

提前致谢

iphone swift uiview storyboard uifont
3个回答
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)

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 

0
投票

为此,您可以使用自动布局。只需将您的视图限制在超级视图的边缘即可。即

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

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

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

yourLabel.font = UIFont.preferredBody 

UIFont.preferredTitle 

等等等等

希望能帮助你掌握窍门

© www.soinside.com 2019 - 2024. All rights reserved.