简体   繁体   English

为不同的设备将不同的字体大小设置为UIButton

[英]Set different font size to UIButton for different devices

I have button called login. 我有一个名为登录的按钮。 when i see the button font size it looks same in all the devices, though the button width and height vary. 当我看到按钮字体大小时,尽管按钮的宽度和高度各不相同,但在所有设备中看起来都一样。 How to define different font size for different devices?. 如何为不同的设备定义不同的字体大小? I am talking about only for iPhone portrait. 我说的只是iPhone肖像。 So don't give solution as size class. 因此,请勿将解决方案作为尺寸等级。

You can use font size variation - define size to font in Storyboard, look in below image it shows how to define size to font. 您可以使用字体大小变化-在Storyboard中定义字体大小,在下图中查看它显示了如何定义字体大小。

在此处输入图片说明

Click on small + button besides Font property, a pop up will appear. 单击字体属性旁边的小+按钮,将出现一个弹出窗口。

As shown in above image you can define size for Width and Height for different variation. 如上图所示,您可以为“ Width和“ Height定义大小以用于不同的变化形式。

Try this 尝试这个

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)
}

You can check the iPhone device size and then in if-else loop apply your button font size logic. 您可以检查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]; 
}

and same goes for all devices. 所有设备也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM