how to change uilabel text size in different iPhones

自闭症网瘾萝莉.ら 提交于 2020-01-06 03:09:06

问题


Is there a way to change UILabel text size for different screen sizes?

I need the text to be bigger in I-phone 6 and 6+.

Can i do this?

Would the correct way to do this will be to check screen height and then changing the text size?

Thanks


回答1:


    if (self.view.frame.size.width == 320){
        //iPhone 2G, 3G, 3GS, 4, 4s, 5, 5s, 5c
        label.font = UIFont(name: "....", size: 20)
    }
    else if (self.view.frame.size.width == 375){
        //iPhone 6
        label.font = UIFont(name: "....", size: 25)
    }
    else if (self.view.frame.size.width == 414){
        //iPhone 6 Plus
        label.font = UIFont(name: "....", size: 30)
    }



回答2:


In the .pch file declare the following

#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)

Use the following where you need to implement

if (IS_IPHONE_6){
        [myLabel setFont:[UIFont fontWithName:@"FontName" size:18]];
    }
    else if (IS_IPHONE_6P){
        [myLabel setFont:[UIFont fontWithName:@"FontName" size:20]];
    }



回答3:


There are convenience methods to determine the height of the current device from a library called Resplendent Utilities. You will first have to install the cocoa pod for this library here http://cocoapods.org/pods/ResplendentUtilities. If you can't install cocoa pods, you can simply get the needed code from the github link to this library https://github.com/Resplendent/ResplendentUtilities.

Once you have this library in your project, import this file https://github.com/Resplendent/ResplendentUtilities/blob/master/Pod/Classes/ResplendentUtilities/Code/ResplendentUtilities/Misc/Compatability/RUScreenSizeToFloatConverter.m

Once you have this file imported, init the class object of this file (RUScreenSizeToFloatConverter). You can then store the appropriateHeightForCurrentScreenHeight property of this class in a CGFloat. This property gives you the height of your current device. You can then use some conditional statements (if (appropriateHeightForCurrentScreenHeight == 736.0f) {}) to determine the size of your label text depending on the height of your device.




回答4:


You can use size classes to configure different styles and layouts depending on the screen sizes. At the bottom of Interface Builder in your storyboard, you'll see wAny hAny. Click that and you can select the size of the device you want to configure your styles to.



来源:https://stackoverflow.com/questions/29236293/how-to-change-uilabel-text-size-in-different-iphones

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!