2017-04-20 60 views
0

在我的应用程序,我将在更改归属字符串的单个字体属性。

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

我知道如何使用下面的函数这个网站字符串转换为构造一个AttributedString格式得到一个HTML字符串。

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

但问题是,我希望保留获得AttributedString的所有属性,但可以改变任何字体大小,字体,这达到attributedString的段落样式。

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

我该怎么做?

谢谢!

+0

http://stackoverflow.com/questions/19921972/parsing-html-into-nsattributedtext-how-to-set-font http://stackoverflow.com/questions/25401742/apply-custom-font-to-归属字符串,它转换从HTML字符串http://stackoverflow.com/questions/41412963/swift-change-font-on-an-html-string-that-has-its-own-styles/41413014 #41413014 – Larme

回答

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 { 

    }  
} 

这应该得到你所有的属性。可能是解析它们的更好方法,但这对你而言可能是一个好的开始。

+0

=>'attributes [“NSFont”]'=>'attributes [NSFontAttributeName]',no? – Larme

+0

我在调试器中检查过,只有“NSFont” – Luzo

+1

'NSFontAttributeName'是iOS定义的一个常量,现在等于'NSFont'。但是在未来,如果苹果改变它的价值为iOS的'UIFont'和'NSFont'为OSX,这可能是有道理的,你的代码将不再工作。当你自己添加属性时,你可以使用'NSFontAttributeName' / – Larme