2013-08-27 27 views
8

所以我試圖改變我的UISegmentedControl的標題的文本屬性,但它不起作用,沒有任何改變。我也應用了自定義背景和分隔線,它可以正常工作,但不是這樣。UISegmentedControl setTitleTextAttributes不起作用

NSDictionary *normaltextAttr = 
      @{[UIColor blackColor]: UITextAttributeTextColor, 
       [UIColor clearColor]: UITextAttributeTextShadowColor, 
       [UIFont fontWithName:_regularFont size:20.f]: UITextAttributeFont}; 


NSDictionary *selectedtextAttr = 
      @{[UIColor colorWithRed:135.0/255.0 green:135.0/255.0 blue:135.0/255.0 alpha:1.0]: UITextAttributeTextColor, 
       [UIColor clearColor]: UITextAttributeTextShadowColor, 
       [NSValue valueWithUIOffset:UIOffsetMake(0, 1)]: UITextAttributeTextShadowOffset, 
       [UIFont fontWithName:_regularFont size:0.0]: UITextAttributeFont}; 

[[UISegmentedControl appearance] setTitleTextAttributes:normaltextAttr 
               forState:UIControlStateNormal]; 
[[UISegmentedControl appearance] setTitleTextAttributes:selectedtextAttr 
               forState:UIControlStateSelected]; 

回答

11

您使用了錯誤的鍵和值的順序,所以它不起作用。

嘗試這個

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                 [UIColor blackColor],UITextAttributeTextColor, 
                 [UIColor clearColor], UITextAttributeTextShadowColor, 
                 [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0], UITextAttributeFont, nil] forState:UIControlStateNormal]; 

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                 [UIColor colorWithRed:135.0/255.0 green:135.0/255.0 blue:135.0/255.0 alpha:1.0],UITextAttributeTextColor, 
                 [UIColor clearColor], UITextAttributeTextShadowColor, 
                 [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, 
                 [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0], UITextAttributeFont, nil] forState:UIControlStateSelected]; 
+0

的作品!所以這種方法不喜歡字典文字? – harinsa

+1

字典文字工作正常;如果他們不這樣做,iOS中會出現嚴重的錯誤! '[self setTitleTextAttributes:@ {UITextAttributeTextColor:[UIColor redColor]} forState:UIControlStateNormal];' – NathanAldenSr

+1

如果答案指出什麼是錯誤的,而不是隻發佈一些有用的東西,那將是很好的。 –

10

在小心的差異如何訂購工廠方法之間的配對(/

[NSDictionary dictionaryWithObjectsAndKeys: value, key, nil] 

和文字聲明(關鍵/

@{key: value} 

您只需使用錯誤的鍵和值的順序。

這將工作:

NSDictionary *normaltextAttr = 
     @{UITextAttributeTextColor : [UIColor blackColor], 
     UITextAttributeTextShadowColor : [UIColor clearColor], 
     UITextAttributeFont : [UIFont fontWithName:_regularFont size:20.f]}; 


[[UISegmentedControl appearance] setTitleTextAttributes:normaltextAttr forState:UIControlStateNormal]; 
+0

這應該是可以接受的答案 – Renetik

6

注意,因爲iOS的7某些鍵現在已經過時了。您現在需要使用類似的東西:

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                 [UIColor blackColor], NSForegroundColorAttributeName, 
                 [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0], NSFontAttributeName, nil] forState:UIControlStateNormal]; 

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                 [UIColor colorWithRed: 135.0/255.0 green: 135.0/255.0 blue: 135.0/255.0 alpha: 1.0],NSForegroundColorAttributeName, 
                 [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0], NSFontAttributeName, nil] forState:UIControlStateSelected];