NSAttributedString 未返回预期值
NSAttributedString not returning expected value
我正在尝试创建如下文本:"bill 开始关注您。" 并将用户名设为半粗体。我试过这个但是做 .string 给了我一个奇怪的长字符串。我该如何解决?
//let nUsername = username
let fontU = UIFont(name: "SFCompactDisplay-Semibold", size: 15)
let fontN = UIFont(name: "SFCompactDisplay-Regular", size: 15)
let usernameAttributes = [NSAttributedString.Key.font: fontU]
let notifAttributes = [NSAttributedString.Key.font: fontN]
let firstString = NSMutableAttributedString(string: username, attributes: usernameAttributes)
let secondString = NSAttributedString(string: " started following you.", attributes: notifAttributes)
firstString.append(firstString)
firstString.append(secondString)
//notifInfoLabel.frame = CGRect(x: 77, y: 15, width: 295, height: 18)
notifInfoLabel.text = "\(firstString.string)"
摆脱:
firstString.append(firstString)
重复用户名。
并且您没有使用最终的属性字符串。使用:
notifInfoLabel.attributedText = firstString
我正在尝试创建如下文本:"bill 开始关注您。" 并将用户名设为半粗体。我试过这个但是做 .string 给了我一个奇怪的长字符串。我该如何解决?
//let nUsername = username
let fontU = UIFont(name: "SFCompactDisplay-Semibold", size: 15)
let fontN = UIFont(name: "SFCompactDisplay-Regular", size: 15)
let usernameAttributes = [NSAttributedString.Key.font: fontU]
let notifAttributes = [NSAttributedString.Key.font: fontN]
let firstString = NSMutableAttributedString(string: username, attributes: usernameAttributes)
let secondString = NSAttributedString(string: " started following you.", attributes: notifAttributes)
firstString.append(firstString)
firstString.append(secondString)
//notifInfoLabel.frame = CGRect(x: 77, y: 15, width: 295, height: 18)
notifInfoLabel.text = "\(firstString.string)"
摆脱:
firstString.append(firstString)
重复用户名。
并且您没有使用最终的属性字符串。使用:
notifInfoLabel.attributedText = firstString