内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

iOS開發(fā):UILabel字號根據(jù)屏幕縮放-創(chuàng)新互聯(lián)

場景:

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

創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、海拉爾網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、HTML5商城網(wǎng)站開發(fā)、集團公司官網(wǎng)建設、外貿(mào)營銷網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為海拉爾等各大城市提供網(wǎng)站開發(fā)制作服務。

解決方案:

如果一個一個設置太麻煩,容易遺漏,這時候我們采用 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 ,一種常見的問題是當攔截一個方法的時候,它會把相關類當作已攔截,就會報錯(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)

搜索引擎優(yōu)化
武邑县| 齐河县| 玉田县| 东乌珠穆沁旗| 崇仁县| 广水市| 上饶县| 赣州市| 锡林浩特市| 巴楚县| 独山县| 安多县| 永清县| 襄汾县| 赤城县| 青阳县| 察哈| 杭锦后旗| 汪清县| 阜城县| 锡林郭勒盟| 桐城市| 阿荣旗| 松溪县| 桃江县| 日喀则市| 德保县| 岳西县| 江阴市| 同江市| 大渡口区| 曲水县| 清涧县| 即墨市| 淮阳县| 轮台县| 工布江达县| 滕州市| 石家庄市| 桃园市| 读书|