代码之家  ›  专栏  ›  技术社区  ›  Blue

Swift 4标签属性

  •  13
  • Blue  · 技术社区  · 8 年前

    我正在从swift 3移动到swift 4。我有UILabels,我给标签赋予了非常具体的文本属性。在初始化strokeTextAttributes时,我得到了一个“在展开可选值时意外发现nil”错误。我完全无法坦诚相待。

    在swift 3中,strokeTextAttributes的值是[String:Any],但swift 4抛出了错误,直到我将其更改为下面的值。

    let strokeTextAttributes = [
        NSAttributedStringKey.strokeColor.rawValue : UIColor.black,
        NSAttributedStringKey.foregroundColor : UIColor.white,
        NSAttributedStringKey.strokeWidth : -2.0,
        NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18)
        ] as! [NSAttributedStringKey : Any]
    
    
    chevronRightLabel.attributedText = NSMutableAttributedString(string: "0", attributes: strokeTextAttributes)
    
    7 回复  |  直到 8 年前
        1
  •  32
  •   Xavier Lowmiller hotpaw2    7 年前

    @Larme对 .rawValue

    此外,您可以使用显式类型避免强制转换导致代码崩溃:

    let strokeTextAttributes: [NSAttributedString.Key: Any] = [
        .strokeColor : UIColor.black,
        .foregroundColor : UIColor.white,
        .strokeWidth : -2.0,
        .font : UIFont.boldSystemFont(ofSize: 18)
    ]
    

    NSAttributedString.Key.

        2
  •  16
  •   Krunal    8 年前

    Swift 4.0+ ,属性字符串接受键类型为的json(字典) NSAttributedStringKey NSAttributedString.Key .

    所以你必须把它从 [String : Any]

    Swift 4.1和;以下- [NSAttributedStringKey : Any] &
    Swift 4.2和;以上- [NSAttributedString.Key : Any]

    Swift 4.2

    初始化器 AttributedString 在Swift 4.2中更改为 [NSAttributedString.Key : Any]?

    public init(string str: String, attributes attrs: [NSAttributedString.Key : Any]? = nil)
    

    下面是示例工作代码。

    let label = UILabel()
    let labelText = "String Text"
    
    let strokeTextAttributes = [
         NSAttributedString.Key.strokeColor : UIColor.black,
         NSAttributedString.Key.foregroundColor : UIColor.white,
         NSAttributedString.Key.strokeWidth : -2.0,
         NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 18)
       ] as [NSAttributedString.Key : Any]
    
    label.attributedText = NSAttributedString(string: labelText, attributes: strokeTextAttributes)
    

    Swift 4.0

    初始化器 AttributedString 在Swift 4.0中更改为 [NSAttributedStringKey : Any]?

    public init(string str: String, attributes attrs: [NSAttributedStringKey : Any]? = nil)
    

    let label = UILabel()
    let labelText = "String Text"
    
    let strokeTextAttributes = [
         NSAttributedStringKey.strokeColor : UIColor.black,
         NSAttributedStringKey.foregroundColor : UIColor.white,
         NSAttributedStringKey.strokeWidth : -2.0,
         NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18)
       ] as [NSAttributedStringKey : Any]
    
    label.attributedText = NSAttributedString(string: labelText, attributes: strokeTextAttributes)
    

    查看此Apple文档,了解更多信息: NSAttributedString - Creating an NSAttributedString Object

        3
  •  1
  •   Community Mohan Dere    6 年前

    NSAttributedStringKey.strokeColor.rawValue 属于类型 String

    NSAttributedStringKey.strokeColor 属于类型 NSAttributedStringKey

    一串 NSAttributedStringKey . 您必须使用以下内容:

    let strokeTextAttributes: [NSAttributedStringKey : Any] = [
        NSAttributedStringKey.strokeColor : UIColor.black,
        NSAttributedStringKey.foregroundColor : UIColor.white,
        NSAttributedStringKey.strokeWidth : -2.0,
        NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18)
    ]
    
        4
  •  0
  •   Azharhussain Shaikh    8 年前

    具有多种颜色的Swift 4属性文本

    extension NSMutableAttributedString 
    {
    @discardableResult func DustyOrange(_ text: String, Fontsize : CGFloat) -> NSMutableAttributedString 
    {
        let attrs: [NSAttributedStringKey: Any] = [.font: UIFont(name: "SFUIDisplay-Regular", size: Fontsize)!, NSAttributedStringKey.foregroundColor: UIColor(red: 242.0/255.0, green: 97.0/255.0, blue: 0.0/255.0, alpha: 1.0) ]
        let boldString = NSMutableAttributedString(string:text, attributes: attrs)
        append(boldString)
        return self
    }
    @discardableResult func WarmGrey(_ text: String, Fontsize : CGFloat) -> NSMutableAttributedString {
        let attrs: [NSAttributedStringKey: Any] = [.font: UIFont(name: "SFUIDisplay-Regular", size: Fontsize)!, NSAttributedStringKey.foregroundColor: UIColor(red: 152.0/255.0, green: 152.0/255.0, blue: 152.0/255.0, alpha: 1.0) ]
        let boldString = NSMutableAttributedString(string:text, attributes: attrs)
        append(boldString)
        return self
    }
    }
    

    现在,您可以执行这样的函数作为全局

    func FormattedString(Orange : String, WarmGrey : String ,fontsize : CGFloat) -> NSMutableAttributedString 
    {
       let paragraphStyle = NSMutableParagraphStyle()
       paragraphStyle.alignment = .left
       paragraphStyle.lineSpacing = 1
       paragraphStyle.paragraphSpacing = 1
       let formattedString = NSMutableAttributedString()
       formattedString
        .DustyOrange(Orange, Fontsize: fontsize)
        .WarmGrey(WarmGrey, Fontsize: fontsize )
      formattedString.addAttributes([NSAttributedStringKey.paragraphStyle: paragraphStyle], range: NSRange(location: 0, length: formattedString.length))
       return formattedString
    }
    

    您可以像这样使用全球化函数

     yourLabelName.attributedText = FormattedString(Orange: "String with orange color", WarmGrey: " String with warm grey color.", fontsize: 11.5)
    

    带图像的属性文本

    func AttributedTextwithImgaeSuffix(AttributeImage : UIImage , AttributedText : String , buttonBound : UIButton) -> NSMutableAttributedString 
     {
       let fullString = NSMutableAttributedString(string: AttributedText + "  ")
       let image1Attachment = NSTextAttachment()
       image1Attachment.bounds = CGRect(x: 0, y: ((buttonBound.titleLabel?.font.capHeight)! - 
      AttributeImage.size.height).rounded() / 2, width: 
      AttributeImage.size.width, height: AttributeImage.size.height)
      image1Attachment.image = AttributeImage
      let image1String = NSAttributedString(attachment: image1Attachment)
      fullString.append(image1String)
      fullString.append(NSAttributedString(string: ""))
      return fullString 
    }
    

    您可以将“NSTextAttachment”与按钮标签一起使用,如下所示。

       yourUIButton.setAttributedTitle(AttributedTextwithImgaeSuffix(AttributeImage: desiredImage, AttributedText: "desired UIButton title", buttonBound: yourUIButton), for: .normal)
    
        5
  •  0
  •   Arunabh Das    8 年前

    在Swift 4中。x、 这应该是这样的:

            let strokeTextAttributes: [NSAttributedStringKey: Any] = [
            NSStrokeColorAttributeName: UIColor.black,
            NSForegroundColorAttributeName : UIColor.white,
            NSStrokeWidthAttributeName : -2.0,
            NSFontAttributeName : UIFont.boldSystemFont(ofSize: 18)
        ]
    
        6
  •  0
  •   user9175949 user9175949    8 年前

    如果您想更改特定的字符串值,下面的答案对您很有帮助:-

    让subStr=“你好” 让allStr=“你好,世界”

        let newStr = NSMutableAttributedString(string: allStr)
        newStr.addAttribute(kCTFontAttributeName as NSAttributedStringKey, value:  UIFont.init(customFont: .MyriadPro_R, withSize: 18)!, range: (allStr as NSString).range(of: subStr))
        newStr.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.PrimaryColor, range: (allStr as NSString).range(of: subStr))
        self.stateLbl.attributedText = newStr
    
        7
  •  0
  •   Dani Pralea dylanjha    7 年前
    let text = systolicString + " / " + diastolicString
    
    let newStr = NSMutableAttributedString(string: text)
    // I have static ranges, but you can also extract them dynamically
    let systolicRange = NSRange(location: 0, length: 2)
    let backslashRange = NSRange(location: 3, length: 1)
    let diastolicRange = NSRange(location: 5, length: 2)
    
    newStr.addAttribute(NSAttributedStringKey.font, value:  UIFont.ubuntuRegular(28), range: systolicRange)
    newStr.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor(hexString: "042f57"), range: systolicRange)
    
    newStr.addAttribute(NSAttributedStringKey.font, value:  UIFont.ubuntuLight(23), range: backslashRange)
    newStr.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor(hexString: "6485a3"), range: backslashRange)
    
    newStr.addAttribute(NSAttributedStringKey.font, value:  UIFont.ubuntuRegular(18), range: diastolicRange)
    newStr.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor(hexString: "042f57"), range: diastolicRange)
    // my UILabel
    valueLabel.attributedText = newStr