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

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

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

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

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

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

有什么想法吗?

xcode swift responsive-design storyboard size
5个回答
11
投票

我找到了解决方案

我上了一节课

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
    }

}

}

然后,设置班级

然后,设置值

快乐编码!


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)

0
投票

不可以,您无法仅为 iPhone 5.5 配置尺寸类别。尺寸等级无法区分 iPhone 5、6 和 6Plus。

仅 1 个边缘保护壳仅支持 iPhone 5.5 横向:

regular width, compact height: 5.5" iPhone in Landscape

唯一的方法是检查代码中的设备大小!


0
投票

动态标签文本大小和动态按钮文本大小:

这些标签和按钮的扩展

我修改了 Yoohogyun 答案以适合所有 iPhone 和 iPad,并且还预期新的屏幕尺寸:


@IBDesignable
class UILabelDeviceClass : UILabel {

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

func overrideFontSize(fontSize:CGFloat){
    let currentFontName = self.font.fontName
    var calculatedFont: UIFont?
    let bounds = UIScreen.main.bounds
    let height = bounds.size.height
    switch height {
    case 480.0: //Iphone 3,4,SE => 3.5 inch
        calculatedFont = UIFont(name: currentFontName, size: fontSize      )
        self.font = calculatedFont
        break
    case 568.0: //iphone 5, 5s => 4 inch
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.99)
        self.font = calculatedFont
        break
    case 667.0: //iphone 6,7,8,(se 2'nd,third) 6s => 4.7 inch
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.95)
        self.font = calculatedFont
        break
    case 736.0: //iphone 6s+ 6+ ,7+ & 8+=> 5.5 inch
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.9)
        self.font = calculatedFont
        break
        
    case 812.0: //  iPhone x,xs,11pro,12mini & 13mini
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.89)
        self.font = calculatedFont
        break
        
    case 844.0: //  iPhone 12 pro,12,13pro,13 & 14
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.85)
        self.font = calculatedFont
        break
        
    case 852:   //  iPhone 14Pro,15 & 15Pro
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.8)
        self.font = calculatedFont
        break
        
    case 896:   //  Iphone XS Max,XR,11 & 11 Pro Max
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.79)
        self.font = calculatedFont
        break
        
    case 926:   //  Iphone 12 Pro Max,iPhone 13 Pro Max,iPhone 14 Plus
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.75)
        self.font = calculatedFont
        break
        
    case 932:   //  iPhone 14 Pro Max,iPhone 15 Plus & iPhone 15 Pro Max
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.7)
        self.font = calculatedFont
        break
        
    case 933...1000:    //  may be new iPhones
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.69)
        self.font = calculatedFont
        break
        
    case 1024:  //  most of IPads
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.65)
        self.font = calculatedFont
        break
        
    case 1080:  //  another Ipads
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.6)
        self.font = calculatedFont
        break
        
    //  latest iPads
    case 1112,1113:
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.59)
        self.font = calculatedFont
        break
        
    case 1180:
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.55)
        self.font = calculatedFont
        break
        
    case 1194:
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.5)
        self.font = calculatedFont
        break
        
    case 1366:
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.49)
        self.font = calculatedFont
        break
        
    case 1367...1400:   //  may be new iPads
        calculatedFont = UIFont(name: currentFontName, size: fontSize / 0.45    )
        self.font = calculatedFont
        break
        
    default:
        print("not an iPhone")
        break
    }
    
   }
 }

//  for dynamic text size for all screens until IPad
@IBDesignable
class UIButtonDeviceClass : UIButton {

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

func overrideFontSize(fontSize:CGFloat){
    let currentFontName = self.titleLabel?.font.fontName//  UIFont(name: "Nexa-Black", size: UIFont.labelFontSize)
    var calculatedFont: UIFont?
    let bounds = UIScreen.main.bounds
    let height = bounds.size.height
    switch height {
    case 480.0: //Iphone 3,4,SE => 3.5 inch
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size: fontSize)
        self.titleLabel?.font = calculatedFont ?? UIFont.systemFont(ofSize: fontSize)
        break
    case 568.0: //iphone 5, 5s => 4 inch
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.99)
        self.titleLabel?.font = calculatedFont
        break
    case 667.0: //iphone 6,7,8,(se 2'nd,third) 6s => 4.7 inch
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.95)
        self.titleLabel?.font = calculatedFont
        break
    case 736.0: //iphone 6s+ 6+ ,7+ & 8+=> 5.5 inch
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.9)
        self.titleLabel?.font = calculatedFont
        break
        
    case 812.0: //  iPhone x,xs,11pro,12mini & 13mini
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.89)
        self.titleLabel?.font = calculatedFont
        break
        
    case 844.0: //  iPhone 12 pro,12,13pro,13 & 14
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.85)
        self.titleLabel?.font = calculatedFont
        break
        
    case 852:   //  iPhone 14Pro,15 & 15Pro
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.8)
        self.titleLabel?.font = calculatedFont
        break
        
    case 896:   //  Iphone XS Max,XR,11 & 11 Pro Max
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.79)
        self.titleLabel?.font = calculatedFont
        break
        
    case 926:   //  Iphone 12 Pro Max,iPhone 13 Pro Max,iPhone 14 Plus
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.75)
        self.titleLabel?.font = calculatedFont
        break
        
    case 932:   //  iPhone 14 Pro Max,iPhone 15 Plus & iPhone 15 Pro Max
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.7)
        self.titleLabel?.font = calculatedFont
        break
        
    case 933...1000:    //  may be new iPhones
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.69)
        self.titleLabel?.font = calculatedFont
        break
        
    case 1024:  //  most of IPads
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.65)
        self.titleLabel?.font = calculatedFont
        break
        
    case 1080:  //  another Ipads
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.6)
        self.titleLabel?.font = calculatedFont
        break
        
    //  latest iPads
    case 1112,1113:
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.59)
        self.titleLabel?.font = calculatedFont
        break
        
    case 1180:
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.55)
        self.titleLabel?.font = calculatedFont
        break
        
    case 1194:
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.5)
        self.titleLabel?.font = calculatedFont
        break
        
    case 1366:
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.49)
        self.titleLabel?.font = calculatedFont
        break
        
    case 1367...1400:   //  may be new iPads
        calculatedFont = UIFont(name: currentFontName ?? "Nexa-Black", size:fontSize / 0.45)
        self.titleLabel?.font = calculatedFont
        break
        
    default:
        print("not an iPhone")
        break
    }
    
   }
}

顺便说一句,它将被加载到故事板中快乐编码💻👏🏻


-1
投票

使用 xcode 7.3,您可以在属性检查器中单击添加“+”来选择每个设备的字体大小

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