不应用导航栏中标题的描边文本属性

时间:2017-08-03 03:22:39

标签: ios swift

我无法弄清楚为什么导航栏标题和文本标签的相同笔划文本属性看起来不同。我希望标题看起来像标签,请指教:

    let myLabel = UILabel(frame: CGRect(x: 50, y: 100, width: 200, height: 30))
    let strokeTextAttributes = [NSFontAttributeName: UIFont.init(name:
        "Helvetica-Bold", size:20.0),
        NSStrokeColorAttributeName : UIColor.black,
                                NSForegroundColorAttributeName : UIColor.white,
                                NSStrokeWidthAttributeName : -3.0,
                                ] as [String : Any]

    myLabel.attributedText = NSAttributedString(string: "Test", attributes: strokeTextAttributes)
    self.navigationController?.navigationBar.titleTextAttributes = strokeTextAttributes
    self.title = "Test"
    self.view.addSubview(myLabel)

enter image description here

1 个答案:

答案 0 :(得分:1)

我使用navigationItem的titleView属性解决了这个问题,如下所示:

let strokeTextAttributes = [NSFontAttributeName: UIFont.init(name:
                        "Helvetica-Bold", size:20.0),
                                            NSStrokeColorAttributeName : UIColor.blue,
                                            NSForegroundColorAttributeName : UIColor.white,
                                            NSStrokeWidthAttributeName : -3.0,
                                            ] as [String : Any]
                let myLabel = UILabel(frame: CGRect(x: 50, y: 100, width: 200, height: 30))


                myLabel.attributedText = NSAttributedString(string: "Test", attributes: strokeTextAttributes)

                navigationItem.titleView = myLabel