自定义字体可以在 Storyboard 中使用吗?
Custom font can be used in Storyboard?
有没有办法在故事板中使用自定义字体?这里的目的是通过情节提要本身涵盖组件样式,并将反映我可以从一个地方从 FontA 更改为 FontB 的 font.So。我知道这可以通过代码来完成,但是很多 UI 的约束是在情节提要中设计的,并且没有创建许多出口。
我只是想知道这是否可能?
是的,可以在故事板中使用自定义字体。
- 将字体文件添加到您的 Xcode 项目。
- 在
Info.plist
中注册您的字体:
UIAppFonts :
Item 0 : MyCustomFont.ttf
您的字体现在应该显示在 Storyboard 中。
更多信息请参考Apple Docs。
是的。您可以在情节提要中使用自定义字体。
按照以下步骤操作:
- 将您的字体包含在您的 XCode 项目中。
- 确保它们包含在目标中。
- 仔细检查您的字体是否作为资源包含在您的包中。
- 在您的应用程序 plist 中包含您的 iOS 自定义字体。
找到字体的名称。
您可以使用以下代码片段找到字体名称:
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/
可能对你有帮助。
是的,您可以从情节提要中使用它们。将您的自定义字体文件添加到应用程序资源并在 info.plist 中提及它们。在这里,我将 bwnistageometricdemo-regular 添加到 info.plist.
<key>UIAppFonts</key>
<array>
<string>bwnistageometricdemo-regular.ttf</string>
</array>
你可以从情节提要中使用它,比如 -
我们发现了两种方法:
1.使用method swizzling
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。
感谢您在上面的所有回答,但我发现这种方式更有效,覆盖面更广,用时更短。现在我可以创建我的字体枚举,并可以设置任何我想更改的内容,而不必担心我是从情节提要还是代码中设置了字体。
有没有办法在故事板中使用自定义字体?这里的目的是通过情节提要本身涵盖组件样式,并将反映我可以从一个地方从 FontA 更改为 FontB 的 font.So。我知道这可以通过代码来完成,但是很多 UI 的约束是在情节提要中设计的,并且没有创建许多出口。
我只是想知道这是否可能?
是的,可以在故事板中使用自定义字体。
- 将字体文件添加到您的 Xcode 项目。
- 在
Info.plist
中注册您的字体:
UIAppFonts :
Item 0 : MyCustomFont.ttf
您的字体现在应该显示在 Storyboard 中。
更多信息请参考Apple Docs。
是的。您可以在情节提要中使用自定义字体。
按照以下步骤操作:
- 将您的字体包含在您的 XCode 项目中。
- 确保它们包含在目标中。
- 仔细检查您的字体是否作为资源包含在您的包中。
- 在您的应用程序 plist 中包含您的 iOS 自定义字体。
找到字体的名称。
您可以使用以下代码片段找到字体名称:
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/
可能对你有帮助。
是的,您可以从情节提要中使用它们。将您的自定义字体文件添加到应用程序资源并在 info.plist 中提及它们。在这里,我将 bwnistageometricdemo-regular 添加到 info.plist.
<key>UIAppFonts</key>
<array>
<string>bwnistageometricdemo-regular.ttf</string>
</array>
你可以从情节提要中使用它,比如 -
我们发现了两种方法: 1.使用method swizzling 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。
感谢您在上面的所有回答,但我发现这种方式更有效,覆盖面更广,用时更短。现在我可以创建我的字体枚举,并可以设置任何我想更改的内容,而不必担心我是从情节提要还是代码中设置了字体。