有没有办法使用自定义字体的故事板?这里目的是覆盖部件通过故事板本身的造型和反映,我可以从FontA到FontB从一个地方改变font.So。我知道它可以通过代码来完成,但制约因素是很多的UI设计在故事板和网点多不创建。
我只是想知道是否有可能?
是的,自定义字体可以在故事板内使用。
Info.plist
您的字体:UIAppFonts :
Item 0 : MyCustomFont.ttf
现在,字体应在故事板显示。
请参阅Apple Docs以获得更多信息。
是。您可以使用自定义的字体与故事板。
跟着这些步骤 :
override func viewDidLoad() {
super.viewDidLoad()
for family: String in UIFont.familyNames
{
print(family)
for names: String in UIFont.fontNames(forFamilyName: family)
{
print("== \(names)")
}
}
}
检查:https://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
它可以帮助你。
有报道说,我们在途中发现两种方法:1。使用方法交叉混合2.创建类别UIFont和覆盖的方法
#import "SystemFontOverride.h"
@implementation UIFont (SystemFontOverride)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
// do your override
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {
UIFont *font = [UIFont fontWithName:@"Raleway-Regular" size:fontSize];
if (font != nil) {
return font;
}
return [UIFont systemFontOfSize:fontSize];
}
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize {
UIFont *font = [UIFont fontWithName:@"Raleway-Bold" size:fontSize];
if (font != nil) {
return font;
}
return [UIFont systemFontOfSize:fontSize];
}
+ (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize {
UIFont *font = [UIFont fontWithName:@"Raleway-Italic" size:fontSize];
if (font != nil) {
return font;
}
return [UIFont systemFontOfSize:fontSize];
}
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize weight:(UIFontWeight)weight NS_AVAILABLE_IOS(8_2) {
NSString *fontName;
if (weight == UIFontWeightSemibold) {
fontName = @"Raleway-SemiBold";
} else if (weight == UIFontWeightBold) {
fontName = @"Raleway-Bold";
} else if (weight == UIFontWeightRegular){
fontName = @"Raleway-Regular";
} else if (weight == UIFontWeightMedium){
fontName = @"Raleway-Medium";
} else if (weight == UIFontWeightThin){
fontName = @"Raleway-Thin";
} else if (weight == UIFontWeightBlack) {
fontName = @"Raleway-Black";
}
if (fontName != nil && fontName.length > 0) {
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
if (font != nil) {
return font;
}
}
return [UIFont systemFontOfSize:fontSize];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
if (self) {
BOOL result = [coder containsValueForKey:@"UIFontDescriptor"];
if (result) {
UIFontDescriptor *descriptor = [coder decodeObjectForKey:@"UIFontDescriptor"];
NSString *fontName;
if ([descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"] isEqualToString:@"CTFontRegularUsage"]) {
fontName = @"Raleway-Regular";
}
else if ([descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"] isEqualToString:@"CTFontEmphasizedUsage"]) {
fontName = @"Raleway-Bold";
}
else if ([descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"] isEqualToString:@"CTFontObliqueUsage"]) {
fontName = @"Raleway-Italic";
}
else {
fontName = descriptor.fontAttributes[@"NSFontNameAttribute"];
}
return [UIFont fontWithName:fontName size:descriptor.pointSize];
}
}
return self;
}
#pragma clang diagnostic pop
@end
另一个渔获物覆盖它们在故事板,我们需要实现的initWithCoder设置systemFont。
感谢您的解答以上,但我发现在更短的时间这种方式更有效,更全面覆盖。现在,我可以创造我的字体枚举,并可以设置任何我想改变,而不会打扰我是否已经从故事板或代码设置字体。