Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ios标签中显示html格式的文本_Html_Ios_Swift_Uiwebview_Uilabel - Fatal编程技术网

如何在ios标签中显示html格式的文本

如何在ios标签中显示html格式的文本,html,ios,swift,uiwebview,uilabel,Html,Ios,Swift,Uiwebview,Uilabel,我想在IOS中的ui标签上显示html格式化文本 在Android中,它有这样的api.setText(Html.fromHtml(somestring)) 我想知道ios中有什么/是否有类似的版本 我搜索并找到此线程: 但它建议使用UIWebView。我需要在每个表单元格中显示html格式的字符串,所以我认为每行1webview似乎有点重 还有其他选择吗 谢谢。您可以尝试属性字符串: var attrStr = NSAttributedString( data: "<

我想在IOS中的
ui标签上显示html格式化文本

在Android中,它有这样的api
.setText(Html.fromHtml(somestring))

我想知道ios中有什么/是否有类似的版本

我搜索并找到此线程:

但它建议使用
UIWebView
。我需要在每个表单元格中显示html格式的字符串,所以我认为每行1
webview
似乎有点重

还有其他选择吗


谢谢。

您可以尝试属性字符串:

var attrStr = NSAttributedString(
        data: "<b><i>text</i></b>".dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true),
        options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
        documentAttributes: nil,
        error: nil)
label.attributedText = attrStr
var attrStr=nsattributestring(
数据:“文本”。数据使用编码(NSUnicoding,allowLossyConversion:true),
选项:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],
文件属性:无,
错误:无)
label.attributeText=attrStr

Swift 2.0的

var attrStr = try! NSAttributedString(
        data: "<b><i>text</i></b>".dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
        options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
        documentAttributes: nil)
label.attributedText = attrStr
var attrStr=try!NSAttribute字符串(
数据:“文本”。数据使用编码(nsunicoding,allowLossyConversion:true)!,
选项:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],
文件属性:无)
label.attributeText=attrStr

Swift 3.0

do {
    let attrStr = try NSAttributedString(
        data: "<b><i>text</i></b>".data(using: String.Encoding.unicode, allowLossyConversion: true)!,
        options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
        documentAttributes: nil)
    label.attributedText = attrStr
} catch let error {

}
do{
让attrStr=try NSAttributedString(
数据:“文本”。数据(使用:String.Encoding.unicode,allowossyconversion:true)!,
选项:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],
文件属性:无)
label.attributeText=attrStr
}捕捉错误{
}
Swift 4
对我来说,保罗的回答奏效了。但对于自定义字体,我必须添加以下代码

//Please take care of force unwrapping
let data = htmlString.data(using: String.Encoding.unicode)! 
        let mattrStr = try! NSMutableAttributedString(
            data: data,
            options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
            documentAttributes: nil)
        let normalFont = UIFontMetrics.default.scaledFont(for: UIFont(name: "NormalFontName", size: 15.0)!)//
        let boldFont = UIFontMetrics.default.scaledFont(for: UIFont(name: "BoldFontName", size: 15.0)!)
        mattrStr.beginEditing()
        mattrStr.enumerateAttribute(.font, in: NSRange(location: 0, length: mattrStr.length), options: .longestEffectiveRangeNotRequired) { (value, range, _) in
            if let oFont = value as? UIFont{
                mattrStr.removeAttribute(.font, range: range)
                if oFont.fontName.contains("Bold"){
                    mattrStr.addAttribute(.font, value: boldFont, range: range)
                }
                else{
                    mattrStr.addAttribute(.font, value: normalFont, range: range)
                }

            }
        }

Objective-C版本:

   NSError *error = nil;
   NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:contentData
                                                    options:@{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType}
                                                    documentAttributes:nil error:&error];

这只是上述答案的Objective-C转换。以上所有答案都是正确的,参考上述答案

OHAttributedLabel可能会帮助您参考此方法在IOS8上速度非常慢谢谢!这太棒了!根据这个开发人员的经验,在iOS 8中一点也不慢。我得到一个错误:
无法使用类型为“(data:NSData?,options:[String:String],documentAttributes:_,error:_)”的参数列表调用类型为“nsAttributeString”的初始值设定项
它不适用于Swift 2.0。请参阅新答案中的解决方案。getting error“表达式类型不明确,没有更多上下文”如何更改此字符串的文本颜色?有可能改变颜色吗。我尝试添加NSForegroundColorAttributeName,但没有效果。它也支持图像吗?有人知道如何更改字体颜色和字体吗?我两个都试过了,但似乎没有其他选项可以使用对我有效的文档类型属性集。可以在示例中自由使用“let”而不是“var”。您不应该使用
try因为可能会引发异常。@fernandi Valle。如果我想要带有html标记的文本,意味着我如何实现this@UmaMadhavi什么样的html标签?因为并不是每个人都是兼容的。你好,类似于此,这对我来说非常有效!但是,有没有一种方法可以让它拾取应用程序其余部分正在使用的字体,而不必为每个或

标记定义字体系列和大小?就我而言,我只是在使用系统字体。问得好,@amyman34。我尽力提供详尽的答案。谢谢保罗,我真的很感谢你愿意帮助我!不幸的是,我不能让它工作。另外,当我添加第二组代码时,Xcode告诉我它只能在iOS11.0或更高版本中工作,并将其包装在If#available(iOS11.0,*){}条件中。。。当我构建它时,它仍然呈现我的html内容,但它没有选择应用程序的字体系列样式,这是SanFrancisco(即默认),而是将其显示为一种序列化字体,如Times。另外,不确定这是否重要,但我必须将所有这些都放在viewDidLoad函数中,否则它将无法生成。有时可能需要在addAttribute(.font,range:range)之前删除属性(.font,range:range)。iOS11兼容性可能是UIFontMetrics的要求。您可以手动计算早期版本的大小。

let newFont = UIFontMetrics.default.scaledFont(for: UIFont.systemFont(ofSize: UIFont.systemFontSize)) // The same is possible for custom font.

let mattrStr = NSMutableAttributedString(attributedString: attrStr!)
mattrStr.beginEditing()
mattrStr.enumerateAttribute(.font, in: NSRange(location: 0, length: mattrStr.length), options: .longestEffectiveRangeNotRequired) { (value, range, _) in
    if let oFont = value as? UIFont, let newFontDescriptor = oFont.fontDescriptor.withFamily(newFont.familyName).withSymbolicTraits(oFont.fontDescriptor.symbolicTraits) {
        let nFont = UIFont(descriptor: newFontDescriptor, size: newFont.pointSize)
        mattrStr.removeAttribute(.font, range: range)
        mattrStr.addAttribute(.font, value: nFont, range: range)
    }
}
mattrStr.endEditing()
label.attributedText = mattrStr
//Please take care of force unwrapping
let data = htmlString.data(using: String.Encoding.unicode)! 
        let mattrStr = try! NSMutableAttributedString(
            data: data,
            options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
            documentAttributes: nil)
        let normalFont = UIFontMetrics.default.scaledFont(for: UIFont(name: "NormalFontName", size: 15.0)!)//
        let boldFont = UIFontMetrics.default.scaledFont(for: UIFont(name: "BoldFontName", size: 15.0)!)
        mattrStr.beginEditing()
        mattrStr.enumerateAttribute(.font, in: NSRange(location: 0, length: mattrStr.length), options: .longestEffectiveRangeNotRequired) { (value, range, _) in
            if let oFont = value as? UIFont{
                mattrStr.removeAttribute(.font, range: range)
                if oFont.fontName.contains("Bold"){
                    mattrStr.addAttribute(.font, value: boldFont, range: range)
                }
                else{
                    mattrStr.addAttribute(.font, value: normalFont, range: range)
                }

            }
        }
   NSError *error = nil;
   NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:contentData
                                                    options:@{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType}
                                                    documentAttributes:nil error:&error];