gpt4 book ai didi

ios - NSAttributedString 项目符号列表问题

转载 作者:搜寻专家 更新时间:2023-10-31 22:18:42 24 4
gpt4 key购买 nike

我正在尝试使用 NSAttributedStringUITextView 创建项目符号列表。而且,这是我到目前为止能够实现的目标:enter image description here

如您所见,两条线之间有一个小的“偏移”。这是我用来构建属性字符串的代码片段:

func add(bulletList strings: [String],
indentation: CGFloat = 15,
lineSpacing: CGFloat = 3,
paragraphSpacing: CGFloat = 10) {

func createParagraphAttirbute() -> NSParagraphStyle {
var paragraphStyle: NSMutableParagraphStyle
let nonOptions = NSDictionary() as! [NSTextTab.OptionKey: Any]

paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.tabStops = [
NSTextTab(textAlignment: .left, location: indentation, options: nonOptions)]
paragraphStyle.defaultTabInterval = indentation
paragraphStyle.firstLineHeadIndent = 0
paragraphStyle.lineSpacing = lineSpacing
paragraphStyle.paragraphSpacing = paragraphSpacing
paragraphStyle.headIndent = indentation
return paragraphStyle
}

var buffer = NSMutableAttributedString.init()

for string in strings {
let formattedString = "\u{2022} \(string)\n"
let attributedString = NSMutableAttributedString(string: formattedString)
let paragraphStyle = createParagraphAttirbute()

attributedString.addAttributes(
[NSAttributedStringKey.paragraphStyle : paragraphStyle],
range: NSMakeRange(0, attributedString.length))

attributedString.addAttributes(
textAttributes,
range: NSMakeRange(0, attributedString.length))

let string:NSString = NSString(string: formattedString)
let rangeForBullet:NSRange = string.range(of: bulletPoint)
attributedString.addAttributes(bulletAttirbutes, range: rangeForBullet)
buffer.append(attributedString)
}
}

您认为所选段落参数有问题吗?因为代码几乎完成了预期的工作,排除了这个差距。

更新1

按照@the4kman 的建议,我更改了提供的代码,如下所示:

paragraphStyle.firstLineHeadIndent = indentation

但现在我所有的行都相互对齐了,包括要点:

enter image description here

更新2

好的,解决方案非常简单——用制表符替换空格。请参阅下面的更新代码。

最佳答案

@the4kman,@Krunal,感谢您的回复!解决方案甚至更简单。在 let formattedString = "\u{2022}\(string)\n 中用 \t 替换空格符号给你有效的缩进。

为了完整起见,完整的解决方案代码是(只替换一个字符):

func add(bulletList strings: [String],
font: UIFont,
indentation: CGFloat = 15,
lineSpacing: CGFloat = 3,
paragraphSpacing: CGFloat = 10,
textColor: UIColor = .black,
bulletColor: UIColor = .red) -> NSAttributedString {

func createParagraphAttirbute() -> NSParagraphStyle {
var paragraphStyle: NSMutableParagraphStyle
let nonOptions = NSDictionary() as! [NSTextTab.OptionKey: Any]

paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.tabStops = [
NSTextTab(textAlignment: .left, location: indentation, options: nonOptions)]
paragraphStyle.defaultTabInterval = indentation
paragraphStyle.firstLineHeadIndent = 0
paragraphStyle.lineSpacing = lineSpacing
paragraphStyle.paragraphSpacing = paragraphSpacing
paragraphStyle.headIndent = indentation
return paragraphStyle
}

let bulletPoint = "\u{2022}"
let textAttributes: [NSAttributedStringKey: Any] = [.font: font, .foregroundColor: textColor]
let bulletAttributes: [NSAttributedStringKey: Any] = [.font: font, .foregroundColor: bulletColor]
let buffer = NSMutableAttributedString.init()

for string in strings {
let formattedString = "\(bulletPoint)\t\(string)\n"
let attributedString = NSMutableAttributedString(string: formattedString)
let paragraphStyle = createParagraphAttirbute()

attributedString.addAttributes(
[NSAttributedStringKey.paragraphStyle : paragraphStyle],
range: NSMakeRange(0, attributedString.length))

attributedString.addAttributes(
textAttributes,
range: NSMakeRange(0, attributedString.length))

let string:NSString = NSString(string: formattedString)
let rangeForBullet:NSRange = string.range(of: bulletPoint)
attributedString.addAttributes(bulletAttributes, range: rangeForBullet)
buffer.append(attributedString)
}

return buffer
}

关于ios - NSAttributedString 项目符号列表问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46888961/

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