是否有可能在 UILabel
中格式化 text
以显示圆点符号?
如果可以,我该怎么做?
是否有可能在 UILabel
中格式化 text
以显示圆点符号?
如果可以,我该怎么做?
如果有人像我一样正在寻找带有项目符号的textview文本,下面是答案。顺便说一下,它仅适用于静态文本。
• Better experience - Refer a friend and How to Play \n• Tournaments performance improvement\n• UI/UX Improvements\n• Critical bug fixes
我已将上述文本分配给TextView。对我来说,它按预期工作。
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
}
UILabel
不会将其文本视为HTML。 - Ben ZottoALT+8 = •
。 - TheTiger