因此,我尝试更改我的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];发布于 2013-08-27 21:22:45
您使用了错误的键和值顺序,因此它无法工作。
尝尝这个
[[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-11-26 09:37:41
注意工厂方法(value / key)之间的排序方式的差异
[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];发布于 2014-11-25 15:34:00
请注意,从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];https://stackoverflow.com/questions/18464902
复制相似问题