更改NSAttributedString的fontsize和字体颜色

时间:2017-08-30 06:16:00

标签: ios swift nsattributedstring

如何在swift中更改NSAttributedString的字体大小和字体颜色?

 @IBOutlet weak var detailsTextView: UITextView!

 if let data = htmltext.data(using: String.Encoding.utf8, allowLossyConversion: true)
 {
        do
        {
            let newFont =  UIFont(name: "Georgia", size: 22.0) ?? UIFont()    
            let attributedText = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)            
            detailsTextView.attributedText = attributedText
        }
        catch{}
}

htmltext是需要设置为UILabel的html值

5 个答案:

答案 0 :(得分:0)

你就是这样做的:

table(stack(setNames(strsplit(as.character(df$type), ","), df$year))[2:1])
#        values
#ind    a b c d e f g h i
#  2012 2 1 3 2 1 1 0 0 0
#  2013 4 1 1 1 0 0 0 0 0
#  2014 1 1 0 0 1 0 2 1 1

答案 1 :(得分:0)

这是设置NSAttributedString的属性的方法:

add_action( 'after_setup_theme', 'register_theme_menu' );
function register_theme_menu() {
  register_nav_menu( 'primary', __( 'Primary Menu', 'theme-slug' ) );
}

答案 2 :(得分:0)

使用此

let newAttributedString = NSMutableAttributedString(attributedString: label.attributedText)

// Enumerate through all the font ranges
newAttributedString.enumerateAttribute(NSFontAttributeName, in: NSMakeRange(0, 
newAttributedString.length), options: []) { value, range, stop in
guard let currentFont = value as? UIFont else {
    return
}

// An NSFontDescriptor describes the attributes of a font: family name, face name, point size, etc.
// Here we describe the replacement font as coming from the "Hoefler Text" family
let fontDescriptor = currentFont.fontDescriptor.addingAttributes([UIFontDescriptorFamilyAttribute: "Hoefler Text"])

// Ask the OS for an actual font that most closely matches the description above
if let newFontDescriptor = fontDescriptor.matchingFontDescriptors(withMandatoryKeys: [UIFontDescriptorFamilyAttribute]).first {
    let newFont = UIFont(descriptor: newFontDescriptor, size: currentFont.pointSize)
    newAttributedString.addAttributes([NSFontAttributeName: newFont], range: range)
}
label.attributedText = newAttributedString
 }

enter image description here

答案 3 :(得分:0)

Swift 5.2

String扩展名中添加以下属性:

extension String
{
    var htmlToAttributedString: NSAttributedString?
    {
        guard let data = data(using: .utf8) else { return nil }
        do
        {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = .right

            let content = try NSMutableAttributedString(data: data, options: [.documentType:     NSAttributedString.DocumentType.html, .characterEncoding:     String.Encoding.utf8.rawValue], documentAttributes: nil)

            content.addAttributes([NSAttributedString.Key.paragraphStyle: paragraphStyle,
                                   NSAttributedString.Key.font: UIFont.alJazeera(.regular(20)),
                                   NSAttributedString.Key.foregroundColor: UIColor.appDark],
                                  range: NSMakeRange(0, content.length))

            return content
        }
        catch
        {
            return nil
        }
    }
}

然后轻松使用它:

textView.attributedText = myText.htmlToAttributedString

注意:UIFont.alJazeera(.regular(20))是在UIFont扩展名中定义的静态方法,而UIColor.appDark也是在UIColor扩展名中定义的静态属性

答案 4 :(得分:-1)

Swift 3.0

你可以这样试试,

let finaltext = "<span style='font-family:Georgia; font-size: 20px'>\(Your html string)</span>"
detailsTextView.attributedText = finaltext.html2AttributedString

并添加扩展程序

extension String {
    var html2AttributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: Data(utf8), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            print(error)
            return nil
        }
    }
 }