在UILabel背景中添加了图标,接下来想要更改图标色调颜色

时间:2017-08-31 07:06:22

标签: ios swift

我在UILabel背景中添加了图标,接下来我想更改图标色调,但颜色根本没有变化,因为我不知道我要为它编写什么代码。 UILabel数据来自网络。我在加载数据后添加了图标

扩展

    extension UILabel {

        func addImageWith(name: String, behindText: Bool) {

            let attachment = NSTextAttachment()
            attachment.image = UIImage(named: name)
            let attachmentString = NSAttributedString(attachment: attachment)

            guard let txt = self.text else {
                return
            }

            if behindText {
                let strLabelText = NSMutableAttributedString(string: txt)
                strLabelText.append(attachmentString)
                self.attributedText = strLabelText
            } else {
                let strLabelText = NSAttributedString(string: txt)
                let mutableAttachmentString = NSMutableAttributedString(attributedString: attachmentString)
                mutableAttachmentString.append(strLabelText)
                self.attributedText = mutableAttachmentString
            }
        }

        func removeImage() {
            let text = self.text
            self.attributedText = nil
            self.text = text
        }
    }

   //Label

      let addressLabel: UILabel = {

            let label = UILabel()
            label.text = " COMIC CAFE, BANNAI"
            label.font = UIFont.systemFont(ofSize: 6)
            label.textColor = .orange

            label.addImageWith(name: "location copy", behindText: false)

            label.textAlignment = .center

            return label
               }()

    **didSet in cell** 
        didSet {

                if let address = product?.store_address {

                    addressLabel.text = product?.store_address
                    addressLabel.addImageWith(name: "location", behindText: false)
                }else {
                    addressLabel.text = ""
                }

1 个答案:

答案 0 :(得分:0)

也许你不能这样做。

let label = UILabel()

label.text = " COMIC CAFE, BANNAI"
label.font = UIFont.systemFont(ofSize: 6)
label.textColor = .orange

label.addImageWith(name: "location copy", behindText: false)
label.textAlignment = .center`