假設(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)站重做改版、海拉爾網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為海拉爾等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。如果一個(gè)一個(gè)設(shè)置太麻煩,容易遺漏,這時(shí)候我們采用 runtime 的替換方法來實(shí)現(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) 的一種實(shí)現(xiàn),
滿足以下幾點(diǎ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項(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 庫無法滿足需求
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前名稱:iOS開發(fā):UILabel字號(hào)根據(jù)屏幕縮放-創(chuàng)新互聯(lián)
瀏覽路徑:http://jinyejixie.com/article12/ccsedc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、標(biāo)簽優(yōu)化、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站排名、搜索引擎優(yōu)化、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)