我想在屬性字串中的前兩行之間應用間距,第三行應該看起來像段落。
預期輸出:
預期輸出截圖
目前的實施:
當前實作截圖
這是我嘗試的代碼。
let myString = "Your account phone numbers are listed here.\nTo change or delete a phone number, tap on it.\nTo add a phone number, go to the top right-hand corner of your screen and tap on “Add”.";
let font = UIFont.systemFont(ofSize: 14)
let attributedString = NSMutableAttributedString(string: myString, attributes: [.font: font])
self.displayLabel.attributedText = attributedString
我創建了標簽并將行數設定為 0,因此它將顯示多行文本。
在標簽中需要在前兩行顯示空格,如預期的輸出螢屏截圖所示。
如何僅將間距應用于前兩行,而第三行應顯示為預期輸出螢屏截圖所示?
uj5u.com熱心網友回復:
您似乎想設定段落之間的間距。這是由 控制的NSParagraphStyle.paragraphSpacing。只需將.paragraphStyle屬性字串的屬性設定為 an NSParagraphStyle:
let paraStyle = NSMutableParagraphStyle()
paraStyle.paragraphSpacing = 10 // or some other number
let attributedString = NSMutableAttributedString(string: myString,
attributes: [
.font: font,
.paragraphStyle: paraStyle
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/329182.html
