7

所以我试图改变我的 UISegmentedControl 的标题的文本属性,但它不起作用,没有任何改变。我还应用了自定义背景和分隔线,它可以正常工作,但不是这个。

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

3 回答 3

10

请注意您在工厂方法(值/)之间订购对的方式的差异

[NSDictionary dictionaryWithObjectsAndKeys: value, key, nil]

和文字声明(/

@{key: value}

您只是使用了错误的键和值顺序。

这将起作用:

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


[[UISegmentedControl appearance] setTitleTextAttributes:normaltextAttr forState:UIControlStateNormal];
于 2013-11-26T01:37:40.540 回答
9

您使用了错误的键和值顺序,因此它不起作用。

尝试这个

[[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];
于 2013-08-27T13:22:45.220 回答
6

请注意,从 iOS 7 开始,其中一些键现已被弃用。你现在需要使用类似的东西:

[[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];
于 2014-11-25T07:33:59.533 回答