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

时间:2016-07-01 06:56:16

标签: 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

4 个答案:

答案 0 :(得分:10)

我找到了解决方案

我制作了一个班级

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

然后,设置值

快乐的编码!

答案 1 :(得分:2)

检查屏幕尺寸并根据它操作字体大小:

let screenSize = UIScreen.mainScreen().bounds.size

if screenSize.height < 568 {
    //Set font size for 4
} else if screenSize.height < 568 {
    //Set font size for 5
} else if screenSize.height < 568 {
    //Set font size for 6
} else {
    //Set font size for 6+
}

更新:

扩展UILabel类以设置标签:

extension UILabel { 
    func setupLabelDynamicSize(size size:CGFloat) {
        let screenSize = UIScreen.mainScreen().bounds.size

        if screenSize.height < 568 {
            //Set self.font equal size
        } else if screenSize.height < 568 {
            //Set self.font equal size + 1
        } else if screenSize.height < 568 {
            //Set self.font equal size + 2
        } else {
            //Set self.font equal size + 3
        }
    }
}

然后,无论您拥有动态尺寸所需的标签,只需致电:

labelObject.setupLabelForDynamicSize(size: 14)

答案 2 :(得分:0)

不,您不能仅为iPhone 5.5配置尺寸等级。尺寸等级无法区分iPhone 5,6与6Plus。

只有1个边缘外壳仅支持iPhone 5.5横向: regular width, compact height: 5.5" iPhone in Landscape

您可以做的唯一方法是检查代码中的设备尺寸!

答案 3 :(得分:-1)

使用xcode 7.3,您可以点击添加&#39; +&#39;在Attributes检查器中选择每个设备的字体大小 enter image description here