是否可以同时应用笔画和填充anNSAttributedString
和 a UILabel
?
问问题
20801 次
5 回答
92
是的,关键是将负值应用于NSStrokeWidthAttributeName
如果此值为正,您将只看到描边而不是填充。
目标-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
斯威夫特 4版本:
let attributes: [NSAttributedStringKey : Any] = [.strokeWidth: -3.0,
.strokeColor: UIColor.yellow,
.foregroundColor: UIColor.red]
label.attributedText = NSAttributedString(string: text, attributes: attributes)
斯威夫特 5.1版本:
let attributes: [NSAttributedString.Key : Any] = [.strokeWidth: -3.0,
.strokeColor: UIColor.yellow,
.foregroundColor: UIColor.red]
label.attributedText = NSAttributedString(string: text, attributes: attributes)
于 2013-04-16T22:01:22.860 回答
8
斯威夫特版本:
let attributes = [NSStrokeWidthAttributeName: -3.0,
NSStrokeColorAttributeName: UIColor.yellowColor(),
NSForegroundColorAttributeName: UIColor.redColor()];
label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes)
斯威夫特 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)
于 2016-04-26T16:10:49.880 回答
2
现在最新的 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)
谢谢你
于 2017-11-09T12:05:06.730 回答
1
斯威夫特 4版本:
let attributes: [NSAttributedStringKey : Any] = [.strokeWidth: -3.0,
.strokeColor: UIColor.yellow,
.foregroundColor: UIColor.red]
label.attributedText = NSAttributedString(string: text, attributes: attributes)
于 2018-06-11T00:17:36.067 回答
0
这是 UITextView 中的 Stroke 和 Oblique/tilt/slant 文本的示例
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
])
}
于 2020-12-22T12:32:36.147 回答