我怎么能中风和填充NSAttributedString W / UILabel
是否有可能应用中风和填充 NSAttributedString
和UILabel
?
是的,关键是将一个负值应用到NSStrokeWidthAttributeName
如果这个值是肯定的,你只会看到笔画,而不是填充。
Objective-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
Swift版本:
let attributes = [NSStrokeWidthAttributeName: -3.0, NSStrokeColorAttributeName: UIColor.yellowColor(), NSForegroundColorAttributeName: UIColor.redColor()]; label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes)
Swift 3版本:
let attributes = [NSStrokeWidthAttributeName: -3.0, NSStrokeColorAttributeName: UIColor.yellow, NSForegroundColorAttributeName: UIColor.red] as [String : Any] label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes)
Swift版本:
let attributes = [NSStrokeWidthAttributeName: -3.0, NSStrokeColorAttributeName: UIColor.yellowColor(), NSForegroundColorAttributeName: UIColor.redColor()]; label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes)
Swift 3版本:
let attributes = [NSStrokeWidthAttributeName: -3.0, NSStrokeColorAttributeName: UIColor.yellow, NSForegroundColorAttributeName: UIColor.red] as [String : Any] label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes)
现在最新在swift苹果中删除[String:Any]属性键值声明用作[NSAttributedStringKey:Any]
let strokeTextAttributes = [ NSAttributedStringKey.strokeColor : UIColor.green, NSAttributedStringKey.foregroundColor : UIColor.lightGray, NSAttributedStringKey.strokeWidth : -4.0, NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 52) ] as [NSAttributedStringKey : Any] hello_cell_lb.attributedText = NSAttributedString(string: "\(hello_array[indexPath.row])", attributes: strokeTextAttributes)
谢谢