HTML format in UITextView

I am new to iOS Development and am currently working on an application that receives some kind of JSON data. But some Backend experts thought it would be better for the User if they simply copied the information directly from Word and pasted it into the information system. So I'm sitting here trying to make an interactive link in a UITableView.

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

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

I already tried UILabel, but after some research, I use the now often offered UITextView. In the Attributed Inspector, I set it as attribute text and enabled link detection. Now the text is displayed in red and is available for viewing.

Now the problem is that the HTML tags and the correct (German) character set are still missing, and I had no idea how to display it correctly.

The line shown is parsed as follows:

  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 populate a Textview with attrStr?.string , the format displays correctly, but the link also went away.

Any suggestions on the proper formatting of the displayed string?

Thanks in advance AR4G4

+8
ios format swift hyperlink uitextview
source share
8 answers

The problem is that you need to change character encoding settings from NSUnicodeStringEncoding to NSUTF8StringEncoding in order to load your html correctly. I think you should create a line-by-line extension for reading a read-only string in order to convert your html code to an attribute 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 
+25
source share

Check the attributes of your UITextView in IB. For links to work, you must have Selectable checked.

enter image description here

+2
source share

I would recommend displaying HTML in a UIWebView . It is more reliable than using UITextView . See Show html text in uitextview for more details .

+1
source share

I used the code for Swift 4:

 var descriptionStr : String = String() //Dynamic text let regex = try! NSRegularExpression(pattern: "<.*?>", options: [.caseInsensitive]) let range = NSRange(location: 0, length: descriptionStr.count) let htmlLessString: String = regex.stringByReplacingMatches(in: descriptionStr, options: NSRegularExpression.MatchingOptions(), range:range, withTemplate: "") textViewRef.text = htmlLessString 
+1
source share

After creating your attribute string, you should set the attributedText property for the UITextView as the NSAttributedString itself, and not the string property of this attribute string.

0
source share

Your versions are pretty close to the start. As Leonardo Savio Dabus said, you should try NSUTF * StringEncoding. The following produces your expected result for me. According to him, you can add it to the line extension if you do it a lot.

  let theString = "F&uuml;r mehr Informationen klicken sie <a href=\"http://www.samplelink.com/subpage.php?id=8\">here</a>." let theAttributedString = NSAttributedString(data: str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil, error: nil) theTextView.attributedText = atString 
0
source share

In another way, I used this:

 var someHtmlString = "F&uuml;r mehr Informationen klicken sie <a href=\"http://www.samplelink.com/subpage.php?id=8\">here</a>." let regex = try! NSRegularExpression(pattern: "<.*?>", options: [.CaseInsensitive]) let range = NSRange(location: 0, length: someHtmlString.characters.count) let htmlLessString: String = regex.stringByReplacingMatchesInString(someHtmlString, options: NSMatchingOptions(), range:range, withTemplate: "") 

End result -> htmlLessString

 "F&uuml;r mehr Informationen klicken sie here." 
0
source share

I had an application in which there was a UITextView, where I wanted to be able to insert HTML-formatted text from a browser, and then save it as a string (containing html formatting) into the database, and then retrieve it from the database again and show it in the same format that was first copied from the website. I managed to do this by making these two extensions:

 extension String { func getAttributedStringFromHTMLString() -> NSAttributedString { do { let attributedString = try NSAttributedString(data: self.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil) return attributedString } catch { print(error) return NSAttributedString() } } } extension NSAttributedString { func getHTMLString() -> String { var htmlText = ""; let documentAttributes = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] do { let htmlData = try self.dataFromRange(NSMakeRange(0, self.length), documentAttributes:documentAttributes) if let htmlString = String(data:htmlData, encoding:NSUTF8StringEncoding) { htmlText = htmlString } return htmlText } catch { print("error creating HTML from Attributed String") return "" } } } 
0
source share

All Articles