在 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
中的图像上。显示的文本:
最佳答案
来自 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/