NSAttributedString 文本打印出灰色而不是黑色?
NSAttributedString text printing out grey instead of black?
我传入一串文本并将字符串中的名称设为粗体和黑色,其余部分只是常规黑色。但是,它不起作用并且名称显示为灰色?
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.{
}
问题图片:
这是因为您将权重设置为 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))
我传入一串文本并将字符串中的名称设为粗体和黑色,其余部分只是常规黑色。但是,它不起作用并且名称显示为灰色?
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.{
}
问题图片:
这是因为您将权重设置为 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))