Swift 4 Label属性

问题描述 投票:12回答:7

我正在从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)
ios swift uilabel nsattributedstring swift4
7个回答
30
投票

@ Larme关于不需要.rawValue的评论是正确的。

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

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

这也摆脱了重复的NSAttributedString.Key.


15
投票

Swift 4.0+中,属性字符串接受json(字典)与密钥类型NSAttributedStringKeyNSAttributedString.Key

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

Swift 4.1及以下 - qazxsw poi& Swift 4.2及以上版本 - qazxsw poi

Swift 4.2

Swift 4.2中[NSAttributedStringKey : Any]的Initialiser更改为[NSAttributedString.Key : Any]

AttributedString

这是示例工作代码。

[NSAttributedString.Key : Any]?

Swift 4.0

Swift 4.0中public init(string str: String, attributes attrs: [NSAttributedString.Key : Any]? = nil) 的Initialiser更改为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)

AttributedString

这是示例工作代码。

[NSAttributedStringKey : Any]?

请查看此Apple文档,了解更多信息:public init(string str: String, attributes attrs: [NSAttributedStringKey : Any]? = nil)


1
投票

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) NSAttributedString - Creating an NSAttributedString Object类型

NSAttributedStringKey.strokeColor.rawValueString类型

所以它无法将NSAttributedStringKey.strokeColor转换为NSAttributedStringKey。你必须使用如下:

String

0
投票

Swift 4归因于多种颜色的文本

NSAttributedStringKey

现在你可以执行类似这样的函数作为全局函数

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

您可以使用这样的全球化功能

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
}

你可以使用“NSTextAttachment”和你的按钮标签。

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

0
投票

在Swift 4.x中,这应该是这样的:

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 
}

0
投票

如果你想改变特定的字符串值,以便以下答案对你有帮助: -

让subStr =“Hello”让allStr =“Hello World”

   yourUIButton.setAttributedTitle(AttributedTextwithImgaeSuffix(AttributeImage: desiredImage, AttributedText: "desired UIButton title", buttonBound: yourUIButton), for: .normal)

0
投票
        let strokeTextAttributes: [NSAttributedStringKey: Any] = [
        NSStrokeColorAttributeName: UIColor.black,
        NSForegroundColorAttributeName : UIColor.white,
        NSStrokeWidthAttributeName : -2.0,
        NSFontAttributeName : UIFont.boldSystemFont(ofSize: 18)
    ]
© www.soinside.com 2019 - 2024. All rights reserved.