Swift-下一行的属性字符串背景颜色

问题描述 投票:0回答:1

我有一个标签,希望在第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

我有一个标签,希望在第1行显示一些单词,在第2行显示一些单词。我可以通过在字符串之间放置“ \ n”来实现。第2行中的单词是带有背景的属性字符串...

ios swift string nsattributedstring
1个回答
0
投票

简单的解决方案-只需在第一行字符串中添加/ n-即可解决问题

© www.soinside.com 2019 - 2024. All rights reserved.