Runtime uses-category to change all fonts throughout the project

Source: Internet
Author: User


On the basis of the maturity of the project, there is a need to introduce a new font in the application and replace the default font for all labels, but at the same time, there is no need to replace the label for some specially-set fonts. At first glance, the problem is really tricky, the project is relatively large, one to set all the use of the label font work is huge, and in many dynamic display interface, may miss some of the label, creating a bug. Second, the label source in the project is not unique, and useful code is created, with Xib and storyboard, which also wastes a lot of effort. In this case, we may have the following two ways of handling it.

1, the use of the framework

Create our own Baselabel class, where the default font settings are made, and do not affect the label that is specially set in the use of the font, this way to meet our needs, but not suitable for our scene, the project is mature, to rebuild a label base class, It's not much less work to get all the uilabel to work than to reset all the label fonts. But it also has the advantage, at least if the next time to change the font, we will not have trouble.

2. Replace Uilabel initialization method with runtime

This is the simplest and easiest way, we can use the runtime mechanism to replace the Uilabel initialization method, where the font of the label is set by default. Because the label can be initialized from three sources in the initWithFrame, init, and nib files, we need to replace the three initialization methods.

First, we create a uilabel category:

#import <UIKit/UIKit.h>

@interface UILabel (Changedefaultfont)

@end


#import "Uilabel+changedefaultfont.h"
#import <objc/runtime.h>
@implementation UILabel (Changedefaultfont)

/**
* Each NSObject subclass will call the following method to replace the Init method here, using our new font
* If the font is specially set in the program, the font will not be affected but do not set the font in the label's Init method
* Replacement of default fonts is supported from both Init and initWithFrame and nib file loading methods
*/
+ (void) load{
Execute this method only once
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Class class = [self class];
When swizzling a class method, use the following:
Class class = Object_getclass ((id) self);
Replace three methods
SEL originalselector = @selector (init);
SEL OriginalSelector2 = @selector (initwithframe:);
SEL OriginalSelector3 = @selector (awakefromnib);
SEL swizzledselector = @selector (yhbaseinit);
SEL SwizzledSelector2 = @selector (yhbaseinitwithframe:);
SEL SwizzledSelector3 = @selector (yhbaseawakefromnib);


Method Originalmethod = Class_getinstancemethod (class, Originalselector);
Method ORIGINALMETHOD2 = Class_getinstancemethod (class, OriginalSelector2);
Method originalMethod3 = Class_getinstancemethod (class, OriginalSelector3);
Method Swizzledmethod = Class_getinstancemethod (class, Swizzledselector);
Method SWIZZLEDMETHOD2 = Class_getinstancemethod (class, SwizzledSelector2);
Method swizzledMethod3 = Class_getinstancemethod (class, SwizzledSelector3);
BOOL Didaddmethod =
Class_addmethod (class,
Originalselector,
Method_getimplementation (Swizzledmethod),
Method_gettypeencoding (Swizzledmethod));
BOOL DIDADDMETHOD2 =
Class_addmethod (class,
OriginalSelector2,
Method_getimplementation (SWIZZLEDMETHOD2),
Method_gettypeencoding (SWIZZLEDMETHOD2));
BOOL didAddMethod3 =
Class_addmethod (class,
OriginalSelector3,
Method_getimplementation (SWIZZLEDMETHOD3),
Method_gettypeencoding (SWIZZLEDMETHOD3));

if (Didaddmethod) {
Class_replacemethod (class,
Swizzledselector,
Method_getimplementation (Originalmethod),
Method_gettypeencoding (Originalmethod));

} else {
Method_exchangeimplementations (Originalmethod, Swizzledmethod);
}
if (DIDADDMETHOD2) {
Class_replacemethod (class,
SwizzledSelector2,
Method_getimplementation (ORIGINALMETHOD2),
Method_gettypeencoding (ORIGINALMETHOD2));
}else {
Method_exchangeimplementations (ORIGINALMETHOD2, SWIZZLEDMETHOD2);
}
if (didAddMethod3) {
Class_replacemethod (class,
SwizzledSelector3,
Method_getimplementation (ORIGINALMETHOD3),
Method_gettypeencoding (ORIGINALMETHOD3));
}else {
Method_exchangeimplementations (originalMethod3, swizzledMethod3);
}
});

}
/**
* Change your font name in these methods
*/
-(Instancetype) Yhbaseinit
{
ID __self = [self yhbaseinit];
Uifont * Font = [Uifont fontwithname:@ "Helvetica-oblique" size:self.font.pointSize];
if (font) {

if (main_screen_height>540)
{
Self.font = [Uifont fontwithname:@ "Helvetica-oblique" size:self.font.pointsize-2];
}
Else
{
Self.font=font;
}

}
return __self;
}

-(Instancetype) Yhbaseinitwithframe: (CGRect) rect{
ID __self = [self yhbaseinitwithframe:rect];
Uifont * Font = [Uifont fontwithname:@ "Helvetica-oblique" size:self.font.pointSize];
if (font) {

if (main_screen_height>540)
{
Self.font = [Uifont fontwithname:@ "Helvetica-oblique" size:self.font.pointsize-2];
}
Else
{
Self.font=font;
}
}
return __self;
}
-(void) yhbaseawakefromnib{
[Self yhbaseawakefromnib];
Uifont * Font = [Uifont fontwithname:@ "Helvetica-oblique" size:self.font.pointSize];
if (font) {

if (main_screen_height>540)
{
Self.font = [Uifont fontwithname:@ "Helvetica-oblique" size:self.font.pointsize-2];
}
Else
{
Self.font=font;
}

}

}


@end


Because of the individual project reason, my project has certain particularity in Iphone6 plus above the size need to compress the font so use this method to detect the length of the screen to determine the device model, so that the size of the font to reduce 2 to achieve the goal, in the actual demand, We can also use to change the different fonts and font sizes given by the product.


This article is from the "zhuoking" blog, make sure to keep this source http://9951038.blog.51cto.com/9941038/1852328

Runtime uses-category to change all fonts throughout the project

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.