我正在做一个项目,将文本属性应用于UIlabel文本。我使用常见的NSAttributedString键来基本上应用strokeColor和strokeWidth以及foregroundColor来生成所需的轮廓效果。这个问题出现在非拉丁字符上,比如阿拉伯语,其中的字母是单独高亮的,而不是整个作品。阿拉伯语中的字母通常是连接在一起的,而英语中的字母是有间距的。我附上我正在处理的例子,并附上问题的截图和所需的结果。我将感谢您的支持和建议。
let quote = "الكتابة على الصور بخطوط جميلة"
//let font = UIFont.systemFon.selft(ofSize: 50)
let attributes: [NSAttributedString.Key: Any] = [
.strokeColor: UIColor.green,
.strokeWidth: -3.0,
.foregroundColor: UIColor.red,
.font: UIFont(name: "Georgia-Bold", size: 40)!,
]
let attributedQuote = NSAttributedString(string: quote, attributes: attributes)
TextLabel.attributedText = attributedQuote
结果与问题。
期望的结果:
快速看一下这个--并与css的类似输出进行比较--我认为你需要在彼此之上使用两个标签层。
试试这个。
let backLabel = UILabel()
let frontLabel = UILabel()
backLabel.translatesAutoresizingMaskIntoConstraints = false
frontLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(backLabel)
view.addSubview(frontLabel)
NSLayoutConstraint.activate([
backLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 100.0),
backLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20.0),
backLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20.0),
frontLabel.topAnchor.constraint(equalTo: backLabel.topAnchor),
frontLabel.leadingAnchor.constraint(equalTo: backLabel.leadingAnchor),
frontLabel.trailingAnchor.constraint(equalTo: backLabel.trailingAnchor),
])
let quote = "الكتابة على الصور بخطوط جميلة"
var attributes: [NSAttributedString.Key: Any] = [
.strokeColor: UIColor.green,
.strokeWidth: 12.0,
.foregroundColor: UIColor.red,
.font: UIFont(name: "Georgia-Bold", size: 40)!,
]
var attributedQuote = NSAttributedString(string: quote, attributes: attributes)
backLabel.attributedText = attributedQuote
attributes = [
.strokeColor: UIColor.green,
.strokeWidth: 0,
.foregroundColor: UIColor.red,
.font: UIFont(name: "Georgia-Bold", size: 40)!,
]
attributedQuote = NSAttributedString(string: quote, attributes: attributes)
frontLabel.attributedText = attributedQuote
结果: