假設(shè)我們有這樣一個(gè)需求,iPhone 6(屏幕寬度為375pt)上的設(shè)計(jì)圖上的字號(hào)為17pt,iPhone 6 Plus上的字號(hào)根據(jù)屏幕寬度縮放,即字號(hào)為(17pt x 414pt / 375pt)= 18.768pt
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比大安網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式大安網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋大安地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。
如果一個(gè)一個(gè)設(shè)置太麻煩,容易遺漏,這時(shí)候我們采用 runtime 的替換方法來實(shí)現(xiàn),如果嫌替換方法太麻煩,我們可以用第三方庫 Aspects 來輔助我們解決。
步驟:
添加pod
pod 'Aspects', '~> 1.4.1'
#import
NS_ASSUME_NONNULL_BEGIN
@interface UILabel (AspectsScaling)
@end
NS_ASSUME_NONNULL_END
UILabel+AspectsScaling.m 文件
#import "UILabel+AspectsScaling.h"
#import "Aspects.h"
@implementation UILabel (AspectsScaling)
+ (void)load {
NSError * error = nil;
[self aspect_hookSelector:@selector(initWithCoder:) withOptions:AspectPositionAfter usingBlock:^(id info, NSCoder * coder) {
[info.instance scaleFont];
} error:&error];
[self aspect_hookSelector:@selector(initWithFrame:) withOptions:AspectPositionAfter usingBlock:^(id info, CGRect frame) {
[info.instance scaleFont];
} error:&error];
//以下是log方法,可以不要
#if DEBUG
[self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionBefore usingBlock:^(id info) {
UILabel * label = info.instance;
NSLog(@"UILabel: Before Scaling font size: %f", label.font.pointSize);
} error:&error];
[self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionAfter usingBlock:^(id info) {
UILabel * label = info.instance;
NSLog(@"UILabel: After Scaling font size: %f", label.font.pointSize);
} error:&error];
#endif
}
- (void)scaleFont {
CGFloat ratio = CGRectGetWidth(UIScreen.mainScreen.bounds) / (CGFloat)375;
self.font = [UIFont fontWithDescriptor:self.font.fontDescriptor size:self.font.pointSize * ratio];
}
@end
- (void)scaleFont;
[self aspect_hookSelector:@selector(initWithCoder:) withOptions:AspectPositionAfter usingBlock:^(id info, NSCoder * coder)...
[self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionBefore usingBlock:^(id info) ...
[self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionAfter usingBlock:^(id info) ...
...
typedef NS_OPTIONS(NSUInteger, AspectOptions) {
AspectPositionAfter = 0, /// Called after the original implementation (default)
AspectPositionInstead = 1, /// Will replace the original implementation.
AspectPositionBefore = 2, /// Called before the original implementation.
AspectOptionAutomaticRemoval = 1 << 3 /// Will remove the hook after the first execution.
};
...
+ (id)aspect_hookSelector:(SEL)selector
withOptions:(AspectOptions)options
usingBlock:(id)block
error:(NSError **)error;
/// Adds a block of code before/instead/after the current `selector` for a specific instance.
- (id)aspect_hookSelector:(SEL)selector
withOptions:(AspectOptions)options
usingBlock:(id)block
error:(NSError **)error;
...
Aspects 是 iOS Aspect-oriented programming (AOP) 的一種實(shí)現(xiàn),
滿足以下幾點(diǎn)就可以使用(但不是必須滿足才能使用)
[UIViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionAfter usingBlock:^(id aspectInfo, BOOL animated) {
NSLog(@"View Controller %@ will appear animated: %tu", aspectInfo.instance, animated);
} error:NULL];
Aspects 不是萬能的,GitHub項(xiàng)目主頁有Compatibility and Limitations ,一種常見的問題是當(dāng)攔截一個(gè)方法的時(shí)候,它會(huì)把相關(guān)類當(dāng)作已攔截,就會(huì)報(bào)錯(cuò)(A method can only be hooked once per class hierarchy ),所以當(dāng)方法名相同時(shí)要考慮其他方法,這個(gè) Aspects 庫無法滿足需求