2013-08-27 26 views
8

Vì vậy, tôi đã cố gắng thay đổi thuộc tính văn bản của tiêu đề của UISegmentedControl của tôi, nhưng nó không hoạt động, không có gì thay đổi. Tôi cũng đã áp dụng nền tùy chỉnh và dải phân cách và nó hoạt động chính xác, nhưng không áp dụng điều này.UISegmentedControl setTitleTextAttributes không hoạt động

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]; 

Trả lời

11

Bạn đã sử dụng thứ tự sai các khóa và giá trị, do đó, nó không hoạt động.

Hãy thử này

[[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

hoạt động! vì vậy phương pháp này không thích từ điển nghĩa đen? – harinsa

+1

Từ điển nghĩa đen hoạt động tốt; nếu không, sẽ có một lỗi nghiêm trọng trong iOS! '[self setTitleTextAttributes: @ {UITextAttributeTextColor: [UIColorAolorolor]} forState: UIControlStateNormal]; ' – NathanAldenSr

+1

Sẽ rất tuyệt nếu câu trả lời chỉ ra điều gì sai trái thay vì chỉ đăng một thứ gì đó hoạt động. –

10

Cảnh giác với những sự khác biệt trong cách bạn đặt cặp của bạn giữa các phương pháp nhà máy (giá trị/chính)

[NSDictionary dictionaryWithObjectsAndKeys: value, key, nil] 

và việc kê khai nghĩa đen (chủ chốt/giá trị)

@{key: value} 

Bạn chỉ cần sử dụng thứ tự khóa và giá trị sai.

này sẽ làm việc:

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


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

Câu trả lời này phải được chấp nhận – Renetik

6

Lưu ý rằng kể từ khi iOS 7 một số các phím này hiện nay đã được tán thành. Bây giờ bạn sẽ cần phải sử dụng một cái gì đó như:

[[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];