gpt4 book ai didi

ios - 如何更改 NSAttributedString 中的 "\t"长度?

转载 作者:行者123 更新时间:2023-11-28 09:51:08 26 4
gpt4 key购买 nike

我想使用属性字符串更改 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])

关于ios - 如何更改 NSAttributedString 中的 "\t"长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47772056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com