一天中的好时光。我正在尝试实现一个带有列表的屏幕,用项目形式的图片标记。为了实现,我选择了 UITextView 和属性文本,我正在尝试为此准备 NSMutableAttributedString
文本中的短语可以有不同的样式(常规和粗体)。我正在尝试通过 NSMutableParagraphStyle 配置间隔:
static var textBlock1: NSMutableAttributedString {
let text1 = "\thow much does it cost to suspend"
let text2 = "Basic services"
let text3 = "Internet, TV, Telephony — 20$ / month for one"
let text4 = "Additional services -"
let text5 = "free."
let completeText = NSMutableAttributedString(string: "")
completeText.append(Const.image)
completeText.append(NSAttributedString(string: text1, attributes: Const.regularAttributes))
completeText.append(NSAttributedString(string: text2, attributes: Const.boldAttributes))
completeText.append(NSAttributedString(string: text3, attributes: Const.regularAttributes))
completeText.append(NSAttributedString(string: text4, attributes: Const.boldAttributes))
completeText.append(NSAttributedString(string: text5, attributes: Const.regularAttributes))
completeText.enumerateAttributes(in: NSMakeRange(0, completeText.string.count), options: []) { (attribute, range, stop) -> Void in
if attribute.keys.contains(.font) {
completeText.addAttributes(
[.paragraphStyle : Const.paragraphStyle],
range: NSMakeRange(range.lowerBound - 1, range.length))
}
}
return completeText
}
当文本中没有换行符(“\n”)时,一切正常,文本缩进正确:
但是当我尝试在文本中添加换行符“\n”时,一切都会中断
let text1 = "\thow much does it cost to suspend\n"
let text2 = "Basic services\n"
let text3 = "Internet, TV, Telephony — 20$ / month for one\n"
let text4 = "Additional services -"
let text5 = "free."
告诉我我做错了什么以及如何解决这个问题。预先感谢您的回答。