7

So I tried to change the text attribute of the title of my UISegmentedControl, but it doesn't work, nothing change. I have also applied a custom background and divider and it works correctly, but not this.

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];
memmons
  • 40,222
  • 21
  • 149
  • 183
harinsa
  • 3,176
  • 5
  • 33
  • 53

3 Answers3

10

Beware of the difference in how you order your pairs between the factory method (value / key)

[NSDictionary dictionaryWithObjectsAndKeys: value, key, nil]

and the literal declaration (key / value)

@{key: value}

You simply use the wrong order of key and value.

This will work:

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


[[UISegmentedControl appearance] setTitleTextAttributes:normaltextAttr forState:UIControlStateNormal];
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Olivier
  • 858
  • 8
  • 17
9

You have use the wrong order of keys and values, so it is not working.

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];
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
  • 1
    Dictionary literals work fine; if they didn't, there'd be a serious bug in iOS! `[self setTitleTextAttributes:@{ UITextAttributeTextColor: [UIColor redColor] } forState:UIControlStateNormal];` – NathanAldenSr Oct 26 '13 at 02:29
  • 1
    It would be nice if answers would point out what's wrong instead of just posting something that works. – Matthias Bauch Jan 14 '14 at 11:00
6

Note that since iOS 7 some of these keys have now been deprecated. You will now need to use something like:

[[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];
Reefwing
  • 2,242
  • 1
  • 22
  • 23