Swift中的项目符号列表-iOS

sur*_*heW 3 nsattributedstring ios swift

我需要在iOS应用程序中将项目符号文本添加到textView中。我在看这个链接这一个,并跟随他们的想法。这是我的代码:

let paragraph = NSMutableParagraphStyle()
paragraph.firstLineHeadIndent = 15
paragraph.headIndent = 15

attributes = [
    NSAttributedStringKey.paragraphStyle: paragraph
]

attributedString = NSAttributedString(string: "\u{2022} Some text some text some text some text some text some text", attributes: attributes)
finalText.append(attributedString)
Run Code Online (Sandbox Code Playgroud)

我需要的是使文本缩进上面的文本的开头。就像图片中的一样:

在此处输入图片说明

我得到的是带有项目符号起点的缩进文本。

在此处输入图片说明

Jac*_*ack 5

我使用自定义缩进时遇到了同样的问题textView,它工作正常-

 @IBOutlet  var bulletTextView: UITextView!

 override func viewDidLoad() {
        let bullet1 = "This is a small string,This is a small string,This is a small string,This is a small string,This is a small string,This is a small string,This is a small string"
        let bullet2 = "This is more of medium string with a few more words etc."
        let bullet3 = "Well this is certainly a longer string, with many more words than either of the previuos two strings"
        strings = [bullet1, bullet2, bullet3]
        let fullAttributedString = NSMutableAttributedString()
        for string: String in strings {
            let attributesDictionary:[NSAttributedStringKey:Any] = [NSAttributedStringKey.font : bulletTextView.font,NSAttributedStringKey.foregroundColor : UIColor.red]
            let bulletPoint: String = "\u{2022}"
            //let formattedString: String = "\(bulletPoint) \(string)\n"
            let attributedString = NSMutableAttributedString(string: bulletPoint, attributes: attributesDictionary)
            attributedString.append(NSAttributedString(string: " \(string) \n"))
            let indent:CGFloat = 15
            let paragraphStyle = createParagraphAttribute(tabStopLocation: indent, defaultTabInterval: indent, firstLineHeadIndent: indent - 10, headIndent: indent)
            attributedString.addAttributes([NSAttributedStringKey.paragraphStyle: paragraphStyle], range: NSMakeRange(0, attributedString.length))
            fullAttributedString.append(attributedString)
        }
        bulletTextView.attributedText = fullAttributedString
    }

    func createParagraphAttribute(tabStopLocation:CGFloat, defaultTabInterval:CGFloat, firstLineHeadIndent:CGFloat, headIndent:CGFloat) -> NSParagraphStyle {
        let paragraphStyle: NSMutableParagraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
        let options:[NSTextTab.OptionKey:Any] = [:]
        paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: tabStopLocation, options: options)]
        paragraphStyle.defaultTabInterval = defaultTabInterval
        paragraphStyle.firstLineHeadIndent = firstLineHeadIndent
        paragraphStyle.headIndent = headIndent
        return paragraphStyle
    }
Run Code Online (Sandbox Code Playgroud)

输出:-

在此输入图像描述


Tom*_*m E 5

设置paragraph.firstLineHeadIndent为零。这只会缩进从第二行开始的行。目前,您正在缩进所有行\xe2\x80\xa6

\n\n
let paragraph = NSMutableParagraphStyle()\n// paragraph.firstLineHeadIndent = 15\nparagraph.headIndent = 15\n
Run Code Online (Sandbox Code Playgroud)\n


Mah*_*a Y 5

从代码中删除para 段.firstLineHeadIndent = 15 ...

let paragraph = NSMutableParagraphStyle()
paragraph.headIndent = 15

attributes = [
    NSAttributedStringKey.paragraphStyle: paragraph
]

attributedString = NSAttributedString(string: "\u{2022} Some text some text some text some text some text some text", attributes: attributes)
finalText.append(attributedString)
Run Code Online (Sandbox Code Playgroud)

请参考我的示例代码屏幕截图

let style = NSMutableParagraphStyle()
        style.alignment = .left
        style.headIndent = 20

        let title = NSMutableAttributedString(string: "\u{2022} I need to add bulleted text to textView in iOS app. I am looking at this link and this one and following their ideas. This is my code:", attributes: [NSAttributedStringKey.paragraphStyle: style,NSAttributedStringKey.foregroundColor:UIColor.blue])

        let titleStr = NSMutableAttributedString(string: "\n\n\u{2022} I need to add bulleted text to textView in iOS app. I am looking at this link and this one and following their ideas. This is my code:", attributes: [NSAttributedStringKey.paragraphStyle: style,NSAttributedStringKey.foregroundColor:UIColor.blue])
        title.append(titleStr)
        titleLabel.attributedText = title
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Mat*_*son 5

要使用动态字体调整 headIndent 大小,我正在使用以下命令:

\n\n
private func updateUI() {\n    let bullet: NSString = "\xe2\x80\xa2 "\n    var attributes = [NSAttributedString.Key: Any]()\n    let paragraph = NSMutableParagraphStyle()\n    leStackView.subviews.compactMap({ $0 as? UILabel }).forEach {\n        attributes[.font] = $0.font\n        paragraph.headIndent = bullet.size(withAttributes: attributes).width\n        attributes[.paragraphStyle] = paragraph\n        let text = $0.text ?? ""\n        $0.attributedText = NSAttributedString(string: text, attributes: attributes)\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

每个项目符号点的标签均在故事板中使用纯文本(包括项目符号)和动态字体设置。

\n\n

我非常感谢对此线程的贡献以及https://bendodson.com/weblog/2018/08/09/bulleted-lists-with-uilabel/

\n