swift - 如何快速将一串逗号值转换为项目符号列表?
问题描述
我正在使用一个将数组作为参数的函数,但问题是我有一串逗号值需要用作参数。
// comma values in a string
let comment = "Examples of things, Another thing, More things"
// the bulletPoint function that takes an array.
func bulletPointList(strings: [String]) -> NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.headIndent = 15
paragraphStyle.minimumLineHeight = 22
paragraphStyle.maximumLineHeight = 22
paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: 15)]
let stringAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15),
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.paragraphStyle: paragraphStyle
]
let string = strings.map({ "•\t\($0)" }).joined(separator: "\n")
return NSAttributedString(string: string,
attributes: stringAttributes)
}
我应该先将注释字符串转换为数组吗?
//The function looks like this called
label.numberOfLines = 0
label.attributedText = bulletPointList(strings: ["Examples of things", "Another thing", "More things"])
-谢谢大家
解决方案
如果我的理解是正确的,你可以使用下面的代码来获取字符串数组。
let comment = "Examples of things, Another thing, More things"
let stringArray = comment.split(separator: ",").map{String($0)}
label.attributedText = bulletPointList(strings: stringArray)
//or
let stringArray1 = comment.components(separatedBy: ",")
label.attributedText = bulletPointList(strings: stringArray1)
推荐阅读
- python - 从 Spyder 导入 wxpython 时出现 ImportError
- python - 将带有负数的列表作为字符串排序会产生意外的结果
- mysql - 如何用regexp_replace在mysql中用特定字符替换多个字符?
- ibm-mq - IBM MQ 镜像 docker 旧版本
- sql-server - 将空值下移到 SQL Server 中的下一行
- spring-security - 在 ff4j 上启用弹簧安全性时如何创建功能切换?
- inno-setup - 使用使用 inno 安装脚本编写的命令行以 VERYSILENT 模式安装应用程序
- sql - 无法在 sql server 中设置地理数据类型的值
- amazon-web-services - Amazon EMR:仅在先前的作业已完成时才开始新的计划作业
- matlab - MATLAB JIT for 循环优化