当我打开一个 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/