假設我們有這樣一個需求,iPhone 6(屏幕寬度為375pt)上的設計圖上的字號為17pt,iPhone 6 Plus上的字號根據(jù)屏幕寬度縮放,即字號為(17pt x 414pt / 375pt)= 18.768pt
如果一個一個設置太麻煩,容易遺漏,這時候我們采用 runtime 的替換方法來實現(xiàn),如果嫌替換方法太麻煩,我們可以用第三方庫 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) 的一種實現(xiàn),
滿足以下幾點就可以使用(但不是必須滿足才能使用)
[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 庫無法滿足需求
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
當前名稱:iOS開發(fā):UILabel字號根據(jù)屏幕縮放-創(chuàng)新互聯(lián)
瀏覽路徑:http://www.rwnh.cn/article12/ccsedc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供Google、標簽優(yōu)化、手機網(wǎng)站建設、網(wǎng)站排名、搜索引擎優(yōu)化、網(wǎng)站導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)