IOS text size adaptive Library (MBFontAdapter) Birth note, iosmbfontadapter

Source: Internet
Author: User

IOS text size adaptive Library (MBFontAdapter) Birth note, iosmbfontadapter
Background

What is iOS text size adaptive? It is too difficult to answer this question... Therefore, it is easy to understand the following story:

One day, the girl who made the UI design asked me weakly: "Can the text font size of our app be higher than that of iPhone 4 s and iPhone 5 on iPhone 6 and iPhone 6 plus? Is it difficult to implement it? Will the workload increase a lot ?" In fact, when I heard this problem, I am not good at all, because the current mainstream apps do not seem to be doing this. However, can I answer this question? Is it difficult to answer? How much will the workload increase? Can you tell mainstream UI apps not to do this? Obviously, no !!! So we have MBFontAdapter.

Prerequisites

The adaptive font size is only designed to meet the needs of uidesign. In simple terms, the interface is beautiful, so there are several prerequisites:

Thought Analysis built-in storyboard Method

Storyboard supports setting different font sizes in different la S (Compact, Regular, and Any), such:

Click the plus sign in the red box to add the font size for different la S.

Note: although this method can separately customize the font size of each control in different la s, it is too troublesome. Because our UI needs to be set in this way for every control with text... Can you imagine the pain of setting one by one?

My methods

There are several controls with text: UILabel, UITextField, UIButton, and UITextView. So I can inherit the classes of these controls and then reload some of their implementation methods. You only need to set the class as an inherited class when using it. The specific implementation steps are as follows:

Step 1

Of course, it is necessary to know whether the current device is iPhone6 or iPhone6Plus (because according to the UI requirements, the font size is determined by the screen size of the iPhone 5 during design, so the font size on the iPhone 6 should be slightly larger, the font size of the iPhone 6 plus is a little larger ). The code is simple.

#define IS_IPHONE_6 ([[UIScreen mainScreen] bounds].size.height == 667.0f)#define IS_IPHONE_6_PLUS ([[UIScreen mainScreen] bounds].size.height == 736.0f)
Step 2

Then we need to determine how much the font size should be larger (according to the subsequent communication with the UI, we have increased the font size on iPhone 6 to 2 and the font size on iPhone 6 plus to 3 ).

// Set the font size of the iPhone 6 to be enlarged here (if the font size of iPhone 6 is enlarged to 2, that is, when the font size of iPhone 4S and iPhone 5 is 15, the font Number of iPhone 6 is 17) # define IPHONE6_INCREMENT 2 // set the font size of the iPhone6Plus to be enlarged here (now it is to enlarge the number 3, that is, when the font of iPhone 4S and iPhone 5 is 15, the font Number of iPhone 6 is 18) # define iphone6plus_in
Step 3

Then there is the core logic. I encapsulate a method. The function of this method is to pass in a UIFont object, and then adjust the UIFont font size based on the current device, then a new UIFont object is returned. The Code is as follows:

+(UIFont *)adjustFont:(UIFont *)font{    UIFont *newFont=nil;    if (IS_IPHONE_6){        newFont = [UIFont fontWithName:font.fontName size:font.pointSize+IPHONE6_INCREMENT];    }else if (IS_IPHONE_6_PLUS){        newFont = [UIFont fontWithName:font.fontName size:font.pointSize+IPHONE6PLUS_INCREMENT];    }else{        newFont = font;    }    return newFont;}
Step 4

Then there is the analysis of the control layer.

First, the control can be generated in two ways: Code Generation and storyboard and xib.

Then there is the problem of setting the initial font size. According to the UI design, the font size in different places must be different, so you must manually set it once, the font of our app is completely set according to the size specified by the UI (only the corresponding font size will be enlarged on iPhone 6 and iPhone 6 plus). If the code is generated, call the setFont method of the control, for storyboard and xib, set the font size at the position marked in the red box:

Then there is the overload method. The first is the setFont method. The Code is as follows:

-(void)setFont:(UIFont *)font{    [super setFont:[MBFontAdapter adjustFont:font]];}

In this way, the user will automatically process the font size when calling. font or setFont.

Then awakeFromNib. This method is called when the control is generated by xib and storyboard, the idea is to get the current font of the control (that is, the font size set in the place where the font is set) and then process it. The Code is as follows:

-(void)awakeFromNib{    [super awakeFromNib];    [super setFont:[MBFontAdapter adjustFont:self.font]];}

You can download the demo for details:
Https://github.com/mmoaay/MBFontAdapter

Note: currently, storyboard and xib are perfectly supported.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.