NSAttributedString不返回期望值

时间:2019-07-08 23:21:36

标签: ios swift nsattributedstring

我正在尝试创建如下文本: 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)"

1 个答案:

答案 0 :(得分:1)

摆脱:

firstString.append(firstString)

与用户名重复。

并且您没有使用最终的属性字符串。使用:

notifInfoLabel.attributedText = firstString