我怎样才能用带有 UILabel 的 NSAttributedString 描边和填充 [英] How can I both stroke and fill with NSAttributedString w/ UILabel
本文介绍了我怎样才能用带有 UILabel 的 NSAttributedString 描边和填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用 NSAttributedString
和 UILabel
同时应用 stroke 和 fill?
Is it possible to apply both stroke and fill with an NSAttributedString
and a UILabel
?
推荐答案
是的,关键是给NSStrokeWidthAttributeName
应用一个Negative值如果此值为正数,您将只会看到笔触,而不会看到填充.
Yes, the key is to apply a Negative value to the NSStrokeWidthAttributeName
If this value is positive you will only see the stroke and not the fill.
目标-C:
self.label.attributedText=[[NSAttributedString alloc]
initWithString:@"string to both stroke and fill"
attributes:@{
NSStrokeWidthAttributeName: @-3.0,
NSStrokeColorAttributeName:[UIColor yellowColor],
NSForegroundColorAttributeName:[UIColor redColor]
}
];
感谢下面的@cacau:另请参阅技术问答 QA1531
Thanks to @cacau below: See also Technical Q&A QA1531
Swift 4 版本:
let attributes: [NSAttributedStringKey : Any] = [.strokeWidth: -3.0,
.strokeColor: UIColor.yellow,
.foregroundColor: UIColor.red]
label.attributedText = NSAttributedString(string: text, attributes: attributes)
Swift 5.1 版本:
let attributes: [NSAttributedString.Key : Any] = [.strokeWidth: -3.0,
.strokeColor: UIColor.yellow,
.foregroundColor: UIColor.red]
label.attributedText = NSAttributedString(string: text, attributes: attributes)
这篇关于我怎样才能用带有 UILabel 的 NSAttributedString 描边和填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文