首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UISegmentedControl setTitleTextAttributes不工作

UISegmentedControl setTitleTextAttributes不工作
EN

Stack Overflow用户
提问于 2013-08-27 20:05:29
回答 3查看 9.4K关注 0票数 7

因此,我尝试更改我的UISegmentedControl的标题的文本属性,但它不起作用,什么也没改变。我还应用了一个自定义的背景和分隔符,它可以正常工作,但不是这个。

代码语言:javascript
复制
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];
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-27 21:22:45

您使用了错误的键和值顺序,因此它无法工作。

尝尝这个

代码语言:javascript
复制
[[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];
票数 9
EN

Stack Overflow用户

发布于 2013-11-26 09:37:41

注意工厂方法(value / key)之间的排序方式的差异

代码语言:javascript
复制
[NSDictionary dictionaryWithObjectsAndKeys: value, key, nil]

和文字声明(/)

代码语言:javascript
复制
@{key: value}

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

这将会起作用:

代码语言:javascript
复制
NSDictionary *normaltextAttr = 
       @{UITextAttributeTextColor : [UIColor blackColor],
         UITextAttributeTextShadowColor : [UIColor  clearColor],
         UITextAttributeFont : [UIFont fontWithName:_regularFont size:20.f]};


[[UISegmentedControl appearance] setTitleTextAttributes:normaltextAttr forState:UIControlStateNormal];
票数 10
EN

Stack Overflow用户

发布于 2014-11-25 15:34:00

请注意,从iOS 7开始,这些键中的一些现在已被弃用。您现在需要使用类似以下内容:

代码语言:javascript
复制
[[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];
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18464902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档