在 iOS 8 中,我们可以为每个尺寸等级设计不同的 UI 布局.我面临的问题是,我设计了紧凑宽度和常规高度的布局(所有 iPhone 纵向的尺寸等级),但我想保持 3.5 和 4 英寸设备(iPhone 4 和 5)的标签字体更小),然后 4.7 英寸 (iPhone 6) 相对更大,5.5 英寸 (iPhone 6 Plus) 设备更大.我已经搜索过,但无法找到为同一大小类中的不同设备设置不同字体大小的解决方案.
In iOS 8, we can design a different UI layout for each size class. The issue I'm facing is, I've designed a layout for Compact Width and Regular Height (size class for all iPhones in portrait) but i want to keep font size of labels smaller for 3.5 and 4 inch devices (iPhone 4 and 5), then relatively bigger for 4.7 inch (iPhone 6) and more bigger for 5.5 inch (iPhone 6 Plus) devices. I've searched but unable to find a solution to set different font size for different devices within same size class.
编辑:我不再推荐这个了.当新设备问世时,这种方法不能很好地扩展.结合使用动态字体大小和特定于大小类别的字体.
Edit: I don't recommend this anymore. This approach doesn't scale well when new devices come out. Use a combination of dynamic font sizes and size classes-specific fonts.
假设一个新的 iPhone 模型问世,如果您使用自动布局和大小类,您不必手动修复所有约束以使您的应用程序与这个新设备兼容.但是,您仍然可以使用以下代码设置 UILabel
的字体大小:
Say a new iPhone model comes out, if you are using Auto Layout and Size Classes you don't have to fix all the constraints manually to make your app compatible with this newer device. However, you can still set the font size of the UILabel
using the following code:
if UIScreen.mainScreen().bounds.size.height == 480 {
// iPhone 4
label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.height == 568 {
// IPhone 5
label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 375 {
// iPhone 6
label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 414 {
// iPhone 6+
label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 768 {
// iPad
label.font = label.font.fontWithSize(20)
}
这篇关于不同设备的单一大小类中的 iOS 不同字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!