2013-06-18 29 views
0

我已經編寫了這個代碼來爲UISegmentedControl設置正常狀態和突出顯示的狀態的字體屬性,但它不工作。請幫我指出原因。在UISegmentedControl的選定選項卡上可以有不同的樣式字體嗎?

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica" size:12.0f], UITextAttributeFont, 

          [UIColor blackColor], UITextAttributeTextColor, 

          nil]; 

NSDictionary *boldAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Bold" size:12.0f], UITextAttributeFont, 

          [UIColor blackColor], UITextAttributeTextColor, 

          nil]; 



[self.tab setTitleTextAttributes:attributes forState:UIControlStateNormal]; 

[self.tab setTitleTextAttributes:boldAttributes forState:UIControlStateHighlighted]; 

我希望選定的文本是粗體,否則它應該是正常的。

+0

我設法只改變文字顏色,設置字體沒有做任何改變 –

+0

你試過UIControlStateSelected了嗎? – Tim

+0

不,我沒有嘗試,現在我又增加了一行:[self.tab setTitleTextAttributes:boldAttributes forState:UIControlStateSelected]; – Shahnawaz

回答

1

我已經嘗試過了類似下面

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
         [UIFont boldSystemFontOfSize:17], UITextAttributeFont, 
         [UIColor blackColor], UITextAttributeTextColor, 
         nil]; 
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; 
[_segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted]; 

,並設置字體

[UIFont fontWithName:@"FontName" size:desiredsize.f] 

也改變了突出選定。

+0

我之前將不同的標籤顏色更改爲淺藍色和綠色,因爲它是必需的,所以我不能將其更改爲白色,因爲它當時不能看到。一種方法來看看是否加粗,出於某種原因,我的代碼無法工作。 – Shahnawaz

+0

此外,我只是試圖改變高亮選擇和相同的結果 – Shahnawaz

+0

你可以添加自己的色調,而不是我在我的代碼中的顏色。而對於粗體的使用,只需在字體名稱末尾加上一個。它通常適用於我,但請記住,for必須是uifont家族的一部分。 –

0

變化

[self.tab setTitleTextAttributes:boldAttributes forState:UIControlStateHighlighted]; 

[self.tab setTitleTextAttributes:boldAttributes forState:UIControlStateSelected]; 
1

這個答案是過時,但對於那些誰正在尋找迅速的解決方案,你可能會想嘗試這種方法:

func stylizeFonts(){ 
    let normalFont = UIFont(name: "Helvetica", size: 16.0) 
    let boldFont = UIFont(name: "Helvetica-Bold", size: 16.0) 

    let normalTextAttributes: [NSObject : AnyObject] = [ 
     NSForegroundColorAttributeName: UIColor.blackColor(), 
     NSFontAttributeName: normalFont! 
    ] 

    let boldTextAttributes: [NSObject : AnyObject] = [ 
     NSForegroundColorAttributeName : UIColor.whiteColor(), 
     NSFontAttributeName : boldFont!, 
    ] 

    self.setTitleTextAttributes(normalTextAttributes, forState: .Normal) 
    self.setTitleTextAttributes(normalTextAttributes, forState: .Highlighted) 
    self.setTitleTextAttributes(boldTextAttributes, forState: .Selected) 
} 

確保在您的viewDidLoad中添加stylizeFonts(),或者如果您是子類,則將其作爲單獨的函數添加。

相關問題