2009-08-19 79 views
28

我的應用程序中有以下代碼。UILabel - 以編程方式在iPhone中設置字體 - 字體

tmp=[[UILabel alloc] initWithFrame:label1Frame]; 
tmp.tag=1; 
tmp.textColor=[UIColor blackColor]; 
[tmp setFont:[UIFont fontWithName:@"American Typewriter" size:18]]; 
tmp.backgroundColor=[UIColor clearColor]; 
[cell.contentView addSubview:tmp]; 
[tmp release]; 

現在,我需要知道,

======>如何通過代碼集 「字樣=大膽」?

+0

「標籤的陰影偏移」是指標籤陰影的大小嗎? – devinfoley 2009-08-19 21:59:35

+0

@flashcards - 我得到的解決方案不需要找出影子。 [tmp setshadowoffset:cgsizemake(1,1)]; – 2009-08-19 22:13:41

+2

@sagar您應該將其作爲您自己問題的答案發布,以便保存。乾杯! – devinfoley 2009-08-19 22:20:00

回答

36

您將需要使用家族內的bold字體的名稱。要了解是否有bold版本的美國打字機,嘗試輸出

[UIFont fontNamesForFamilyName:@"AmericanTypewriter"] 

到控制檯。

在這種情況下,您應該使用"AmericanTypewriter-Bold"

[UIFont fontNamesForFamilyName:@"AmericanTypewriter-Bold"] 
+0

@flashcards - 不,我試過你的邏輯,但我的應用程序崩潰了。 – 2009-08-19 22:07:03

+0

嗯......看起來像fontNamesForFamilyName是UIFont上的一個靜態方法,它帶有一個姓氏。我現在會解決我的問題。你嘗試使用「AmericanTypewriter-Bold」嗎? – devinfoley 2009-08-19 22:17:49

+5

@sagar - 「美國打字機」之間沒有空格。我認爲你已經在他們之間放置了空間。 – Brij 2009-12-11 13:20:32

20

怎麼樣使用setter屬性:

// Create a string 
NSString *text = @"You are getting sleepy."; 

// Get a font to draw it in 
UIFont *font = [UIFont boldSystemFontOfSize:28]; 

// Draw the string 
[text drawInRect:(CGRect) 
withFont:font]; 
+0

這隻有在你想使用系統字體時纔有用。 @Spark沒有使用系統字體。 – 2012-10-03 12:29:20

17

剛的NSLog你想要得到的字體:

NSLog(@" %@", [UIFont fontNamesForFamilyName:@"American Typewriter"]); 

,你會得到數組:

(
    "AmericanTypewriter-CondensedLight", 
    "AmericanTypewriter-Light", 
    "AmericanTypewriter-Bold", 
    AmericanTypewriter, 
    "AmericanTypewriter-CondensedBold", 
    "AmericanTypewriter-Condensed" 
) 

使用任何你需要的代碼:

UILabel * loading = [[UILabel alloc] initWithFrame:CGRectMake(350, 402, 150, 25)]; 
    [loading setFont:[UIFont fontWithName:@"AmericanTypewriter-Bold" size:12.0]]; 
    [loading setTextColor:[UIColor whiteColor]]; 
    [loading setBackgroundColor:[UIColor clearColor]]; 
    [loading setText:@"Loading ..."]; 
    [self.view addSubview:loading]; 
1

像@devinfoley寫道,你必須添加-BoldfontName你使用。對我來說,它不適用於fontNamesForFamilyName,而是我使用fontWithName:size。如果以編程方式創建UILabel,可能會有效。在我的情況下,我只是將font設置在我的UILabel的擴展名內awakeFromNib中,並將此擴展名設置爲Identity Inspector中的class,用於我的特定UILabel。使用self.font.pointSize,您可以在Interface Builder內設置fontSize,如果您有更多的UI元素,可以更好地處理它。

- (void)awakeFromNib{ 

    [super awakeFromNib]; 
    [self setAdjustsFontSizeToFitWidth:YES]; 
    [self setFont:[UIFont fontWithName:@"Font-Bold" size:self.font.pointSize]]; 
} 

如果您font不工作,你應該打印所有的家庭fonts,所以你可以看到,如果你寫的fontName錯誤。這樣你也可以檢查是否有bold字體可用,如果沒有打印你沒有這個選項。

NSLog (@"Available Fonts: %@", [UIFont fontNamesForFamilyName:@"Your Font"]); 

更多font東西:https://stackoverflow.com/a/8529661/1141395

2

你可以在這個網站iOSfonts支持的字體名稱的所有iOS。 把你的確切字符串的預覽找到最好的字體和使用它像上面的答案

UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 150, 50)]; 
    [loading setFont:[UIFont fontWithName:@"Avenir-Black" size:12.0]]; 
    [loading setTextColor:[UIColor redColor]]; 
    [loading setBackgroundColor:[UIColor clearColor]]; 
    [loading setText:@"My Label Title"]; 
    [self.view myLabel]; 
0

斯威夫特更新

提到如果您已經添加的字體應用程序(如解釋here ),可以使用雨燕的extension功能,以作爲例如UILabel和字體的Roboto:

extension UILabel { 

    func setFont(style: String, size: Int){ 
     self.font = UIFont(name: style, size: CGFloat(size)) 
    } 

    struct FontStyle { 
     public static let italic: String = "Roboto-LightItalic" 
     public static let normal: String = "Roboto-Light" 
     public static let thin: String = "Roboto-Thin" 
    } 
} 

Roboto-LightItalicRoboto-Light是TTF的字體文件名。然後,您可以簡單地致電

someUILabel.setFont(style: UILabel.FontStyle.normal, size: 20) 
相關問題