2013-08-27 2 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

키와 값의 순서가 잘못되어 작동하지 않습니다.

(당신은 팩토리 메소드 사이에 쌍 (/)

[NSDictionary dictionaryWithObjectsAndKeys: value, key, nil] 

및 리터럴 선언을 주문하는 방법이

[[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에 심각한 버그가있을 것입니다! '[UITextAttributeTextAttributes : @ {UITextAttributeTextColor : [UIColor redColor]} forState : UIControlStateNormal]; ' – NathanAldenSr

+1

답변이 뭔가 효과가있는 것을 게시하는 것이 아니라 잘못된 것을 지적한다면 좋을 것입니다. –

10

가의 차이에주의 키를 사용해보십시오/)

@{key: value} 

잘못된 키와 값의 순서 만 사용하면됩니다.

이 작동합니다 : 아이폰 OS 7 이후 이러한 키 중 일부는 현재 사용되지 않습니다 것을

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


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

답변을 수락해야합니다 – Renetik

6

참고. 이제 다음과 같은 것을 사용해야합니다 :

[[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]; 
관련 문제