Swift 中的项目符号列表 - iOS

Posted

技术标签:

【中文标题】Swift 中的项目符号列表 - iOS【英文标题】:Bulleted list in Swift - iOS 【发布时间】:2018-07-18 12:23:49 【问题描述】:

我需要在 ios 应用程序的 textView 中添加项目符号文本。我正在查看this link 和this one 并遵循他们的想法。这是我的代码:

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

attributes = [
    NSAttributedStringKey.paragraphStyle: paragraph
]

attributedString = NSAttributedString(string: "\u2022 Some text some text some text some text some text some text", attributes: attributes)
finalText.append(attributedString)

我需要的是让文本以上面 text 的开头缩进。和图片一样:

我得到的是与 bullet 的起点缩进的文本。

【问题讨论】:

你可能想要使用段落的 indention 来达到你想要的效果,所以你可以开始例如这里:developer.apple.com/library/archive/documentation/Cocoa/…,然后可以继续,例如这里:developer.apple.com/documentation/uikit/nsparagraphstyle,在这两个之后,整个概念应该在你面前。 在 textView 中使用 html 段落并启用属性文本。 【参考方案1】:

从代码中删除 paragraph.firstLineHeadIndent = 15...

let paragraph = NSMutableParagraphStyle()
paragraph.headIndent = 15

attributes = [
    NSAttributedStringKey.paragraphStyle: paragraph
]

attributedString = NSAttributedString(string: "\u2022 Some text some text some text some text some text some text", attributes: attributes)
finalText.append(attributedString)

请参考我的示例代码截图

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

        let title = NSMutableAttributedString(string: "\u2022 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\u2022 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

【讨论】:

【参考方案2】:

我在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 = "\u2022"
            //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
    

输出:-

【讨论】:

【参考方案3】:

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

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

【讨论】:

简单的方法。 你是对的,谢谢。尽管如此,我仍然没有得到所需的缩进。现在将 firstLineHeadIndent 设置为 5 就可以了。 firstLineHeadIndent = 0 是您链接的示例中使用的内容。【参考方案4】:

要使用动态字体调整 headIndent 的大小,我正在使用这个:

private func updateUI() 
    let bullet: NSString = "• "
    var attributes = [NSAttributedString.Key: Any]()
    let paragraph = NSMutableParagraphStyle()
    leStackView.subviews.compactMap( $0 as? UILabel ).forEach 
        attributes[.font] = $0.font
        paragraph.headIndent = bullet.size(withAttributes: attributes).width
        attributes[.paragraphStyle] = paragraph
        let text = $0.text ?? ""
        $0.attributedText = NSAttributedString(string: text, attributes: attributes)
    

每个项目符号点的标签在情节提要中设置为纯文本(包括项目符号)和动态字体。

我非常感谢对这个帖子的贡献以及https://bendodson.com/weblog/2018/08/09/bulleted-lists-with-uilabel/

【讨论】:

【参考方案5】:

简单的解决方案:

extension Sequence where Self.Element == String 

  func toBulletList(_ bulletIndicator: String = "•",
                    itemSeparator: String = "\n",
                    spaceCount: Int = 2) -> String 
    let bullet = bulletIndicator + String(repeating: " ", count: spaceCount)
    let list = self
      .map  bullet + $0 
      .reduce("",  $0 + ($0.isEmpty ? $0 : itemSeparator) + $1 )
    return list
  

用法:

let items: [String] = [
"one",
"two",
"three"
]

let list = items.toBulletList()
po list -> 
    •  one
    •  two
    •  three

【讨论】:

【参考方案6】:

我遇到了同样的问题,我终于意识到Label 不支持它。如果你想在同一行中使用子弹列表,你应该使用text view

【讨论】:

以上是关于Swift 中的项目符号列表 - iOS的主要内容,如果未能解决你的问题,请参考以下文章

可以在 Obj C 中链接,找不到符号,也不会在 Swift 中链接

如何从列表中的项目中删除标点符号并将其另存为列表中的单独项目?

在 swift4 中使用文件管理器保存

iOS Swift如何根据下拉列表中的选择显示动态用户界面

Markdown 中的多行项目符号列表

未定义符号:Swift.String.unicodeScalars.getter:XCode 13 中的 Swift.String.UnicodeScalarView