使用 NSAttributedString
和 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:另请参见 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)
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 4 版本:
let attributes: [NSAttributedStringKey : Any] = [.strokeWidth: -3.0,
.strokeColor: UIColor.yellow,
.foregroundColor: UIColor.red]
label.attributedText = NSAttributedString(string: text, 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)
谢谢你
这是UITextView中描边、倾斜/斜体文本的示例
public func setAttributedText(fontStyle: INSTextFontPickerStyle, strokeColor: UIColor = UIColor.white, foregroundColor: UIColor = UIColor.black, isObliqueness: Bool = false, isStrokes: Bool = false) {
self.fontStyle = fontStyle
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = innterTextView.textAlignment
// Tilting the text
let obliquenessValue = isObliqueness ? NSNumber(value: 0.2) : NSNumber(value: 0) // The angle of tilting in clockwise
// Stroke a line in text edges
var strokeWidth = NSNumber(value: 0.0)
var _strokeColor = UIColor.clear
if isStrokes || fontStyle == .strokeEdges {
strokeWidth = NSNumber(value: -2.0) // The width of stroke
_strokeColor = strokeColor
}
textView.attributedText = NSAttributedString(string: textView.text, attributes: [
NSAttributedString.Key.foregroundColor : foregroundColor,
NSAttributedString.Key.font : UIFont.systemFont(ofSize: (actualFontSize > 0 ? actualFontSize : fontSize)),
NSAttributedString.Key.paragraphStyle : paragraphStyle,
NSAttributedString.Key.strokeColor : _strokeColor,
NSAttributedString.Key.strokeWidth : strokeWidth,
NSAttributedString.Key.obliqueness: obliquenessValue
])
}