iOS uses runtime to modify the default font for Uilabel globally
Source: Internet
Author: User
<span id="Label3"></p><p style="text-align: center"><p style="text-align: center">iOS uses runtime to modify the default font for Uilabel globally</p></p><p><p>first, The requirements of the background introduction</p></p><p><p>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.</p></p><p><p>second, the Treatment method</p></p><p><p>1, the use of the framework</p></p><p><p>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.</p></p><p><p>2. Replace Uilabel initialization method with runtime</p></p><p><p>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.</p></p><p><p>first, we create a Uilabel category:</p></p><p><p>#import <UIKit/UIKit.h><br><br>@interface UILabel (yhbasechangedefaultfont)<br><br>@end</p></p><p><p>Add the following code to It:</p></p><p>#import "uilabel+yhbasechangedefaultfont.h"<br>#import <objc/runtime.h><br>@implementation UILabel (yhbasechangedefaultfont)<br>/**<br>* Each NSObject subclass will call the following method to replace the Init method here, using our new font<br>* 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<br>* Replacement of default fonts is supported from both Init and initWithFrame and nib file loading methods<br>*/<br>+ (void) load{<br>Execute this method only once<br>Static dispatch_once_t oncetoken;<br>Dispatch_once (&oncetoken, ^{<br>Class class = [self class];<br>When swizzling a class method, use the Following:<br>Class class = Object_getclass ((id) self);<br>Replace three methods<br>SEL originalselector = @selector (init);<br>SEL OriginalSelector2 = @selector (initwithframe:);<br>SEL OriginalSelector3 = @selector (awakefromnib);<br>SEL swizzledselector = @selector (yhbaseinit);<br>SEL SwizzledSelector2 = @selector (yhbaseinitwithframe:);<br>SEL SwizzledSelector3 = @selector (yhbaseawakefromnib);<br><br><br>Method Originalmethod = Class_getinstancemethod (class, originalselector);<br>Method originalMethod2 = Class_getinstancemethod (class, originalSelector2);<br>Method originalMethod3 = Class_getinstancemethod (class, originalSelector3);<br>Method Swizzledmethod = Class_getinstancemethod (class, swizzledselector);<br>Method swizzledMethod2 = Class_getinstancemethod (class, swizzledSelector2);<br>Method swizzledMethod3 = Class_getinstancemethod (class, swizzledSelector3);<br>BOOL Didaddmethod =<br>Class_addmethod (class,<br>originalselector,<br>Method_getimplementation (swizzledmethod),<br>Method_gettypeencoding (swizzledmethod));<br>BOOL DIDADDMETHOD2 =<br>Class_addmethod (class,<br>originalSelector2,<br>Method_getimplementation (swizzledMethod2),<br>Method_gettypeencoding (swizzledMethod2));<br>BOOL didAddMethod3 =<br>Class_addmethod (class,<br>originalSelector3,<br>Method_getimplementation (swizzledMethod3),<br>Method_gettypeencoding (swizzledMethod3));<br><br>If (DIDADDMETHOD) {<br>Class_replacemethod (class,<br>swizzledselector,<br>Method_getimplementation (originalmethod),<br>Method_gettypeencoding (originalmethod));<br><br>} else {<br>Method_exchangeimplementations (originalmethod, swizzledmethod);<br>}<br>If (didAddMethod2) {<br>Class_replacemethod (class,<br>swizzledSelector2,<br>Method_getimplementation (originalMethod2),<br>Method_gettypeencoding (originalMethod2));<br>}else {<br>Method_exchangeimplementations (originalMethod2, swizzledMethod2);<br>}<br>If (didAddMethod3) {<br>Class_replacemethod (class,<br>swizzledSelector3,<br>Method_getimplementation (originalMethod3),<br>Method_gettypeencoding (originalMethod3));<br>}else {<br>Method_exchangeimplementations (originalMethod3, swizzledMethod3);<br>}<br>});<br><br>}<br>/**<br>* Change your font name in these methods<br>*/<br>-(instancetype) Yhbaseinit<br>{<br>ID __self = [self yhbaseinit];<br>Uifont * Font = [uifont fontwithname:@ "enter Your font name here" size:self.font.pointSize];<br>If (font) {<br>self.font=font;<br>}<br>Return __self;<br>}<br><br>-(instancetype) yhbaseinitwithframe: (cgrect) rect{<br>ID __self = [self yhbaseinitwithframe:rect];<br>Uifont * Font = [uifont fontwithname:@ "enter Your font name here" size:self.font.pointSize];<br>If (font) {<br>self.font=font;<br>}<br>Return __self;<br>}<br>-(void) yhbaseawakefromnib{<br>[self yhbaseawakefromnib];<br>Uifont * Font = [uifont fontwithname:@ "enter Your font name here" size:self.font.pointSize];<br>If (font) {<br>self.font=font;<br>}<br><br>}<br><br>@end</p><p><p>In the method above, we want to uilabel the default display of fonts, we create a uilabel from the Init,initwithframe and nib files to add to the view, do nothing else:</p></p><p><p>UILabel * label = [[UILabel alloc]initwithframe:cgrectmake (20, 100, 280, 30)];<br>Label.text = @ "you are the label from initwithframe";<br>UILabel * Label2 = [[UILabel alloc]init];<br>Label2.frame= cgrectmake (20, 200, 280, 30);<br>Label2.Text = @ "you are the label from init";<br>[self.view addsubview:label];<br>[self.view addsubview:label2];</p></p><p><p><br></p></p><p><p>Run the effect as follows, you can see, the font has been replaced:</p></p><p><p></p></p><p><p>iOS uses runtime to modify the default font for Uilabel globally</p></p></span>
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