gpt4 book ai didi

ios - NSAttributedString 文本打印出灰色而不是黑色?

转载 作者:行者123 更新时间:2023-11-28 05:39:11 25 4
gpt4 key购买 nike

我传入一串文本并将字符串中的名称设为粗体和黑色,其余部分只是常规黑色。但是,它不起作用并且名称显示为灰色?

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
}

这里我控制台打印出字符串、名称和总的 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.{
}

问题图片:

enter image description here

最佳答案

这是因为您将权重设置为 strokeWidth NSAttributedStringKey.strokeWidth: UIFont.Weight.semibold。您可以使用以下可能对您有所帮助的代码。

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

关于ios - NSAttributedString 文本打印出灰色而不是黑色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57550826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com