![](/img/trans.png)
[英]HTML to NSAttributedString and NSAttributedString to HTML
[英]NSAttributedString with tabs
NSAttributedString可以創建帶制表位的文本列。 這與在具有相同限制的文字處理器中完成的方式類似。
let text = "Name\t: Johny\nGender\t: Male\nAge\t: 25\nFavourites\t: Reading, writing"
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = [NSTextTab(textAlignment: NSTextAlignment.Left, location: 150, options: [:])]
paragraphStyle.headIndent = 150
label.attributedText = NSAttributedString(string: text, attributes: [NSParagraphStyleAttributeName: paragraphStyle])
tabStops
提供每個選項卡后繼續文本的位置的點位置。 在這里,我們在第一列之后的合理點處做了一個選項卡。 headIndent
告訴標簽,包裝文本需要縮進固定數量,因此它將換行到下一行。
這種方法的局限性是:
headIndent
或者將“:”拆分為\\t:\\t
並設置第二個制表位。 如果你不讓文本換行,這不是問題。 如果這些限制過於嚴格,您可以將標簽重組為具有自動布局約束的多個標簽的集合。
在Swift 4.2或更高版本中
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = [NSTextTab.init(textAlignment: .left, location: 150, options: [:])]
paragraphStyle.headIndent = 150
let attributedTitle = NSAttributedString(string: "Some Title", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14.0), NSAttributedString.Key.paragraphStyle: paragraphStyle])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.