ios - iOS加载xib时会调用API吗?

标签 ios storyboard xib

当我打开一个 xib 文件作为源代码时,我发现一些代码如下:

<fontDescription key="fontDescription" type="system" pointSize="16"/>

当这个 xib 文件加载时,它会调用 UIFont API 吗?

最佳答案

是的,它调用UIFont API。我测试了这个:

//
//  UIFont+Swizzled.m
//  Test
//
//  Created by Brandon T on 2016-02-29.
//  Copyright © 2016 Test. All rights reserved.
//

#import "UIFont+Swizzled.h"
#import <objc/runtime.h>

@implementation UIFont (Swizzled)

+ (void)load {
    //Quick and dirty swizzle. Should really only run once and check if method is added.. but w/e..

    Method original, swizzled;

    original = class_getClassMethod(self, @selector(fontWithName:size:));
    swizzled = class_getClassMethod(self, @selector(fontWithNameX:size:));
    method_exchangeImplementations(original, swizzled);

    original = class_getClassMethod(self, @selector(fontWithSize:));
    swizzled = class_getClassMethod(self, @selector(fontWithSizeX:));
    method_exchangeImplementations(original, swizzled);

    original = class_getClassMethod(self, @selector(systemFontOfSize:));
    swizzled = class_getClassMethod(self, @selector(systemFontOfSizeX:));
    method_exchangeImplementations(original, swizzled);

    original = class_getClassMethod(self, @selector(fontWithDescriptor:size:));
    swizzled = class_getClassMethod(self, @selector(fontWithDescriptorX:size:));
    method_exchangeImplementations(original, swizzled);
}

+ (nullable UIFont *)fontWithNameX:(NSString *)fontName size:(CGFloat)fontSize {
    return [self fontWithNameX:fontName size:fontSize];
}

- (UIFont *)fontWithSizeX:(CGFloat)fontSize {
    return [self fontWithSizeX:fontSize];
}

+ (UIFont *)systemFontOfSizeX:(CGFloat)fontSize {
    return [self systemFontOfSizeX:fontSize];
}

+ (UIFont *)fontWithDescriptorX:(UIFontDescriptor *)descriptor size:(CGFloat)pointSize {
    return [self fontWithDescriptorX:descriptor size:pointSize];
}
@end

然后,我使用单个 Controller 创建了一个空白 Storyboard,为其添加了一个标签,并将字体更改为 17 号的 Helvetica Neue。

接下来,我将上述类别文件包含到与 Storyboard关联的 Controller 中。

//
//  ViewController.m
//  Test
//
//  Created by Brandon T on 2016-02-27.
//  Copyright © 2016 Test. All rights reserved.
//

#import "ViewController.h"
#import "UIFont+Swizzled.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

结果:

2016-02-29 22:12:19.585 Test[94691:4139779] systemFontOfSize -- Size:17.000000

2016-02-29 22:12:19.586 Test[94691:4139779] fontWithDescriptor  -- Descriptor: UICTFontDescriptor <0x7fe60340e770> = {
    NSFontNameAttribute = HelveticaNeue;
    NSFontSizeAttribute = 17;
}, Size:17.000000

所以它调用了两件事。首先是SystemFontOfSize..然后它调用FontWithDescriptor

关于ios - iOS加载xib时会调用API吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35713712/

相关文章:

ios - 在搜索栏中输入内容时导航栏消失

ios - 是否可以从 xib 文件中为 UI 控件(例如 UIButton)设置 UIAccessibilityFrame

iOS Eureka 创建后更改节标题

ios - 无法使用 Xcode 在模拟器中运行应用程序

ios - YouTube 嵌入视频与 WKWebView 开始随机播放

IOS StoryBoard 约束;底部按钮上方的大空间

ios - RealmSwift : How to create To-One Relationships properly?

ios - Storyboard 中自定义类集的本地化不起作用

ios - 带有 xib 文件的 Swift 错误 IBOutlet

ios - Horizo​​ntal UIStackView - 如何对齐内部项目,以便一个向左浮动,另一个固定在中间?