如何在 UILabel 中设置 2 行属性字符串

How to set 2 lines of attribute string in UILabel

我想在 UILabel 中设置属性文本。应该是2行。所以我制作了 2 个这样的属性字符串。

var myMutableTitle = NSMutableAttributedString(string: title!, attributes: [NSFontAttributeName:UIFont.init(name: fontBold, size: 15.0)!])
var mutDj=NSMutableAttributedString(string: dj!, attributes: [NSFontAttributeName:UIFont.init(name: font, size: 15.0)!])

如何附加这两个属性字符串以显示在 2 行中,如

Title
DJ name

请帮帮我。 谢谢

\n添加到第二个属性文本

var myMutableTitle = NSMutableAttributedString(string: title!, attributes: [NSFontAttributeName:UIFont.init(name: fontBold, size: 15.0)!])
var mutDj= NSMutableAttributedString(string: "\n \(dj)", attributes: [NSFontAttributeName:UIFont.init(name: font, size: 15.0)!])
myMutableTitle.appendAttributedString(mutDj)

yourLabel.numberOfLines = 0
yourLabel.attributedText = myMutableTitle

您可以像这样创建一个,而不是创建 2 个 NSMutableAttributedString

let str1 = "\(title!) \n(dj!)"
let attributedStr = NSMutableAttributedString(string: str1)
attributedStr.addAttribute(NSFontAttributeName, value: UIFont.init(name: fontBold, size: 15.0)!, range: (str1 as NSString).rangeOfString(title!))
attributedStr.addAttribute(NSFontAttributeName, value: UIFont.init(name: font, size: 15.0)!, range: (str1 as NSString).rangeOfString(dj!))
label.attributedText = attributedStr