2017-07-26 85 views
2

我試圖在按鈕上設置標題的字體。我從蘋果文檔中讀取Objective C中的字體並嘗試實現,但不改變字體。我也試過這個帖子... setting UIButton font。我得到了我的字體從谷歌的字體......「Nunito-Black.tff」如何在按鈕的標題上設置字體Objective-C

這裏是我下面的代碼,

- (void)addHeaderButton { 

    _headerButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    self.headerButton.frame = self.bounds; 
    self.headerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
    [self.headerButton setTitleColor:[UIColor colorWithRed:(0.0/255.0) green:(51.0/255.0) blue:(102.0/255.0) alpha:(1.0)] forState:UIControlStateNormal]; 
    self.headerButton.titleLabel.font = [UIFont fontWithName:@"Nunito-Black" size:25]; 
    //[self.headerButton setFont:[UIFont systemFontOfSize:25]]; 
    [self.headerButton addTarget:self action:@selector(headerButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 

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

是否字體名稱設爲您的尊敬的元素之後獲取字體的名稱它至少用於「UILabel」?是'[UIFont fontWithName:@「Nunito-Black」size:25];'nil? – Larme

+1

您是否已將字體添加到項目中?閱讀[這裏和其他常見錯誤](http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/)。另外,請參閱[包含的字體]列表(http://iosfonts.com/)。 – albertamg

+0

@albertamg是的,我做過 –

回答

3

以下是將自定義字體添加到應用程序的步驟。

第1步:

添加您的自定義字體到您的應用程序包和你Info.plistUIAppFonts字體由應用提供)鍵。它看起來像下面的圖片

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]; 
1

您需要添加字體的確切名稱下的「字體來的Info.plist由應用程序提供'