iphone - NSForegroundColorAttributeName 在 Swift 中不起作用?

标签 iphone swift ios8

viewDidLoad 中,我有类似下面的内容来将文本属性添加到 UITextField:

let textAttributes = [
    NSForegroundColorAttributeName: UIColor.whiteColor(),
    NSStrokeColorAttributeName: UIColor.blackColor(),
    NSFontAttributeName: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
    NSStrokeWidthAttributeName: 1.0
]

self.textField.delegate = self
self.textField.defaultTextAttributes = textAttributes
self.textField.text = "Top text field"

除了 NSForegroundColorAttributeName 之外,所有这些属性似乎都可以正常工作。此文本显示透明。这是 Swift 的错误吗?

文本被放置在 UIScrollView 中的图像上。显示的文本:

Screen Shot

最佳答案

来自 Technical Q&A QA1531 :

This is because the sign of the value for NSStrokeWidthAttributeName is interpreted as a mode; it indicates whether the attributed string is to be filled, stroked, or both. Specifically, a zero value displays a fill only, while a positive value displays a stroke only. A negative value allows displaying both a fill and stroke.

根据你的设置

NSStrokeWidthAttributeName: 1.0

字体仅被描边而不被填充,从而产生“轮廓字体”。你会想要设置

NSStrokeWidthAttributeName: -1.0

而是让字体被描边填充。

如果您按住命令并单击,您也可以找到该信息 Xcode中的NSStrokeWidthAttributeName跳转定义:

NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)

关于iphone - NSForegroundColorAttributeName 在 Swift 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30955277/

相关文章:

ios - 自动布局在设置 preferredMaxLayoutWidth 时在 UILabel iPhone 6/6+ 的顶部和底部添加额外的填充

ios - 创建全局变量的最佳实践

ios - 当新的 iOS 应用程序安装 iphone 时收到通知

c# - 是否可以在 MonoTouch 中对 UILabel 的文本使用 getter/setter?

xcode - Cocoapods 没有这样的模块 'Siesta'

ios - 自动调整 UITableViewCell 变小

ios - Swift 中 UIImageWriteToSavedPhotosAlbum 后图像的 URL

iphone - ccTouchMoved 反复触发声音 & ccTouchEnded 不起作用

ios - SCNNode : Random color for each individial node, 不是全部吗? - swift ,SceneKit

swift - 使用照片框架删除相机胶卷 Assets