最新消息:20181230 VPS服务器已从Linode换到腾讯云香港,主题仍用朋友推荐的大前端D8

[已解决]UISegmentControl中设置setTitleTextAttributes没效果

iOS crifan 742浏览 0评论

折腾:

[已解决]swift实现左右切换的分段可点击的按钮选项

期间,用代码:

    let actionList:[String] = ["登录", "注册"]

    var switchActionSegment:UISegmentedControl

        self.switchActionSegment = UISegmentedControl(items: actionList)

        self.switchActionSegment.backgroundColor = AppMainColor

        self.switchActionSegment.setTitleTextAttributes(["NSFontAttributeName" : UIFont.systemFontOfSize(20), "NSForegroundColorAttributeName" : UIColor.whiteColor()], forState: UIControlState.Normal)

但是文字没有生效:

文字还是很小,颜色也不是白色

uisegmentedcontrol setTitleTextAttributes not work

ios – UISegmentedControl setTitleTextAttributes does not work – Stack Overflow

换成:

UISegmentedControl.appearance().setTitleTextAttributes(["NSFontAttributeName" : UIFont.systemFontOfSize(20), "NSForegroundColorAttributeName" : UIColor.whiteColor()], forState: UIControlState.Normal)

没用。

ios – UISegmentedControl setTitleTextAttributes:forState: not working well with font – Stack Overflow

UITextAttributes font not working for UISegmentedControl selected state – Xamarin Forums

uisegmentedcontrol settitletextattributes not working

uisegmentedcontrol settitletextattributes

How can I change the text color of a segmented control in Swift?

iphone – How to change font color of UISegmentedControl – Stack Overflow

ios – UISegmentedControl only changes text color when revisiting ViewController – Stack Overflow

最终才发现,是自己写错了,把:

NSFontAttributeName

写成了:

“NSFontAttributeName” 

所以改为:

        self.switchActionSegment.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(20), NSForegroundColorAttributeName : UIColor.whiteColor()], forState: UIControlState.Normal)

        self.switchActionSegment.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.redColor()], forState: UIControlState.Selected)

即可生效:

转载请注明:在路上 » [已解决]UISegmentControl中设置setTitleTextAttributes没效果

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
61 queries in 0.124 seconds, using 9.97MB memory