Ios NSAttributedString和Button存在问题

Ios NSAttributedString和Button存在问题,ios,swift,uibutton,nsattributedstring,nsattributedstringkey,Ios,Swift,Uibutton,Nsattributedstring,Nsattributedstringkey,我正在尝试用NSAttribute字符串更新按钮标题字段。因此,我试图设置属性标题,但它无法正常工作。它不显示我放入NSAttributedString中的正确属性。我得出的结论是,我初始化NSAttributedString的方法不正确,或者按钮覆盖了颜色 我获取字符串的原始值,但不使用属性属性 卡片结构 struct Card { private var shape : Shape = Shape.none private var color : Color = Color.

我正在尝试用NSAttribute字符串更新按钮标题字段。因此,我试图设置属性标题,但它无法正常工作。它不显示我放入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 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时也是一样的,文档“不做任何事情”中说明了这一点:

指定0(默认值)表示没有其他更改

通过将其设置为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";
}
$>仅笔划颜色:
你好{
NSColor=“UIExtendedGrayColorSpace 1”;
NSFont=“font-family:\”.SFUIDisplay\“字体重量:正常;字体样式:正常;字体大小:40.00pt”;
NSStrokeColor=“UIExtendedSRGBColorSpace 1 0 0 1”;
}
$>笔划颜色+宽度设置为0:
你好{
NSColor=“UIExtendedGrayColorSpace 1”;
NSFont=“font-family:\”.SFUIDisplay\“字体重量:正常;字体样式:正常;字体大小:40.00pt”;
NSStrokeColor=“UIExtendedSRGBColorSpace 1 0 0 1”;
NSStrokeWidth=0;
}
$>笔划颜色+宽度设置为-4:
你好{
NSColor=“UIExtendedGrayColorSpace 1”;
NSFont=“font-family:\”.SFUIDisplay\“字体重量:正常;字体样式:正常;字体大小:40.00pt”;
NSStrokeColor=“UIExtendedSRGBColorSpace 1 0 0 1”;
NSStrokeWidth=“-4”;
}
渲染:


ui如果按钮类型为系统,则按钮遵循tintColor属性。尝试将按钮类型更改为自定义?嗯。。。只是把它变成黑色,没有点击按钮。你的问题中有很多信息缺失。应用于属性字符串的实际属性是什么?将
print(“attributes:\(strokeTextAttributes)”)
添加到您的
Card.rawValue
函数中,然后使用两张打印的精确输出更新您的问题。attributes:[\u C.NSAttributedStringKey(\u rawValue:NSStrokeColor):UIExtendedSRGBColorSpace 1 0 1]属性:[\u C.NSAttributedStringKey(\u rawValue:NSStrokeColor):UIExtendedSRGBColorSpace 0 0 1 1]我很确定我的按钮有问题。setAttributedTitle。。。我使用[.strokeColor:UIColor.orange]硬编码属性。。。还是不行。那一定意味着按钮出了问题。兄弟,你是个传奇。。。我已经搔头两天了哈哈!请问,您是如何从日志中发现这一点的?我熟悉
NSAttributedString
。其中一些需要两种设置。当您知道可以修改多少设置时,可以设置笔划的宽度似乎很正常,而日志中没有,只有颜色。此外,逻辑与myView.layer.borderColor和myView.layer.borderWidth的逻辑类似。