IOS--App Global font settings

Source: Internet
Author: User
Tags uikit

method One:

Write a Uilabel (fontextension) extension
Rewrite initWithFrame (handwriting code must go method) and awakefromnib (Xib must walk method)
Of course, UIButton, Uitextview and other controls can be used this way


#import <UIKit/UIKit.h>

@interface UILabel (fontextension)

@end

#import "Uilabel+fontextension.h"

#define KGLOBALFONTFAMILYNAME @ "Snell roundhand"//Global font name

@implementation UILabel (fontextension)

-(Instancetype) initWithFrame: (CGRect) frame
{
if (self = [Super Initwithframe:frame]) {
[Self setglobalfont];
}

return self;
}

-(void) awakefromnib
{
[Super awakefromnib];
[Self setglobalfont];
}

-(void) Setglobalfont
{
[Self Setfont:[uifont fontwithname:kglobalfontfamilyname size:17];
}

@end

Method Two:

Write a base class (such as Fontviewcontroller) to inherit Uiviewcontroller
Rewrite viewwilllayourtsubviews

-(void) viewwilllayoutsubviews
{
[Self setfontfamily:@ "Microsoft Yahei" ForView:self.view Andsubviews:yes];
}

Traverse all Uilabel in Self.view to set its font
-(void) setfontfamily: (nsstring*) fontFamily Forview: (uiview*) View andsubviews: (BOOL) issubviews
{
if ([View Iskindofclass:[uilabel class]])
{
UILabel *LBL = (UILabel *) view;
[LbL setfont:[uifont fontwithname:fontfamily size:[[lbl font] pointsize]-2]];
}

if (issubviews)
{
For (UIView *sview in View.subviews)
{
[Self setfontfamily:fontfamily forview:sview andsubviews:yes];
}
}
}

Method Three:

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 (Yhbasechangedefaultfont)

@end

Add the following code to it:

#import "Uilabel+yhbasechangedefaultfont.h"
#import <objc/runtime.h>
@implementation UILabel (Yhbasechangedefaultfont)
/**
* 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:@ "Enter your font name here" size:self.font.pointSize];
if (font) {
Self.font=font;
}
return __self;
}

-(Instancetype) Yhbaseinitwithframe: (CGRect) rect{
ID __self = [self yhbaseinitwithframe:rect];
Uifont * Font = [Uifont fontwithname:@ "Enter your font name here" size:self.font.pointSize];
if (font) {
Self.font=font;
}
return __self;
}
-(void) yhbaseawakefromnib{
[Self yhbaseawakefromnib];
Uifont * Font = [Uifont fontwithname:@ "Enter your font name here" size:self.font.pointSize];
if (font) {
Self.font=font;
}

}

@end

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:

UILabel * label = [[UILabel alloc]initwithframe:cgrectmake (20, 100, 280, 30)];
Label.text = @ "You are the label from initWithFrame";
UILabel * Label2 = [[UILabel alloc]init];
Label2.frame= CGRectMake (20, 200, 280, 30);
Label2.Text = @ "You are the label from Init";
[Self.view Addsubview:label];
[Self.view Addsubview:label2];


Run the effect as follows, you can see, the font has been replaced:

IOS--App Global font settings

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.