2013-08-27 18 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に深刻なバグがあるだろう! '@ [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]; 
関連する問題