Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use of RunTime-Category changes all fonts for the entire project

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

On the basis of the relatively mature project, there is such a demand that new fonts need to be introduced in the application, and all Label default fonts need to be replaced, but at the same time, some special label fonts do not need to be replaced. At first glance, this problem is really thorny. First of all, the project is relatively large, the font workload of setting up all the label used is huge, and in many dynamic display interfaces, some label may be left out, resulting in bug. Second, the source of label in the project is not unique, useful code creation, there are xib and storyBoard, which will also waste a lot of energy. In this case, we may have the following two ways to deal with it.

1. Use the framework

Create our own BaseLabel class, in which the default font settings, and does not affect the use of special font settings in the process of label, this way can meet our needs, but not suitable for our scene, the project is mature, rebuild a label base class, so that all UILabel will not be replaced by it's workload is not much less than the workload of resetting all label fonts. But it also has an advantage, at least if we change the font next time, we won't have to bother.

2. Replace the UILabel initialization method with runtime

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

First, we create a category of UILabel:

# import

@ interface UILabel (ChangeDefaultFont)

@ end

# import "UILabel+ChangeDefaultFont.h"

# import

@ implementation UILabel (ChangeDefaultFont)

/ * *

* each subclass of NSObject calls the following method to replace the init method here with our new font

* if fonts are specially set in the program, fonts with special settings will not be affected, but do not set fonts in the init method of Label

* loading methods from init and initWithFrame and nib files both support changing default fonts

, /

+ (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

Due to personal project reasons, my project has a certain particularity that the size above Iphone6 plus needs to compress fonts, so I use this method to detect the length of the screen to determine the device model, so that the font size can be reduced by 2 font sizes to achieve the goal. In the actual demand, we can also be used to replace different fonts and font sizes given by the product.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report