UITextView 中的 HTML 格式 [英] HTML Format in UITextView

查看:16
本文介绍了UITextView 中的 HTML 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 iOS 开发很陌生,现在正在开发一个接收某种 JSON 数据的应用程序.但是一些后端专家认为,如果他们直接从Word中复制信息并将其粘贴到信息系统中,对用户来说会更好.所以我坐在这里,试图在 UITableView 中创建一个可点击的链接.

i'm quite new to iOS Development and right now working on an app which receive some kind of JSON Data. But some Backend Experts thought, that it would be better for the User if they just copy the Information straight out of Word and paste it into the information System. So I'm sitting here, trying to make a clickable Link in a UITableView.

我解析来自 Web 的数据并获得具有以下格式的字符串:

I parse the data from Web and get a String with this format:

F&uuml;r mehr Informationen klicken sie <a href="http://www.samplelink.com/subpage.php?id=8">here</a>.

我已经尝试过 UILabel,但经过一些研究,我现在使用经常推荐的 UITextView.在属性检查器中,我将其设置为属性文本并启用链接检测.现在文本显示为红色并且可以点击.

I tried already a UILabel, but after some research I use now the often suggested UITextView. In the Attributed Inspector, i set it as an Attributed Text and enabled the Link Detection. Now the text is shown red and is clickable.

我现在的问题是,仍然缺少 HTML 标签和正确的(德语)字符集,我不知道如何以正确的方式显示它.

The Problem now for me is, that the HTML Tags and the correct (German) Character Set is still missing and i got no idea, how to display it in the right way.

显示的字符串是这样解析的:

The shown string is parsed in this way:

    func showHTMLString(unformattedString: String) -> NSAttributedString{
    var attrStr = NSAttributedString(
        data: tmpString.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
        options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
        documentAttributes: nil,
        error: nil)
    return attrStr!
}

如果我用 attrStr?.string 填充 Textview,格式会以正确的方式显示,但链接也不见了.

If i fill the Textview with attrStr?.string the Format is shown in the correct way but the link is gone as well.

对于如何以正确的方式格式化显示的字符串有任何建议吗?

Any suggestions how to format the shown string in the right way?

提前致谢AR4G4

推荐答案

问题在于您必须将字符编码选项从 NSUnicodeStringEncoding 更改为 NSUTF8StringEncoding 以正确加载您的 html.我认为您应该创建一个字符串扩展只读计算属性来将您的 html 代码转换为属性字符串:

The problem there is that you have to change the Character Encoding options from NSUnicodeStringEncoding to NSUTF8StringEncoding to load your of your html the proper way. I think you should create a string extension read-only computed property to convert your html code to attributed string:

Xcode 8.3.1 • Swift 3.1

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

<小时>

let htmlStringCode = "F&uuml;r mehr Informationen klicken sie <a href="http://www.samplelink.com/subpage.php?id=8">here</a>"

htmlStringCode.data.attributedString?.string ?? ""  // "Für mehr Informationen klicken sie here"

<小时>

就你而言


in your case

yourTextView.attributedText = htmlStringCode.data.attributedString

这篇关于UITextView 中的 HTML 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆