NSAttributedString更改颜色和字体大小

时间:2020-01-28 11:15:58

标签: html swift string nsattributedstring

嗨,我有个小问题,我正在使用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

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

@ Edit1 解决方案必须如下所示 Android version 这就是在Android上的外观。

1 个答案:

答案 0 :(得分: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
}
相关问题