这是我的答案,在“吃饭”的位置上快速手动修复。您可能不需要它取决于您的标签布局。
let attributedText = myList.reduce(into: NSMutableAttributedString()) { result, next in
let whitespace = NSMutableAttributedString(string: " ")
whitespace.addAttributes(
[
.font: UIFont.systemFont(ofSize: 12),
.foregroundColor: UIColor.darkGray,
.backgroundColor: UIColor.white
],
range: NSRange(location: 0, length: whitespace.length)
)
let text = NSMutableAttributedString(string: next)
text.addAttributes(
[
.font: UIFont.systemFont(ofSize: 12),
.foregroundColor: UIColor.darkGray,
.backgroundColor: UIColor.lightGray
],
range: NSRange(location: 0, length: text.length)
)
result.append(whitespace)
if(next.hasPrefix("Meal")){
result.append(whitespace)
}
result.append(text)
result.append(whitespace)
guard myList.last != next else { return }
result.append(NSMutableAttributedString(string: " "))
}