NSAttributedString更改颜色和字体大小

问题描述 投票:0回答:1

嗨,我有个小问题,我正在使用func:

static func convertFromHTMLString(_ input: String?) -> NSAttributedString? {

        guard let input = input else { return nil }

        guard let data = input.data(using: String.Encoding.unicode, allowLossyConversion: true) else { return nil  }
        return try? NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil)
    }

从具有以下属性的Localizable.strings中读取我的字符串:

String

但是当我运行我的应用程序时,它看起来像:

App screen

这会将我的Label颜色更改为黑色,将字体大小更改为10-12; /我的标签应具有白色和字体大小17,有人知道如何解决它吗?谢谢 ! :)

html swift string nsattributedstring
1个回答
0
投票
func convertFromHTMLString(_ input: String?) -> NSAttributedString? {

    guard let input = input else { return nil }

    guard let data = input.data(using: String.Encoding.unicode, allowLossyConversion: true) else { return nil  }


    if let string = try? NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil).string
    {
        //Set color and font
        let myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.white , NSAttributedString.Key.font: UIFont(name: "Chalkduster", size: 17.0)!  ]
        let myAttrString = NSAttributedString(string: string, attributes: myAttribute)
        return myAttrString
    }


    return nil
}
© www.soinside.com 2019 - 2024. All rights reserved.