swift - 如何在 swift 中将一串逗号值转换为项目符号列表?

标签 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)

关于swift - 如何在 swift 中将一串逗号值转换为项目符号列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57617146/

相关文章:

iOS (Swift) - 应用程序委托(delegate)窗口的 Root View Controller 在模拟器中工作,但在手机上加载应用程序时失败

swift - 裁剪后 UIImage 旋转 90 度

IOS - 最新版本的默认 TableView 单元格分隔符颜色

ios - 如何回到 WKWebView 历史记录中的起点

ios - Swift Quick/Nimble - 等待谓词匹配

swift - 访问 Swift 4 中 PartialKeyPath 的可选值

Swift - 调度队列和串行运行的 U/I

ios - Swift inout 参数性能

ios - UITableView 中的 MFMessageComposeViewController (Swift)

ios - 如何在 Xcode 中构建响应式布局(不是自动布局)?