如何使用圆点符号格式化UILabel?

108

是否有可能在 UILabel 中格式化 text 以显示圆点符号

如果可以,我该怎么做?


@Hoque:UILabel不会将其文本视为HTML。 - Ben Zotto
2
这里有一个相关的类!https://codeload.github.com/eyalc/ECListView/zip/master - Hemang
22
为什么这个问题被关闭了?这是一个合法的问题,有一个合法的答案。 - len
2
为什么http://stackoverflow.com/users/237838/andrew-barber将这个标记为离题,它可能是一个重复的问题,但绝不是离题的... - AppHandwerker
3
快捷键 ALT+8 = • - TheTiger
可能是一个重复问题:NSAttributedString插入项目符号? - Jonathan Cabrera
12个回答

-1

如果有人像我一样正在寻找带有项目符号的textview文本,下面是答案。顺便说一下,它仅适用于静态文本。

•   Better experience - Refer a friend and How to Play \n• Tournaments performance improvement\n• UI/UX Improvements\n• Critical bug fixes

我已将上述文本分配给TextView。对我来说,它按预期工作。


-1
这里我添加了一个带有项目符号的警告视图控制器,希望对你有所帮助。
*** 注意: 这仅适用于UIAlertViewController。
  func showBulletPointsAlert(title:String,subTitles:[String],getCompleted: @escaping((_ selected: Int) -> ())) {
        let alertController = UIAlertController(title: "\(title)\n", message: nil, preferredStyle: .alert)
        let value = convertToList(subTitles: subTitles, font: .boldSystemFont(ofSize: 14))
        alertController.setValue(value, forKey: "attributedMessage")

        let okAction = UIAlertAction(title: "Continue", style: .default, handler: { _ in
            getCompleted(0)
        })
        alertController.addAction(okAction)

        if let rootViewController = UIApplication.shared.windows.first?.rootViewController {
            rootViewController.present(alertController, animated: true, completion: nil)
        }
    }
    
    func convertToList(subTitles: [String],
             font: UIFont,
             bullet: String = "\u{2022}",
             indentation: CGFloat = 20,
             lineSpacing: CGFloat = 2,
             paragraphSpacing: CGFloat = 12,
             textColor: UIColor = .gray,
                       bulletColor: UIColor = ThemeManager.shared.mainTextColor) -> NSAttributedString {

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

        let paragraphStyle = NSMutableParagraphStyle()
        let nonOptions = [NSTextTab.OptionKey: Any]()
        paragraphStyle.tabStops = [
            NSTextTab(textAlignment: .left, location: indentation, options: nonOptions)]
        paragraphStyle.defaultTabInterval = indentation
        paragraphStyle.lineSpacing = lineSpacing
        paragraphStyle.paragraphSpacing = paragraphSpacing
        paragraphStyle.headIndent = indentation
        paragraphStyle.lineBreakMode = .byCharWrapping

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

            attributedString.addAttributes(
                [NSAttributedString.Key.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
    }
   

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接