Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISegmentedControl setTitleTextAttributes does not work

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];
like image 405
harinsa Avatar asked Aug 27 '13 12:08

harinsa


2 Answers

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];
like image 125
Olivier Avatar answered Nov 13 '22 03:11

Olivier


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];
like image 24
Toseef Khilji Avatar answered Nov 13 '22 05:11

Toseef Khilji