我正在構建一個天氣應用程式。在這個應用程式中,我向用戶顯示溫度值。在這個值中,我想讓溫度值和度數符號大小不同。因為當度數符號與溫度值大小相同時,它會改變視圖的中心。
我嘗試這樣的 NSAttributedText 方式。
let attrString = NSMutableAttributedString(string: temperature.value,
attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 36)])
attrString.append(NSMutableAttributedString(string:"°",
attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18)]))
temperature = attrString
我也嘗試了度數符號的 Unicode 版本“\u{00B0}”。順便說一句沒關系。
結果是這樣的。

但是,我想要這樣的結果。

我怎樣才能達到這種外觀?提前致謝
uj5u.com熱心網友回復:
添加附加屬性 .baselineOffset: NSNumber(value: 18) 在 temperature.value 中添加“°”的位置
let attrString = NSMutableAttributedString(string: temperature.value,
attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 36)])
attrString.append(NSMutableAttributedString(string:"°",
attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18),.baselineOffset: NSNumber(value: 18)]))
temperature = attrString
uj5u.com熱心網友回復:
一種解決方案是使用.baselineOffset密鑰。將此添加到度數符號的屬性字典中:
.baselineOffset: 16
調整數字以獲得您想要的確切位置。數字越大,度數符號就越高。
如果您希望字符看起來更靠近,您可能還想嘗試使用.kern屬性鍵(使用負值)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/529560.html
