首页 文章

更改attributedString的各个字体属性 .

提问于
浏览
0

在我的应用程序中,我将获得格式为的html字符串

<p style = "text-align: center;"><strong>"some text in here</strong></p>

我知道如何使用以下函数将此html字符串转换为attributedString .

[[NSAttributedString alloc] initWithData:[aboutData dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];

但问题是,我希望保留所获得的AttributedString的所有属性,但能够更改此获得的attributionString的字体大小,字体系列和段落样式 .

例如,如果我从这个html获得一个粗体的attributionString,我怎么能改变这个特定字符串的字体大小或字体系列而不改变它的粗体状态?

我怎么做?

谢谢!

1 回答

  • 0
    text.enumerateAttributes(
       in: NSRange.init(location: 0, length: text.length), 
       options: NSAttributedString.EnumerationOptions(rawValue: 0)
    ) { (attributes, range, pointer) in
        //GET ATTRIBUTES HERE
        if let font = attributes["NSFont"] as? UIFont {
    
        }    
    }
    

    这应该可以获得所有属性 . 可能是解析它们的更好方法,但这对你来说是一个好的开始 .

相关问题