Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

ios - HTML Format in UITextView

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.

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>.

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.

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!
}

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?

Thanks in advance AR4G4

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...