Swift中的外观代理(iOS)

5

有人尝试在Swift中使用外观代理吗?

这个语法不起作用,有没有人找出如何在像segmentedControl或UITabBar这样的控件上设置标题文本属性?我认为我离成功很近了。

segmentedControl.titleTextAttributesForState(UIControlState.Normal) = 
NSDictionary(objects: [UIFont(name: fontFamilyRegular, size: 16.0)], 
forKeys: [NSFontAttributeName])
3个回答

10
这应该就可以了:
segmentedControl.setTitleTextAttributes([
    NSFontAttributeName: UIFont(name: "Helvetica", size: 16.0)!,
    NSForegroundColorAttributeName: UIColor.blueColor()
    ], forState: UIControlState.Normal)

1
太棒了!这就是我想要的。谢谢。 - DBoyer
你如何使用这个语法设置多个属性? - colindunn

3
请确保您将字体解压缩(!)
let font = UIFont(name: "HelveticaNeue-Light", size:15.0)!
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName:font,NSForegroundColorAttributeName:UIColor.redColor()], forState: UIControlState.Normal)

大家好,我刚刚安装了XCode 6.3.1和iPhone 4s iOS 8.3,一切正常。也许是自定义颜色的问题?我只是在代码中将其更改为红色,这样人们就可以复制和粘贴了。如果您还没有解决问题,请告诉我。 - Adriano Spadoni

1
对于XCode 6.1,请尝试以下操作:

UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object: UIFont(name: "Helvetica", size: 16.0)!, forKey: NSFontAttributeName), forState: UIControlState.Normal)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接