您好,登錄后才能下訂單哦!
假設我們有這樣一個需求,iPhone 6(屏幕寬度為375pt)上的設計圖上的字號為17pt,iPhone 6 Plus上的字號根據屏幕寬度縮放,即字號為(17pt x 414pt / 375pt)= 18.768pt
如果一個一個設置太麻煩,容易遺漏,這時候我們采用 runtime 的替換方法來實現,如果嫌替換方法太麻煩,我們可以用第三方庫 Aspects 來輔助我們解決。
步驟:
添加pod
pod 'Aspects', '~> 1.4.1'
#import <UIKit/UIKit.h>
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<AspectInfo> info, NSCoder * coder) {
[info.instance scaleFont];
} error:&error];
[self aspect_hookSelector:@selector(initWithFrame:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> info, CGRect frame) {
[info.instance scaleFont];
} error:&error];
//以下是log方法,可以不要
#if DEBUG
[self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> 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<AspectInfo> 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<AspectInfo> info, NSCoder * coder)...
[self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> info) ...
[self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> 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<AspectToken>)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<AspectToken>)aspect_hookSelector:(SEL)selector
withOptions:(AspectOptions)options
usingBlock:(id)block
error:(NSError **)error;
...
Aspects 是 iOS Aspect-oriented programming (AOP) 的一種實現,
滿足以下幾點就可以使用(但不是必須滿足才能使用)
[UIViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo, BOOL animated) {
NSLog(@"View Controller %@ will appear animated: %tu", aspectInfo.instance, animated);
} error:NULL];
Aspects 不是萬能的,GitHub項目主頁有Compatibility and Limitations ,一種常見的問題是當攔截一個方法的時候,它會把相關類當作已攔截,就會報錯(A method can only be hooked once per class hierarchy ),所以當方法名相同時要考慮其他方法,這個 Aspects 庫無法滿足需求
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。