我怎么能中风和填充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)
谢谢
- 导航控制器和视图控制器之间的区别?
- 在Xcode 4.2中启用崩溃日志符号
- 为什么在尝试loginBox时遇到redirectURI不匹配?
- 无法呈现多边形(无法保留指示:XX):featureID:X key:XXXX
- 计算中的小数点。 要么 ,
- 无法用XCode / Obj-C编译Cocoapods – “Pods-prefix.pch.dia:没有这样的文件或目录”
- 如何比较两个NSMutableArray?
- iphone sdk CGAffineTransform获取对象的旋转angular度
- dataWithContentsOfURL返回NSCocoaErrorDomain代码= 256蜂窝,但不是WiFi