Matteo
Matteo

Reputation: 13

NSAttributedString text printing out grey instead of black?

I pass in a string of text and make the name in the string bold and black and the rest just regular black. However, it is not working and the name is coming out grey?

cell.acceptedLabel.attributedText = attributedFunc(stringinput: "\(currentUser.notifications[indexPath.row - 1].usersName ?? "Someone") accepted your buddy request.", indexPathRow: indexPath.row - 1)
func attributedFunc(stringinput: String, indexPathRow: Int) -> NSMutableAttributedString {

        let amountText = NSMutableAttributedString.init(string: stringinput)
        print(stringinput)
        if currentUser.notifications[indexPathRow].type == "buddyRequestAccepted" {

            let usersName = currentUser.notifications[indexPathRow].usersName
            print(usersName)
            let usersNameDigits = usersName?.count
            amountText.setAttributes([NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15),
                                      NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.strokeWidth: UIFont.Weight.semibold],
                                     range: NSMakeRange(0, usersNameDigits!))

            amountText.setAttributes([NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15), NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.strokeWidth: UIFont.Weight.regular], range: NSMakeRange(usersNameDigits! + 1, 12))

            // if you want, you can add more attributes for different ranges calling .setAttributes many times
            print(amountText)
            return amountText

        } else if currentUser.notifications[indexPathRow].type == "memberRequestAccepted" {
            return amountText
        }
        return amountText
    }

Here I console print out the String, the name, and the total NSAttributedString:

Austin accepted your buddy request.

Optional("Austin")

Austin{
    NSColor = "UIExtendedGrayColorSpace 0 1";
    NSFont = "<UICTFont: 0x7fb062f3d2c0> font-family: \".SFUIText\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
    NSStrokeWidth = "0.300000011920929";
} {
}accepted you{
    NSColor = "UIExtendedGrayColorSpace 0 1";
    NSFont = "<UICTFont: 0x7fb062f3d2c0> font-family: \".SFUIText\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
    NSStrokeWidth = 0;
}r buddy request.{
}

Picture of issue:

enter image description here

Upvotes: 1

Views: 148

Answers (1)

Komal Goyani
Komal Goyani

Reputation: 847

This is because of you set weight to strokeWidth NSAttributedStringKey.strokeWidth: UIFont.Weight.semibold. You can use the below code that may help you.

amountText.setAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15, weight: .semibold),
                              NSAttributedString.Key.foregroundColor: UIColor.black],
                             range: NSMakeRange(0, usersNameDigits))

Upvotes: 1

Related Questions