自定义的字体可以在故事板可以用吗?

问题描述 投票:1回答:4

有没有办法使用自定义字体的故事板?这里目的是覆盖部件通过故事板本身的造型和反映,我可以从FontA到FontB从一个地方改变font.So。我知道它可以通过代码来完成,但制约因素是很多的UI设计在故事板和网点多不创建。

我只是想知道是否有可能?

ios xcode fonts uistoryboard
4个回答
0
投票

是的,自定义字体可以在故事板内使用。

  1. 字体文件添加到您的Xcode项目。
  2. 注册在Info.plist您的字体:
UIAppFonts : 
Item 0 : MyCustomFont.ttf

现在,字体应在故事板显示。

请参阅Apple Docs以获得更多信息。


0
投票

是。您可以使用自定义的字体与故事板。

跟着这些步骤 :

  • 在您的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/

它可以帮助你。


0
投票

是的,你可以从故事板中使用它们。您的自定义字体文件添加到应用程序资源,并提到他们info.plist中。在这里,我将bwnistageometricdemo,定期到Info.plist中。

<key>UIAppFonts</key> <array> <string>bwnistageometricdemo-regular.ttf</string> </array>

你可以用它从故事板一样 -

enter image description here


0
投票

有报道说,我们在途中发现两种方法: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。

感谢您的解答以上,但我发现在更短的时间这种方式更有效,更全面覆盖。现在,我可以创造我的字体枚举,并可以设置任何我想改变,而不会打扰我是否已经从故事板或代码设置字体。

© www.soinside.com 2019 - 2024. All rights reserved.