可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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ü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
回答1:
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ü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
回答2:
Check the attributes of your UITextView in IB. In order for the links to work, you must have Selectable
checked.
回答3:
I would recommend displaying HTML in a UIWebView
. It is more robust than using a UITextView
. See Display html text in uitextview for more information.
回答4:
I used 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
回答5:
Having created your attributed string, you would then set the attributedText
property of the UITextView
to be the NSAttributedString
itself, not the string
property of that attributed string.
回答6:
Your versions is pretty close to begin with. As Leonardo Savio Dabus stated you should probably try NSUTF*StringEncoding. The following produces your expected output for me. As he said, you might want to add it to an extension of string, if you are doing this a lot.
let theString = "Fü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
回答7:
Another way I used to do this :
var someHtmlString = "Fü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 is
"Für mehr Informationen klicken sie here."
回答8:
I had an app that had a UITextView where I wanted to be able to paste html formatted text from the browser and then save it as a string(containing html formatting) to the database, and then another time retrieve it from the database and show it with the same format as it was first copied from the website.
I managed 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 ""
}
}
}