xcode - 如何在 Xcode Story Board 中添加关于多设备的动态字体大小

标签 xcode swift responsive-design storyboard size

我在 storyBoard 中添加了 autoLayout (w: Any, h: Any)

但是由于字体大小是固定的,所有设备的字体大小都是一样的 (4、4.7、5.5 英寸)

4 英寸看起来不错。但是在5.5英寸,太小了

我想在任何设备上动态增加和减少 UIlabel 字体大小。

有什么想法吗?

enter image description here

最佳答案

我找到了解决方案

我上了一节课

class UILabelDeviceClass : UILabel {

@IBInspectable var iPhoneFontSize:CGFloat = 0 {
    didSet {
        overrideFontSize(iPhoneFontSize)
    }
}

func overrideFontSize(fontSize:CGFloat){
    let currentFontName = self.font.fontName
    var calculatedFont: UIFont?
    let bounds = UIScreen.mainScreen().bounds
    let height = bounds.size.height
    switch height {
    case 480.0: //Iphone 3,4,SE => 3.5 inch
        calculatedFont = UIFont(name: currentFontName, size: fontSize * 0.7)
        self.font = calculatedFont
        break
    case 568.0: //iphone 5, 5s => 4 inch
        calculatedFont = UIFont(name: currentFontName, size: fontSize * 0.8)
        self.font = calculatedFont
        break
    case 667.0: //iphone 6, 6s => 4.7 inch
        calculatedFont = UIFont(name: currentFontName, size: fontSize * 0.9)
        self.font = calculatedFont
        break
    case 736.0: //iphone 6s+ 6+ => 5.5 inch
        calculatedFont = UIFont(name: currentFontName, size: fontSize)
        self.font = calculatedFont
        break
    default:
        print("not an iPhone")
        break
    }

}

然后,设置类

Img1

Img2

然后,设置值

编码愉快!

关于xcode - 如何在 Xcode Story Board 中添加关于多设备的动态字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38138926/

相关文章:

jquery - 阻止 jQuery Mobile 水平单选按钮断开或环绕

ios - 无法通过 CoreSimulator 启动 IBCocoaTouchImageCatalogTool XCode 9.4 上的问题

objective-c - xcode:如何在xcode中调试?

ios - 禁用特定 UIView iOS 12 的自动旋转

ios - PFQueryTableView 未显示 - 在 SWIFT 中使用 PARSE

swift - Swift Ui 中 ForEach 中的限制元素

ios - 如何使用 Swift 在 IOS 中使用自定义标题操作在 tableview 中显示 Collection View

CSS 媒体查询像素与设置宽度不匹配

ios - 如何通过按钮关闭并重新打开 viewController?

firefox - 如何将高分辨率设备添加到响应式设计模式?