NSAttributedString 和 Button 的问题
Issues with NSAttributedString and Button
我正在尝试使用 NSAttributedString 更新按钮标题字段。所以我尝试设置属性标题,但它无法正常工作。它不显示我放入 NSAttributedString 中的正确属性。我得出的结论是我初始化 NSAttributedString 的方法不正确,或者按钮覆盖了颜色。
我得到了字符串的原始值,但没有属性值。
卡片结构
struct Card {
private var shape : Shape = Shape.none
private var color : Color = Color.none
private var number : Number = Number.none
private var shading : Shading = Shading.none
//These attributes are not showing up
private var strokeTextAttributes : [NSAttributedStringKey:Any] = [NSAttributedStringKey:Any]()
private var manipulatedValue : String = ""
init(shape : Shape, color : Color, number : Number, shading : Shading){
self.shape = shape
self.color = color
self.number = number
self.shading = shading
//How many multiples of the shape should be on the card
for _ in 1...number.rawValue{
manipulatedValue.append(shape.rawValue)
}
//0 is the NSStringKey, 1 is the UIColor
strokeTextAttributes[color.rawValue.0] = color.rawValue.1
}
func rawValue() -> NSAttributedString{
return NSAttributedString(string: manipulatedValue, attributes: strokeTextAttributes)
}
}
ViewController Class
class ViewController: UIViewController {
private var game : Set = Set()
override func viewDidLoad() {
super.viewDidLoad()
for index in 0...game.cardsInPlay.count-1{
cardButtons[index].layer.borderWidth = 3.0
cardButtons[index].layer.borderColor = UIColor.black.cgColor
cardButtons[index].layer.cornerRadius = 8.0
cardButtons[index].setAttributedTitle(game.cardsInPlay[index]!.rawValue(), for: UIControlState.normal)
}
}
@IBOutlet var cardButtons: [UIButton]!
}
我尝试将这些值硬编码到 Card 结构的初始值设定项中。但同样的情况会再次发生。该按钮显示卡片和数字的正确形状,但不是正确的颜色。该颜色将是 Main.Storyboard 检查器中的默认颜色。浅蓝。问题是字典不工作,因此 strokeTextAttributes 不包含任何有意义的内容,或者 UIButton 正在做一些愚蠢的事情。
您正在设置 NSAttributedStringKey.strokeColor
,但您没有设置 NSAttributedStringKey.strokeWidth
。这就是为什么你没有得到想要的结果。
如果它不存在,我想(这完全有意义)将 .strokeWidth
设置为 0 是一样的,这在文档 "doing nothing":
中有说明
Specify 0 (the default) for no additional changes
通过将其设置为 0 或根本不设置,我得到了相同的结果。
示例测试代码:
let str = "Hello !"
let attributedString = NSMutableAttributedString.init(string: str,
attributes: [.font: UIFont.systemFont(ofSize: 40),
.foregroundColor: UIColor.white])
let label1 = UILabel(frame: CGRect(x: 30, y: 40, width: 200, height: 50))
label1.backgroundColor = .lightGray
label1.attributedText = attributedString
let label2 = UILabel(frame: CGRect(x: 30, y: 95, width: 200, height: 50))
label2.backgroundColor = .lightGray
label2.attributedText = attributedString
let label3 = UILabel(frame: CGRect(x: 30, y: 150, width: 200, height: 50))
label3.backgroundColor = .lightGray
label3.attributedText = attributedString
let label4 = UILabel(frame: CGRect(x: 30, y: 205, width: 200, height: 50))
label4.backgroundColor = .lightGray
label4.attributedText = attributedString
attributedString.addAttributes([NSAttributedStringKey.strokeColor: UIColor.red],
range: NSRange.init(location: 0, length: attributedString.string.utf16.count))
label2.attributedText = attributedString
print("Only Stroke Color:\n\(attributedString)")
attributedString.addAttributes([NSAttributedStringKey.strokeWidth: 0],
range: NSRange.init(location: 0, length: attributedString.string.utf16.count))
label3.attributedText = attributedString
print("Stroke Color + Width set to 0:\n\(attributedString)")
attributedString.addAttributes([NSAttributedStringKey.strokeWidth: -4],
range: NSRange.init(location: 0, length: attributedString.string.utf16.count))
label4.attributedText = attributedString
print("Stroke Color + Width set to -4:\n\(attributedString)")
self.view.addSubview(label1)
self.view.addSubview(label2)
self.view.addSubview(label3)
self.view.addSubview(label4)
日志输出:
$>Only Stroke Color:
Hello !{
NSColor = "UIExtendedGrayColorSpace 1 1";
NSFont = "<UICTFont: 0x7f8973e11120> font-family: \".SFUIDisplay\"; font-weight: normal; font-style: normal; font-size: 40.00pt";
NSStrokeColor = "UIExtendedSRGBColorSpace 1 0 0 1";
}
$>Stroke Color + Width set to 0:
Hello !{
NSColor = "UIExtendedGrayColorSpace 1 1";
NSFont = "<UICTFont: 0x7f8973e11120> font-family: \".SFUIDisplay\"; font-weight: normal; font-style: normal; font-size: 40.00pt";
NSStrokeColor = "UIExtendedSRGBColorSpace 1 0 0 1";
NSStrokeWidth = 0;
}
$>Stroke Color + Width set to -4:
Hello !{
NSColor = "UIExtendedGrayColorSpace 1 1";
NSFont = "<UICTFont: 0x7f8973e11120> font-family: \".SFUIDisplay\"; font-weight: normal; font-style: normal; font-size: 40.00pt";
NSStrokeColor = "UIExtendedSRGBColorSpace 1 0 0 1";
NSStrokeWidth = "-4";
}
渲染:
我正在尝试使用 NSAttributedString 更新按钮标题字段。所以我尝试设置属性标题,但它无法正常工作。它不显示我放入 NSAttributedString 中的正确属性。我得出的结论是我初始化 NSAttributedString 的方法不正确,或者按钮覆盖了颜色。
我得到了字符串的原始值,但没有属性值。
卡片结构
struct Card {
private var shape : Shape = Shape.none
private var color : Color = Color.none
private var number : Number = Number.none
private var shading : Shading = Shading.none
//These attributes are not showing up
private var strokeTextAttributes : [NSAttributedStringKey:Any] = [NSAttributedStringKey:Any]()
private var manipulatedValue : String = ""
init(shape : Shape, color : Color, number : Number, shading : Shading){
self.shape = shape
self.color = color
self.number = number
self.shading = shading
//How many multiples of the shape should be on the card
for _ in 1...number.rawValue{
manipulatedValue.append(shape.rawValue)
}
//0 is the NSStringKey, 1 is the UIColor
strokeTextAttributes[color.rawValue.0] = color.rawValue.1
}
func rawValue() -> NSAttributedString{
return NSAttributedString(string: manipulatedValue, attributes: strokeTextAttributes)
}
}
ViewController Class
class ViewController: UIViewController {
private var game : Set = Set()
override func viewDidLoad() {
super.viewDidLoad()
for index in 0...game.cardsInPlay.count-1{
cardButtons[index].layer.borderWidth = 3.0
cardButtons[index].layer.borderColor = UIColor.black.cgColor
cardButtons[index].layer.cornerRadius = 8.0
cardButtons[index].setAttributedTitle(game.cardsInPlay[index]!.rawValue(), for: UIControlState.normal)
}
}
@IBOutlet var cardButtons: [UIButton]!
}
我尝试将这些值硬编码到 Card 结构的初始值设定项中。但同样的情况会再次发生。该按钮显示卡片和数字的正确形状,但不是正确的颜色。该颜色将是 Main.Storyboard 检查器中的默认颜色。浅蓝。问题是字典不工作,因此 strokeTextAttributes 不包含任何有意义的内容,或者 UIButton 正在做一些愚蠢的事情。
您正在设置 NSAttributedStringKey.strokeColor
,但您没有设置 NSAttributedStringKey.strokeWidth
。这就是为什么你没有得到想要的结果。
如果它不存在,我想(这完全有意义)将 .strokeWidth
设置为 0 是一样的,这在文档 "doing nothing":
Specify 0 (the default) for no additional changes
通过将其设置为 0 或根本不设置,我得到了相同的结果。
示例测试代码:
let str = "Hello !"
let attributedString = NSMutableAttributedString.init(string: str,
attributes: [.font: UIFont.systemFont(ofSize: 40),
.foregroundColor: UIColor.white])
let label1 = UILabel(frame: CGRect(x: 30, y: 40, width: 200, height: 50))
label1.backgroundColor = .lightGray
label1.attributedText = attributedString
let label2 = UILabel(frame: CGRect(x: 30, y: 95, width: 200, height: 50))
label2.backgroundColor = .lightGray
label2.attributedText = attributedString
let label3 = UILabel(frame: CGRect(x: 30, y: 150, width: 200, height: 50))
label3.backgroundColor = .lightGray
label3.attributedText = attributedString
let label4 = UILabel(frame: CGRect(x: 30, y: 205, width: 200, height: 50))
label4.backgroundColor = .lightGray
label4.attributedText = attributedString
attributedString.addAttributes([NSAttributedStringKey.strokeColor: UIColor.red],
range: NSRange.init(location: 0, length: attributedString.string.utf16.count))
label2.attributedText = attributedString
print("Only Stroke Color:\n\(attributedString)")
attributedString.addAttributes([NSAttributedStringKey.strokeWidth: 0],
range: NSRange.init(location: 0, length: attributedString.string.utf16.count))
label3.attributedText = attributedString
print("Stroke Color + Width set to 0:\n\(attributedString)")
attributedString.addAttributes([NSAttributedStringKey.strokeWidth: -4],
range: NSRange.init(location: 0, length: attributedString.string.utf16.count))
label4.attributedText = attributedString
print("Stroke Color + Width set to -4:\n\(attributedString)")
self.view.addSubview(label1)
self.view.addSubview(label2)
self.view.addSubview(label3)
self.view.addSubview(label4)
日志输出:
$>Only Stroke Color:
Hello !{
NSColor = "UIExtendedGrayColorSpace 1 1";
NSFont = "<UICTFont: 0x7f8973e11120> font-family: \".SFUIDisplay\"; font-weight: normal; font-style: normal; font-size: 40.00pt";
NSStrokeColor = "UIExtendedSRGBColorSpace 1 0 0 1";
}
$>Stroke Color + Width set to 0:
Hello !{
NSColor = "UIExtendedGrayColorSpace 1 1";
NSFont = "<UICTFont: 0x7f8973e11120> font-family: \".SFUIDisplay\"; font-weight: normal; font-style: normal; font-size: 40.00pt";
NSStrokeColor = "UIExtendedSRGBColorSpace 1 0 0 1";
NSStrokeWidth = 0;
}
$>Stroke Color + Width set to -4:
Hello !{
NSColor = "UIExtendedGrayColorSpace 1 1";
NSFont = "<UICTFont: 0x7f8973e11120> font-family: \".SFUIDisplay\"; font-weight: normal; font-style: normal; font-size: 40.00pt";
NSStrokeColor = "UIExtendedSRGBColorSpace 1 0 0 1";
NSStrokeWidth = "-4";
}
渲染: