加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 17:14:47 所属栏目:百科 来源:网络整理
导读:我在storyBoard中添加了autoLayout(w:Any,h:Any) 但由于字体大小固定,所有设备的字体大小都相同 (4,4.7,5.5英寸) 4英寸看起来不错.但在5.5英寸,这太小了 我想在任何设备中动态地发布和减少UIlabel字体大小. 有任何想法吗? 解决方法 我找到了解决方案 我做
我在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

然后,设定价值

快乐的编码!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读