UILabel
属性付き文字列を使用して、タブのデフォルトの幅を変更したい。どうすればそれを達成できますか?属性を追加する必要NSMutableParagraphStyle
があると思いますが、タブの長さの原因となるプロパティがわかりません。
たとえば、このコードを使用してみましょう。
let text = "test\ttest"
let attributedText = NSMutableAttributedString(string: text)
let paragraphStyle = NSMutableParagraphStyle()
let textRange = NSRange(location: 0, length: text.length)
attributedText.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: textRange)
Apple Developer Documentationによると、var tabStops: [NSTextTab]!
はNSTextTab
レシーバーのタブストップを表すオブジェクトの配列です。次のように、タブにアクセスして場所を変更できます。
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: newTabLength, options: [:])]
label.attributedText = NSAttributedString(string: text, attributes: [NSParagraphStyleAttributeName: paragraphStyle])
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加