用子弹点格式化UILabel?

Rob*_*Rob 85 objective-c bullet uilabel ios swift

是否可以格式化texta UILabel以显示项目符号

如果是这样,我该怎么办?

Chr*_*ble 154

也许在字符串中使用Unicode代码点作为项目符号?

Objective-C的

myLabel.text = @"\u2022 This is a list item!";
Run Code Online (Sandbox Code Playgroud)

斯威夫特4

myLabel.text = "\u{2022} This is a list item!"
Run Code Online (Sandbox Code Playgroud)

  • 原谅我的无知,但我一直使用UILabel,我想知道你是否可以指出"例如". (4认同)
  • 另一种方法是使用`option + 8` (4认同)
  • 如果使用可本地化的字符串,请记住使用大写'u':\ U2022 (3认同)

Zac*_*c24 78

加上 " • "

即使我正在为我寻找这样的东西textView.我做了什么,只是在我的字符串上面附加字符串并将其传递给我textView,同样可以做到labels.

我为将来的观众回答了这个问题


Kru*_*nal 35

这是Swift很好的解决方案

let label = UILabel()
label.frame = CGRect(x: 40, y: 100, width: 280, height: 600)
label.textColor = UIColor.lightGray
label.numberOfLines = 0

let arrayString = [
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
    "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
    "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
    "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
]

label.attributedText = add(stringList: arrayString, font: label.font, bullet: "?")

self.view.addSubview(label)
Run Code Online (Sandbox Code Playgroud)

添加项目符号

func add(stringList: [String],
         font: UIFont,
         bullet: String = "\u{2022}",
         indentation: CGFloat = 20,
         lineSpacing: CGFloat = 2,
         paragraphSpacing: CGFloat = 12,
         textColor: UIColor = .gray,
         bulletColor: UIColor = .red) -> NSAttributedString {

    let textAttributes: [NSAttributedStringKey: Any] = [NSAttributedStringKey.font: font, NSAttributedStringKey.foregroundColor: textColor]
    let bulletAttributes: [NSAttributedStringKey: Any] = [NSAttributedStringKey.font: font, NSAttributedStringKey.foregroundColor: bulletColor]

    let paragraphStyle = NSMutableParagraphStyle()
    let nonOptions = [NSTextTab.OptionKey: Any]()
    paragraphStyle.tabStops = [
        NSTextTab(textAlignment: .left, location: indentation, options: nonOptions)]
    paragraphStyle.defaultTabInterval = indentation
    //paragraphStyle.firstLineHeadIndent = 0
    //paragraphStyle.headIndent = 20
    //paragraphStyle.tailIndent = 1
    paragraphStyle.lineSpacing = lineSpacing
    paragraphStyle.paragraphSpacing = paragraphSpacing
    paragraphStyle.headIndent = indentation

    let bulletList = NSMutableAttributedString()
    for string in stringList {
        let formattedString = "\(bullet)\t\(string)\n"
        let attributedString = NSMutableAttributedString(string: formattedString)

        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: bullet)
        attributedString.addAttributes(bulletAttributes, range: rangeForBullet)
        bulletList.append(attributedString)
    }

    return bulletList
}
Run Code Online (Sandbox Code Playgroud)

结果如下:

在此输入图像描述


Dax*_*gar 7

在swift 3.1中

lblItemName.text = "\u{2022} This is a list item!"
Run Code Online (Sandbox Code Playgroud)


Sco*_*des 7

是的。复制并粘贴以下项目符号:\xe2\x80\xa2Swift 的编译器可以在 Xcode 中根据需要解释和显示项目符号,无需其他任何操作。

\n\n

重复利用

\n\n
extension String {\n    static var bullet: String {\n        return "\xe2\x80\xa2 "\n    }\n}\n\n\nprint(String.bullet + "Buy apples")\nlet secondPoint: String = .bullet + "Buy oranges"\nprint(secondPoint)\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出

\n\n
\xe2\x80\xa2 Buy apples\n\xe2\x80\xa2 Buy oranges\n
Run Code Online (Sandbox Code Playgroud)\n\n

可重复使用的阵列

\n\n
extension Array where Element == String {\n\n    var bulletList: String {\n        var po = ""\n        for (index, item) in self.enumerated() {\n            if index != 0 {\n                po += "\\n"\n            }\n            po += .bullet + item\n        }\n        return po\n    }\n}\n\n\nprint(["get apples", "get oranges", "get a bannana"].bulletList)\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出

\n\n
\xe2\x80\xa2 get apples\n\xe2\x80\xa2 get oranges\n\xe2\x80\xa2 get a bannana\n
Run Code Online (Sandbox Code Playgroud)\n


Jac*_*ack 6

Swift 4中,我使用了"•"和新的Line

 @IBOutlet weak var bulletLabel: UILabel!
 let arrayOfLines = ["Eat egg for protein","You should Eat Ghee","Wheat is with high fiber","Avoid to eat Fish "]
 for value in arrayOfLines {
     bulletLabel.text = bulletLabel.text!  + " • " + value + "\n"
  }
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述

  • 为什么要避免吃鱼 (6认同)