gpt4 book ai didi

string - Swift 字符串填充不相等?

转载 作者:行者123 更新时间:2023-11-30 13:38:37 31 4
gpt4 key购买 nike

(快速)

当使用String.stringByPaddingToLength时,它会正确填充到设定的字符数,但是由于某些字符比其他字符更大,不同的行具有不同的填充位置。

发生了什么......(不等间距)

SomeValueName:..................SomeValue  (40 characters wide)

SomeDifferentValue..........SomeValue (40 characters wide)

我需要什么(等间距)

someValueName:..............SomeValue (40 characters wide)

someDifferentValue...........SomeValue (40 characters wide)

我不能使用stringByPaddingToLength来获得这个结果吗?似乎它只是担心字符的数量,而不是实际的最终位置。

最佳答案

使用制表符对齐字符串:

let paragraphStyle = NSMutableParagraphStyle()
let terminator = NSTextTab.columnTerminatorsForLocale(NSLocale.currentLocale())
// Adjust the location as needed for your text
let tab = NSTextTab(textAlignment: .Right, location: 72.0 * 2, options: [NSTabColumnTerminatorsAttributeName: terminator])
paragraphStyle.tabStops = [tab]

let attributedString = NSMutableAttributedString(string: "Apple:\t$1.50\nOrange:\t$1", attributes: [NSParagraphStyleAttributeName: paragraphStyle])
self.textView.attributedText = attributedString

打印:

Apple:         $1.5
Orange: $1

它们在小数点处对齐。我还没有找到如何添加前导点。如果有人这样做,请随时编辑我的答案。

关于string - Swift 字符串填充不相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35821893/

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