2013-08-27 21 views
8

Próbowałem zmienić atrybut tekstowy tytułu mojego UISegmentedControl, ale to nie działa, nic się nie zmienia. Zastosowałem również niestandardowe tło i dzielnik i działa poprawnie, ale nie to.UISegmentedControl setTitleTextAttributes nie działa

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

Odpowiedz

11

Użyto złej kolejności klawiszy i wartości, więc nie działa.

Try This

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

działa! więc ta metoda nie lubi dosłowności słownikowej? – harinsa

+1

Literały słownikowe działają dobrze; jeśli tego nie zrobi, wystąpi poważny błąd w iOS! '[self setTitleTextAttributes: @ {UITextAttributeTextColor: [UIColor redColor]} forState: UIControlStateNormal];' – NathanAldenSr

+1

Byłoby miło, gdyby odpowiedzi wskazywałyby na to, co jest nie tak, zamiast publikowania czegoś, co działa. –

10

Strzeż różnicy w jaki sposób zamówić par między metody fabryki (wartości/klucz)

[NSDictionary dictionaryWithObjectsAndKeys: value, key, nil] 

i dosłowne deklaracji (klucz/wartość)

@{key: value} 

Po prostu używasz złej kolejności klucza i wartości.

to będzie działać:

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


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

To powinna być akceptowana odpowiedź – Renetik

6

Należy pamiętać, że od iOS 7 niektóre z tych klawiszy zostały już przestarzała. Będziesz teraz musiał użyć czegoś takiego:

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