The implementation of the font size adaptation for iOS development

Source: Internet
Author: User
Tags uikit

An iOS development project is nothing more than a pure code layout, xib, or SB layout. So how to achieve the two ways of matching the size of the font. Font Size fit------Pure Code definition a macro is defined as follows:

#define Sizescale (screen_width!= 414? 1:1.2)

#define KFONT (value) [Uifont systemfontofsize:value * Sizescale]

The 1.2 in the macro is the magnification of the size under plus.

Set the font size in pure code by using this macro to fit the whole

Font Size fit------xib or SB font size fit no outside is to set the font size of UIButton, Uilabel, Uitextview, Uitextfield By creating these several classes to achieve (runtime way of black Magic method swizzling) nonsense not to say, directly on the code:. h

#import <UIKit/UIKit.h>

#import <objc/runtime.h>



/**

* button

*/

@interface UIButton (MyFont)


@end



/**

* Label

*/

@interface Uilabel (MyFont)


@end


/**

* TextField

*/


@interface Uitextfield (MyFont)


@end


/**

* TextView

*/

@interface Uitextview (MyFont)


@end


. m

#import "Uibutton+myfont.h"


screen proportions of different devices (of course multiples can be controlled by themselves)


@implementation UIButton (MyFont)


+ (void) load{

Method imp = Class_getinstancemethod ([Selfclass], @selector (initwithcoder:));

Method myimp = Class_getinstancemethod ([Selfclass], @selector (myinitwithcoder:));

Method_exchangeimplementations (Imp, myimp);

}


-(ID) Myinitwithcoder: (nscoder*) adecode{

[Self myinitwithcoder:adecode];

if (self) {

Part is not like changing the font to set the tag value to 333 skip

if (Self.titleLabel.tag!=333) {

CGFloat fontsize =self.titlelabel.font.pointsize;

Self.titleLabel.font = [uifontsystemfontofsize:fontsize * Sizescale];

}

}

return self;

}


@end



@implementation Uilabel (MyFont)


+ (void) load{

Method imp = Class_getinstancemethod ([Selfclass], @selector (initwithcoder:));

Method myimp = Class_getinstancemethod ([Selfclass], @selector (myinitwithcoder:));

Method_exchangeimplementations (Imp, myimp);

}


-(ID) Myinitwithcoder: (nscoder*) adecode{

[Self myinitwithcoder:adecode];

if (self) {

Part is not like changing the font to set the tag value to 333 skip

if (Self.tag!=333) {

CGFloat fontsize =self.font.pointsize;

Self.font = [uifontsystemfontofsize:fontsize * Sizescale];

}

}

return self;

}


@end


@implementation Uitextfield (MyFont)


+ (void) load{

Method imp = Class_getinstancemethod ([Selfclass], @selector (initwithcoder:));

Method myimp = Class_getinstancemethod ([Selfclass], @selector (myinitwithcoder:));

Method_exchangeimplementations (Imp, myimp);

}


-(ID) Myinitwithcoder: (nscoder*) adecode{

[Self myinitwithcoder:adecode];

if (self) {

Part is not like changing the font to set the tag value to 333 skip

if (Self.tag!=333) {

CGFloat fontsize =self.font.pointsize;

Self.font = [uifontsystemfontofsize:fontsize * Sizescale];

}

}

return self;

}


@end


@implementation Uitextview (MyFont)


+ (void) load{

Method imp = Class_getinstancemethod ([Selfclass], @selector (initwithcoder:));

Method myimp = Class_getinstancemethod ([Selfclass], @selector (myinitwithcoder:));

Method_exchangeimplementations (Imp, myimp);

}


-(ID) Myinitwithcoder: (nscoder*) adecode{

[Self myinitwithcoder:adecode];

if (self) {

Part is not like changing the font to set the tag value to 333 skip

if (Self.tag!=333) {

CGFloat fontsize =self.font.pointsize;

Self.font = [uifontsystemfontofsize:fontsize * Sizescale];

}

}

return self;

}


@end

Related Article

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.