如何更改 NSAttributedString 中的“\t”长度?
How to change "\t" length in NSAttributedString?
我想使用属性字符串更改 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)
您可以尝试将 \t 替换为您想要的 space 个数
var text = "test\ttest"
text = text.replacingOccurrences(of: "\t", with: " ")
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])
要通过 NSMutableParagraphStyle
更改制表位的长度,您必须创建一个新的 NSTextTab
实例数组并将其分配给 tabStops
数组
let text = "test\ttest\ttest"
let attributedText = NSMutableAttributedString(string: text)
let paragraphStyle = NSMutableParagraphStyle()
let tabInterval : CGFloat = 40.0
var tabs = [NSTextTab]()
for i in 1...10 { tabs.append(NSTextTab(textAlignment: .left, location: tabInterval * CGFloat(i))) }
paragraphStyle.tabStops = tabs
let textRange = NSRange(location: 0, length: text.count)
attributedText.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: textRange)
我想使用属性字符串更改 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)
您可以尝试将 \t 替换为您想要的 space 个数
var text = "test\ttest"
text = text.replacingOccurrences(of: "\t", with: " ")
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])
要通过 NSMutableParagraphStyle
更改制表位的长度,您必须创建一个新的 NSTextTab
实例数组并将其分配给 tabStops
数组
let text = "test\ttest\ttest"
let attributedText = NSMutableAttributedString(string: text)
let paragraphStyle = NSMutableParagraphStyle()
let tabInterval : CGFloat = 40.0
var tabs = [NSTextTab]()
for i in 1...10 { tabs.append(NSTextTab(textAlignment: .left, location: tabInterval * CGFloat(i))) }
paragraphStyle.tabStops = tabs
let textRange = NSRange(location: 0, length: text.count)
attributedText.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: textRange)