答案 0 :(得分:8)
同时解决这个问题,这是我的发现。这真是一团糟。
iOS7上没有中等系统字体,他们在iOS 8.2中添加了它。在iOS7上,经过长时间的延迟后,它会按字母顺序选择第一个字体(Academy Engraved)。
有趣的是,iOS 7粗体系统字体实际上是Medium Helvetica Neue字体:
(lldb) po [UIFont boldSystemFontOfSize:12]
<UICTFont: 0x12c58f8b0> font-family: ".HelveticaNeueInterface-MediumP4"; font-weight: bold; font-style: normal; font-size: 12.00pt
并且systemFont是常规的Helvetica Neue。
iOS 7的解决方法是在界面构建器中选择System Bold字体,它在iOS7设备上运行时看起来比在界面构建器上看起来更薄。不幸的是,在iOS8和iOS9上,它看起来真的很大而且不是中等......
我最终切换到Helvetica-Neue Medium用于那些不幸意味着我在iOS 9的某些屏幕上与系统字体/旧金山和Helvetica-Neue不匹配的情况。 无法等待获得支持iOS7的绿灯。
答案 1 :(得分:4)
我使用方法调配来修复此iOS 7错误。我的方法适用于Interface Builder。
<强> UIFont + Swizzling.h 强>
@import UIKit;
@interface UIFont (UIFont_Swizzle)
@end
<强> UIFont + Swizzling.m 强>
#import "UIFont+Swizzle.h"
@import ObjectiveC.runtime;
@implementation UIFont (UIFont_Swizzle)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if ([NSProcessInfo instancesRespondToSelector:@selector(operatingSystemVersion)]) return;
Class class = object_getClass((id)self);
SEL originalSelector = @selector(fontWithDescriptor:size:);
SEL swizzledSelector = @selector(swizzled_fontWithDescriptor:size:);
Method originalMethod = class_getClassMethod(class, originalSelector);
Method swizzledMethod = class_getClassMethod(class, swizzledSelector);
BOOL didAddMethod = class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
+ (UIFont *)swizzled_fontWithDescriptor:(UIFontDescriptor *)descriptor size:(CGFloat)pointSize {
id usageAttribute = descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"];
if (!descriptor.fontAttributes[UIFontDescriptorNameAttribute] &&
[usageAttribute isKindOfClass:[NSString class]] &&
[usageAttribute isEqualToString:@"CTFontMediumUsage"]) {
descriptor = [descriptor fontDescriptorByAddingAttributes:@{UIFontDescriptorNameAttribute: @"HelveticaNeue-Medium"}];
}
id font = [self swizzled_fontWithDescriptor:descriptor size:pointSize];
return font;
}
@end
特别感谢Xtrace的创作者:)
答案 2 :(得分:0)
检查版本号,如果小于8,则在viewDidLoad中以编程方式更改字体:
if !NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: 8, minorVersion: 0, patchVersion: 0)) {
//change font
}