OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在按钮Objective-C的标题上设置字体 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:32
标题: ios - 如何在按钮Objective-C的标题上设置字体

我正在尝试在按钮上设置标题的字体。我从 Apples Documentation on Fonts in Objective C 阅读并尝试实现但它没有更改字体。我也试过这个帖子... setting UIButton font .我的字体来自谷歌字体...“Nunito-Black.tff”

下面是我的代码,

- (void)addHeaderButton {

    _headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.headerButton.frame = self.bounds;
    self.headerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.headerButton setTitleColor:[UIColor colorWithRed0.0/255.0) green51.0/255.0) blue102.0/255.0) alpha1.0)] forState:UIControlStateNormal];
    self.headerButton.titleLabel.font = [UIFont fontWithName"Nunito-Black" size:25];
    //[self.headerButton setFont:[UIFont  systemFontOfSize:25]];
    [self.headerButton addTarget:self actionselector(headerButtonAction forControlEvents:UIControlEventTouchUpInside];

    [self.contentView addSubview:self.headerButton];
}



Best Answer-推荐答案


以下是在您的应用中添加自定义字体的步骤。

步骤:1

使用 UIAppFonts(应用程序提供的字体)键将您的自定义字体添加到您的应用程序包和 Info.plist。如下图所示

enter image description here

步骤:2

使用以下代码段获取自定义添加的字体名称

[Objective-C]

for (NSString *familyName in [UIFont familyNames]){
        NSLog(@"Family name: %@", familyName);
        for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
            NSLog(@"--Font name: %@", fontName);
        }
    }

[ swift - 3]

for family in UIFont.familyNames {
            print("\(family)")

            for name in UIFont.fontNames(forFamilyName: family) {
                print("   \(name)")
            }
        }

步骤:3

从上面的代码片段中获取字体的名称,获取名称后,您可以将字体名称设置为您尊重的元素,如下所示

self.headerButton.titleLabel.font = [UIFont fontWithName"Nunito-Black" size:25];

关于ios - 如何在按钮Objective-C的标题上设置字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45332900/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4