[英]Set highlighted text color for UILabel using NSAttributedString.?
[英]How to set NSAttributedString text color?
我試圖將一些屬性設置為 NSAttributedString。 除了前景色外,一切正常。
這就是我嘗試設置屬性的方式:
func setLabelText(text:String){
let strokeTextAttributes = [
NSAttributedString.Key.strokeColor : UIColor.white,
NSAttributedString.Key.foregroundColor : UIColor.red,
NSAttributedString.Key.font : UIFont.init(name: "Raleway-ExtraBold", size: 26)!,
NSAttributedString.Key.strokeWidth : 0.5,
]
as [NSAttributedString.Key : Any]
label.attributedText = NSMutableAttributedString(string: text, attributes: strokeTextAttributes)
}
正如您在圖像中看到的那樣,它沒有設置文本顏色:
你知道為什么它會忽略 foregroundColor 屬性嗎?
提前致謝。
您的問題出在strokeWidth
屬性上,因為您使用的是正數,只有筆划受到影響。 您需要使用負數來更改筆划並填充文本,如strokeWidth屬性的文檔中所述:
指定正值以單獨更改筆划寬度。 指定負值來描邊和填充文本。
let strokeTextAttributes: [NSAttributedString.Key : Any] = [
.strokeColor : UIColor.white,
.foregroundColor : UIColor.red,
.font : UIFont.init(name: "Raleway-ExtraBold", size: 26)!,
.strokeWidth : -0.5,
]
在我看來,最好指定列表的數據類型,而不是將列表轉換為該類型的特定類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.