【发布时间】:2020-02-12 22:29:43
【问题描述】:
我有一个标签,我希望在第 1 行显示一些单词,在第 2 行显示一些单词。我可以通过在字符串之间放置“\n”来实现它。第 2 行的单词是带有背景颜色的属性字符串,但是背景颜色从第一行开始。
有没有办法,我可以在第 2 行获得背景颜色?
下面是代码:
let attributedString = NSMutableAttributedString(string: "Line 1 Text" ?? "")
let attributesForNonSelectedRow = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold),NSAttributedString.Key.foregroundColor:UIColor(rgb: 0x707070),NSAttributedString.Key.backgroundColor:UIColor(rgb: 0xE5E5E5)]
let myTitle = NSAttributedString(string: " \n Line 2 Text", attributes: attributesForNonSelectedRow)
attributedString.append(myTitle)
searchTitleLabel.lineBreakMode = .byWordWrapping
searchTitleLabel.numberOfLines = 0
searchTitleLabel.attributedText = attributedString
【问题讨论】:
标签: ios swift string nsattributedstring