溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS開發(fā):UILabel字號根據(jù)屏幕縮放

發(fā)布時間:2020-07-18 02:28:06 來源:網(wǎng)絡(luò) 閱讀:758 作者:PSPDF 欄目:移動開發(fā)

場景:

假設(shè)我們有這樣一個需求,iPhone 6(屏幕寬度為375pt)上的設(shè)計圖上的字號為17pt,iPhone 6 Plus上的字號根據(jù)屏幕寬度縮放,即字號為(17pt x 414pt / 375pt)= 18.768pt

解決方案:

如果一個一個設(shè)置太麻煩,容易遺漏,這時候我們采用 runtime 的替換方法來實現(xiàn),如果嫌替換方法太麻煩,我們可以用第三方庫 Aspects 來輔助我們解決。

步驟:

  1. 添加pod

    pod 'Aspects', '~> 1.4.1'
  2. 新建UILabel Category,命名為UILabel+AspectsScaling
    以下為文件內(nèi)容
    UILabel+AspectsScaling.h 文件
#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

解釋:

  1. 顯然,這是縮放字體的方法
    - (void)scaleFont;
  2. 這個方法是在原來的initWithCoder: 方法后面執(zhí)行一個 block ,這是 Aspects 庫的方法,利用的是 runtime,可以自行了解源碼
    [self aspect_hookSelector:@selector(initWithCoder:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> info, NSCoder * coder)...
  3. 再看 log 方法,這個 log 方法利用 Aspects ,在替換字體前后 NSLog 字體的字號,這個區(qū)別在參數(shù) AspectPositionBefore 和 AspectPositionAfter
    [self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> info) ...
    [self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> info) ...
  4. 我們看看 Aspects 的Aspects.h文件:
    里面提供兩個方法,
    • 一個是類方法(修改類的所有實例的方法),
    • 一個是實例方法(修改單個實例的方法),
    • 返回值是一個id<AspectToken>可以保存以后取消修改,
    • usingBlock:(id)block 里面的類型id一般情況下可以寫成^(id<AspectInfo> info, ...) ...是要修改的方法的所有參數(shù),如@selector(initWithFrame:) ,block 類型^(id<AspectInfo> info, CGRect frame)
...
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;
...

總結(jié)

Aspects 是 iOS Aspect-oriented programming (AOP) 的一種實現(xiàn),
滿足以下幾點就可以使用(但不是必須滿足才能使用)

  • 原來要有實例方法實現(xiàn)
  • 頻繁調(diào)用,一個一個修改太麻煩
  • 在原來的實例方法的前面和后面可以插入代碼完成需求
  • 最最常用的是log,以后可以一步注釋
[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 ,一種常見的問題是當(dāng)攔截一個方法的時候,它會把相關(guān)類當(dāng)作已攔截,就會報錯(A method can only be hooked once per class hierarchy ),所以當(dāng)方法名相同時要考慮其他方法,這個 Aspects 庫無法滿足需求

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI