为不同的设备设置不同的字体大小到UIButton
问题描述:
我有一个名为login的按钮。当我看到按钮的字体大小在所有设备中看起来都一样,虽然按钮的宽度和高度有所不同。如何为不同的设备定义不同的字体大小?我只是谈论iPhone的肖像。所以不要给解决方案作为尺寸等级。为不同的设备设置不同的字体大小到UIButton
答
您可以检查iPhone设备的大小,然后在if-else循环中应用您的按钮字体大小逻辑。
#define iPhoneVersion ([[UIScreen mainScreen] bounds].size.height == 568 ? 5 : ([[UIScreen mainScreen] bounds].size.height == 480 ? 4 : ([[UIScreen mainScreen] bounds].size.height == 667 ? 6 : ([[UIScreen mainScreen] bounds].size.height == 736 ? 7 : ([[UIScreen mainScreen] bounds].size.height == 736 ? 61 : ([[UIScreen mainScreen] bounds].size.height == 736 ? 61 : (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 10 :61)))))))
if (iPhoneVersion == 4)
{
mybutton.titleLabel.font = [UIFont systemFontOfSize:12];
}
else if (iPhoneVersion == 5)
{
mybutton.titleLabel.font = [UIFont systemFontOfSize:14];
}
else if (iPhoneVersion == 6)
{
mybutton.titleLabel.font = [UIFont systemFontOfSize:16];
}
else if (iPhoneVersion == 7)
{
mybutton.titleLabel.font = [UIFont systemFontOfSize:17];
}
对于所有设备也是如此。
答
试试这个
if UIScreen.mainScreen().bounds.size.height == 480 {
// iPhone 4
mybutton.titleLabel.font = mybutton.titleLabel.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.height == 568 {
// IPhone 5
mybutton.titleLabel.fontt = mybutton.titleLabel.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 375 {
// iPhone 6
mybutton.titleLabel.font = mybutton.titleLabel.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 414 {
// iPhone 6+
mybutton.titleLabel.font = mybutton.titleLabel.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 768 {
// iPad
mybutton.titleLabel.font = mybutton.titleLabel.font.fontWithSize(20)
}
您可以使用大小类此。 – KKRocks
[iOS不同设备的单个大小类中的iOS不同字体大小的可能重复](http://stackoverflow.com/questions/28076020/ios-different-font-sizes-within-single-size-class-for-different-设备) – KKRocks