Ios UISegmentedControl setTitleTextAttributes不工作
因此,我尝试更改UISegmentedControl标题的文本属性,但没有效果,没有任何更改。我还应用了自定义背景和分隔符,它工作正常,但不是这个Ios UISegmentedControl setTitleTextAttributes不工作,ios,uisegmentedcontrol,Ios,Uisegmentedcontrol,因此,我尝试更改UISegmentedControl标题的文本属性,但没有效果,没有任何更改。我还应用了自定义背景和分隔符,它工作正常,但不是这个 NSDictionary *normaltextAttr = @{[UIColor blackColor]: UITextAttributeTextColor, [UIColor clearColor]: UITextAttributeTextShadowColor,
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];
您使用了错误的键和值顺序,因此无法正常工作 试试这个
[[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];
注意工厂方法(值/键)之间的配对顺序差异 以及文字声明(键/值) 您只是使用了错误的键和值顺序 这将有助于:
NSDictionary *normaltextAttr =
@{UITextAttributeTextColor : [UIColor blackColor],
UITextAttributeTextShadowColor : [UIColor clearColor],
UITextAttributeFont : [UIFont fontWithName:_regularFont size:20.f]};
[[UISegmentedControl appearance] setTitleTextAttributes:normaltextAttr forState:UIControlStateNormal];
请注意,自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];
字典文字工作良好;如果他们不这样做,iOS中就会有一个严重的bug<代码>[self-setTitleTextAttributes:@{UITextAttributeTextColor:[UIColor redColor]}用于状态:UIControlStateNormal]代码>如果答案能指出问题所在,而不是仅仅发布一些有用的东西,那就太好了。
NSDictionary *normaltextAttr =
@{UITextAttributeTextColor : [UIColor blackColor],
UITextAttributeTextShadowColor : [UIColor clearColor],
UITextAttributeFont : [UIFont fontWithName:_regularFont size:20.f]};
[[UISegmentedControl appearance] setTitleTextAttributes:normaltextAttr forState:UIControlStateNormal];
[[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];