Swift - 下一行的属性字符串背景颜色
Swift - Attribute string background color on next line
我有一个标签,我希望在第 1 行和第 2 行显示一些单词。我可以通过在字符串之间放置“\n”来实现它。第 2 行的单词是具有背景颜色的属性字符串,但是背景颜色从第 1 行本身开始。
有没有办法,我可以只在第 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
简单的解决方案:- 只需在第一个字符串的行中添加 /n - 即可解决问题
let attributedString = NSMutableAttributedString(string: "Line 1 Text\n" ?? "")
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: "Line 2 Text", attributes: attributesForNonSelectedRow)
attributedString.append(myTitle)
searchTitleLabel.lineBreakMode = .byWordWrapping
searchTitleLabel.numberOfLines = 0
searchTitleLabel.attributedText = attributedString
通过约束的解决方案:
为什么不尝试通过提供 Leading、Top 和 Height 约束来设置标签的约束,您可以实现它..
我写了这个问题。我所要做的就是将“\n”放在第 1 行的后面而不是第 2 行的前面,这样就解决了背景颜色问题。
let attributedString = NSMutableAttributedString(string: "Line 1 Text \n" ?? "")
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: "Line 2 Text", attributes: attributesForNonSelectedRow)
attributedString.append(myTitle)
searchTitleLabel.lineBreakMode = .byWordWrapping
searchTitleLabel.numberOfLines = 0
searchTitleLabel.attributedText = attributedString
我有一个标签,我希望在第 1 行和第 2 行显示一些单词。我可以通过在字符串之间放置“\n”来实现它。第 2 行的单词是具有背景颜色的属性字符串,但是背景颜色从第 1 行本身开始。
有没有办法,我可以只在第 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
简单的解决方案:- 只需在第一个字符串的行中添加 /n - 即可解决问题
let attributedString = NSMutableAttributedString(string: "Line 1 Text\n" ?? "")
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: "Line 2 Text", attributes: attributesForNonSelectedRow)
attributedString.append(myTitle)
searchTitleLabel.lineBreakMode = .byWordWrapping
searchTitleLabel.numberOfLines = 0
searchTitleLabel.attributedText = attributedString
通过约束的解决方案:
为什么不尝试通过提供 Leading、Top 和 Height 约束来设置标签的约束,您可以实现它..
我写了这个问题。我所要做的就是将“\n”放在第 1 行的后面而不是第 2 行的前面,这样就解决了背景颜色问题。
let attributedString = NSMutableAttributedString(string: "Line 1 Text \n" ?? "")
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: "Line 2 Text", attributes: attributesForNonSelectedRow)
attributedString.append(myTitle)
searchTitleLabel.lineBreakMode = .byWordWrapping
searchTitleLabel.numberOfLines = 0
searchTitleLabel.attributedText = attributedString