Display html text in uitextview - ios

How can I display HTML text in textview?
For example,
string <h1>Krupal testing <span style="font-weight:
bold;">Customer WYWO</span></h1>
Suppose text is bold so it display in textview as bold string
but I want display normal text. Is this possible in the iPhone SDK?

Use following block of code for ios 7+.
NSString *htmlString = #"<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p><img src='http://blogs.babble.com/famecrawler/files/2010/11/mickey_mouse-1097.jpg' width=70 height=100 />";
NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options: #{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes: nil
error: nil
];
textView.attributedText = attributedString;

For Swift 4, Swift 4.2: and Swift 5
let htmlString = """
<html>
<head>
<style>
body {
background-color : rgb(230, 230, 230);
font-family : 'Arial';
text-decoration : none;
}
</style>
</head>
<body>
<h1>A title</h1>
<p>A paragraph</p>
<b>bold text</b>
</body>
</html>
"""
let htmlData = NSString(string: htmlString).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
let attributedString = try! NSAttributedString(data: htmlData!, options: options, documentAttributes: nil)
textView.attributedText = attributedString
For Swift 3:
let htmlString = """
<html>
<head>
<style>
body {
background-color : rgb(230, 230, 230);
font-family : 'Arial';
text-decoration : none;
}
</style>
</head>
<body>
<h1>A title</h1>
<p>A paragraph</p>
<b>bold text</b>
</body>
</html>
"""
let htmlData = NSString(string: htmlString).data(using: String.Encoding.unicode.rawValue)
let attributedString = try! NSAttributedString(data: htmlData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
textView.attributedText = attributedString

Use a UIWebView on iOS 5-.
On iOS 6+ you can use UITextView.attributedString, see https://stackoverflow.com/a/20996085 for how.
There's also an undocumented -[UITextView setContentToHTMLString:] method. Do not use this if you want to submit to AppStore.

BHUPI's answer is correct, but if you would like to combine your custom font from UILabel or UITextView with HTML content, you need to correct your html a bit:
NSString *htmlString = #"<b>Bold</b><br><i>Italic</i><p> <del>Deleted</del><p>List<ul><li>Coffee</li><li type='square'>Tea</li></ul><br><a href='URL'>Link </a>";
htmlString = [htmlString stringByAppendingString:#"<style>body{font-family:'YOUR_FONT_HERE'; font-size:'SIZE';}</style>"];
/*Example:
htmlString = [htmlString stringByAppendingString:[NSString stringWithFormat:#"<style>body{font-family: '%#'; font-size:%fpx;}</style>",_myLabel.font.fontName,_myLabel.font.pointSize]];
*/
NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options: #{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes: nil
error: nil
];
textView.attributedText = attributedString;
You can see the difference on the picture below:

You can have a look the OHAttributedLabel classes, I used these to overcome this kind of problem with my textField. In this they have overridden the drawRect method to obtain the required style.
https://github.com/AliSoftware/OHAttributedLabel

My first response was made before iOS 7 introduced explicit support for displaying attributed strings in common controls. You may now set attributedText of UITextView to an NSAttributedString created from HTML content using:
-(id)initWithData:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict error:(NSError **)error
- initWithData:options:documentAttributes:error: (Apple Doc)
Original answer, preserved for history:
Unless you use a UIWebView, your solution will rely directly on CoreText. As ElanthiraiyanS points out, some open source projects have emerged to simplify rich text rendering. I would recommend NSAttributedString-Additions-For-HTML (Edit: the project has been supplanted DTCoreText), which features classes to generate and display attributed strings from HTML.

Answer has fitted to me that from BHUPI.
The code transfer to swift as below:
Pay attention "allowLossyConversion: false"
if you set the value to true, it will show pure text.
let theString = "<h1>H1 title</h1><b>Logo</b><img src='http://www.aver.com/Images/Shared/logo-color.png'><br>~end~"
let theAttributedString = try! NSAttributedString(data: theString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
UITextView_Message.attributedText = theAttributedString

For Swift3
let theString = "<h1>H1 title</h1><b>Logo</b><img src='http://www.aver.com/Images/Shared/logo-color.png'><br>~end~"
let theAttributedString = try! NSAttributedString(data: theString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
UITextView_Message.attributedText = theAttributedString

For some cases UIWebView is a good solution. Because:
it displays tables, images, other files
it's fast (comparing with NSAttributedString: NSHTMLTextDocumentType)
it's out of the box
Using NSAttributedString can lead to crashes, if html is complex or contains tables (so example)
For loading text to web view you can use the following snippet (just example):
func loadHTMLText(_ text: String?, font: UIFont) {
let fontSize = font.pointSize * UIScreen.screens[0].scale
let html = """
<html><body><span style=\"font-family: \(font.fontName); font-size: \(fontSize)\; color: #112233">\(text ?? "")</span></body></html>
"""
self.loadHTMLString(html, baseURL: nil)
}

You can also use one more way. Three20 library offers a method through which we can construct a styled textView. You can get the library here: http://github.com/facebook/three20/
The class TTStyledTextLabel has a method called textFromXHTML: I guess this would serve the purpose. But it would be possible in readonly mode. I don't think it will allow to write or edit HTML content.
There is also a question which can help you regarding this: HTML String content for UILabel and TextView
I hope its helpful.

NSDoc save the text file in a string to an html file then simultaneously load it into a webview that is in the same place as your UITextView..

Related

UITextView or UILabel with html hyperlinks

I need text with multiple tapeable links init.
In Android I just used simple html string for that
Example:
"Photo The owner / CC BY-SA 3.0"
And it worked only when I clicked on words corresponding links.
I spent a day and did not manage to find simple solution for ios.
I tried to use extension for UITextView :
extension UITextView {
func setHTMLFromString(text: String) {
let modifiedFont = NSString(format:"<span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%#</span>" as NSString, text)
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
let attrStr = try! NSAttributedString(
data: modifiedFont.data(using: String.Encoding.unicode.rawValue, allowLossyConversion: true)!,
options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
self.attributedText = attrStr
}
But links work not only when I click on the hyperlink world but also when I click on simple text between. And I did not manage to align this text in textView(center) as well.
Does anybody know correct way?
Can't find documentation at the moment, but close observation indicates:
Because tapping with a finger is not as precise as clicking with a mouse pointer, the link gets a larger "tap region" than the text itself.
With your example - "Photo The owner / CC BY-SA 3.0" - I can tap on the ow characters without triggering either link. If my tap is on The it triggers the Photo link, if my tap is on new / it triggers that link.
Edit
One option to center the text...
In your extension, change
let modifiedFont = NSString(format:"<span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%#</span>" as NSString, text)
to
let modifiedFont = NSString(format:"<p style=\"text-align:center\"><span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%#</span></p>" as NSString, text)
added the <p style=... tag.

Attributed text swift how to retain font color with <font color=.." tag

I'm trying to use Android's resources in iOS app.
I have a text in xml:
<string name="myText"><![CDATA[<h2><font color="#aa2c2c">Some RED header text</font></h2><br /><br />
<font color="#aa2c2c">Red again</font>then back to normal <br /><br />]]></string>
I'm implementing night/day option for text. The thing is - when I change textView.textColor to black or white - it changes everything including red text.But I want red to remain red and unattributed text to be what I set it to be programmatically.
In Android textView.setTextColor method for Spanned text changes only unattributed text. I want the same thing in iOS
this is how the text is set :
let text = try! NSAttributedString(
data: resources.getString(resId).dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
tv.attributedText = text;

NSAttributeString battling css scripting already in a string

Okay I'm having an issue with NSAttributeString. I'm getting different html/css strings that occupy "busDescriptio" depending on what business you choose from a website database. I'm able to customize the string as I see fit as NSAttributeString but unfortunately on some cases the string already has css scripting in it which overrides my style that I insert into the string. Is it possible to override the scripting that is within my string? If so, how would I be able to do this? ***If I cant override the script can I just extract a certain tag from my string or replace it? Oh here is what my string looks like. As you see their is a style that is being poppulated within the string aka(busDescriptio). I'm not able to change that with regular scripting using NSAttributeString.
/* This is a random description my busDescriptio pulls in which changes everytime someone selects a different business*/<p><span style="color:rgb(0, 0, 0); font-family:verdana,arial,tahoma; font-size:12px">Baxter Eye Care has been serving The Woodlands with quality eye care and personal friendly service since 1981. Dr. Baxter, Dr. Daniels and Dr. Shosa are dedicated to your eye health and vision needs.</span></p>
This is the code I'm using to do this
extension String {
var html2String:String {
return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
}
var html2NSAttributedString:NSAttributedString {
return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!
}
}
extension NSData{
var htmlString:String {
return NSAttributedString(data: self, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
}
}
let result = html2String("\(busDescriptio)") // Business Description HTML
let yourAttributedText = "<style type=\"text/css\">#busDescriptio{color:white;align-content:left;}#green{color:#0F0}#blue{color: #00F; font-weight: Bold; font-size: 32}</style><span id=\"busDescriptio\" >\(result),</span><span id=\"green\" > Green </span><span id=\"blue\">and Blue</span>".html2NSAttributedString
// Create UITextView
var textView = UITextView(frame: CGRectMake(0, 95.0, screenWidth-10, 300.0))
textView.attributedText = yourAttributedText
textView.backgroundColor = UIColor.clearColor()
textView.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
border.addSubview(textView)
func html2String(html:String) -> String {
return NSAttributedString(data: html.dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
}
I think you're stuck thinking about HTML. Once you have created an NSAttributedString or, more accurately, an NSMutableAttributedString from the HTML, you can just apply your own attributes. At that point, the HTML is no longer relevant. You certainly shouldn't try to achieve formatting by manipulating the HTML before converting it into an attributed string.
You don't say what "style" or attributes you want to change, so it's hard to give you an example, but just set or remove whatever attributes you want. You have the power to completely override whatever styling was introduced by the CSS in the HTML.
For example, do [someMutableAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, someMutableAttributedString.length)] to change the color to blue.
Here's some Objective-C code which works for me on OS X:
NSString* html = #"<style type=\"text/css\">#busDescriptio{color:white;align-content:left;}#green{color:#0F0}#blue{color: #00F; font-weight: Bold; font-size: 32}</style><span id=\"busDescriptio\" ><p><span style=\"color:rgb(0, 0, 0); font-family:verdana,arial,tahoma; font-size:12px\">Baxter Eye Care has been serving The Woodlands with quality eye care and personal friendly service since 1981. Dr. Baxter, Dr. Daniels and Dr. Shosa are dedicated to your eye health and vision needs.</span></p>,</span><span id=\"green\" > Green </span><span id=\"blue\">and Blue</span>";
NSData* data = [html dataUsingEncoding:NSUTF8StringEncoding];
NSMutableAttributedString* str = [[NSMutableAttributedString alloc] initWithData:data
options:#{ NSCharacterEncodingDocumentOption: #(NSUTF8StringEncoding),
NSDocumentTypeDocumentOption: NSHTMLTextDocumentType }
documentAttributes:NULL
error:NULL];
[str addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0, str.length)];
self.label.attributedStringValue = str;
I don't work in Swift, but here's a pass at a simple translation:
let html = "<style type=\"text/css\">#busDescriptio{color:white;align-content:left;}#green{color:#0F0}#blue{color: #00F; font-weight: Bold; font-size: 32}</style><span id=\"busDescriptio\" ><p><span style=\"color:rgb(0, 0, 0); font-family:verdana,arial,tahoma; font-size:12px\">Baxter Eye Care has been serving The Woodlands with quality eye care and personal friendly service since 1981. Dr. Baxter, Dr. Daniels and Dr. Shosa are dedicated to your eye health and vision needs.</span></p>,</span><span id=\"green\" > Green </span><span id=\"blue\">and Blue</span>"
let data = html.dataUsingEncoding(NSUTF8StringEncoding)!
var str = NSMutableAttributedString(data:data,
options:[ NSCharacterEncodingDocumentOption: NSUTF8StringEncoding,
NSDocumentTypeDocumentOption: NSHTMLTextDocumentType ],
documentAttributes:nil,
error:nil)
str.addAttribute(NSForegroundColorAttributeName, value:NSColor.redColor(), range:NSMakeRange(0, str.length))
self.label.attributedStringValue = str;
So, the code starts with HTML code in a string. It creates an NSMutableAttributedString from that. Then it changes the foreground color, effectively replacing the color resulting from the HTML. Finally, it sets the attributed string as the content of an NSTextField label. For iOS, you could use a UILabel or UITextView or whatever.
In the code you put in your question, there are some things that are troublesome. It's not clear what busDescription is. Is it a string containing HTML code?
Is there a reason that you're interpolating it into a string to pass it to html2String()? That is, why this:
let result = html2String("\(busDescriptio)")
and not this:
let result = html2String(busDescriptio)
?
It seems that html2String() interprets HTML into an attributed string and then just extracts the plain text from that. Why are you doing that?
You are then interpolating that plain text into another block of HTML code. Again, why are you doing that? If you want to apply colors, fonts, etc. to the plain text string, just build an attributed string directly from that plain text string — no HTML involved — and apply the desired attributes.
Or, start from the original busDescriptio, make a mutable attributed string from that HTML code, and apply whatever attributes you like to that.
For example, here's another example:
NSString* busDescriptio = #"<p><span style=\"color:rgb(0, 0, 0); font-family:verdana,arial,tahoma; font-size:12px\">Baxter Eye Care has been serving The Woodlands with quality eye care and personal friendly service since 1981. Dr. Baxter, Dr. Daniels and Dr. Shosa are dedicated to your eye health and vision needs.</span></p>";
NSData* data = [busDescriptio dataUsingEncoding:NSUTF8StringEncoding];
NSMutableAttributedString* foo = [[NSMutableAttributedString alloc] initWithData:data
options:#{ NSCharacterEncodingDocumentOption: #(NSUTF8StringEncoding),
NSDocumentTypeDocumentOption: NSHTMLTextDocumentType }
documentAttributes:NULL
error:NULL];
[str addAttribute:NSFontAttributeName value:[NSFont fontWithName:#"Verdana" size:12] range:NSMakeRange(0, str.length)];
Basically, although your initial input is HTML code, you build a mutable attributed string from that and then don't work in HTML from that point on. Just apply and/or remove attributes from the attributed string, as desired. You can also build separate attributed strings, like your green or blue text in your example, and append those to the mutable one.
Okay so using what Ken Thomases wrote I was able to make it work! After looking at his code and seeing what I did wrong I was able to make it work. Here it is in Swift code just in case anyone has the same problem I had. I'm still having trouble with some images but ideally everything from font color, size, height, alignments and background work! Thanks
// busDescriptio is being occupied by a string with html and css scripting
var data: NSData = busDescriptio.dataUsingEncoding(NSUTF8StringEncoding)!
var foo: NSMutableAttributedString = NSMutableAttributedString(data: data,
options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding],
documentAttributes:nil,
error:nil)!
var paragraph = NSMutableParagraphStyle()
paragraph.alignment = NSTextAlignment.Left
foo.addAttribute(NSFontAttributeName, value: UIFont(name: "Arial", size:12)!, range:NSMakeRange(0, foo.length))
foo.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSRange(location:0,length: foo.length))
foo.addAttribute(NSBackgroundColorAttributeName, value: UIColor.clearColor(), range: NSRange(location: 0, length: foo.length))
foo.addAttribute(NSParagraphStyleAttributeName, value:paragraph, range:NSMakeRange(0, foo.length))

Using MMMarkdown, only system font will show bold or italicized text

I'm using MMMarkdown and when I apply no font to UILabel or UITextView (I have to keep UITextView so TTTAttributedLabel won't do), all of markdown works. When I give the label or textview a font, I only get markdown hyperlinks to work. I tried switching to the AttributedMarkdown library but hyperlinks don't work at all with that one.
In my Markdown Class:
class Markdown: NSObject {
func markdownString(stringForVideoDescription:NSString) -> NSMutableAttributedString {
var options = [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType]
var error:NSError?
var markdown:NSString = stringForVideoDescription
var html:NSString = MMMarkdown.HTMLStringWithMarkdown(markdown, error: &error)
var markdownAttributedString:NSMutableAttributedString = NSMutableAttributedString(data: html.dataUsingEncoding(NSUTF32StringEncoding)!, options: options, documentAttributes: nil, error: &error)!
if let font: UIFont = UIFont(name: "Marion-Regular", size: 14) {
markdownAttributedString.addAttributes([NSUnderlineStyleAttributeName:NSUnderlineStyleAttributeName], range: NSMakeRange(0, markdownAttributedString.length))
}
println(html) // used to see that markdown is in fact working
return markdownAttributedString
}
In my view, I'm actually pulling from json api but I replace to test with:
let bodyText = "\*This* sample \[I'm an inline-style link](https://www.google.com)"
myTextView.attributedText = Markdown().markdownString(bodyText)
Do I need to set up css to specify font for each bold, italicized text? Any help is greatly appreciated as my designers don't want to use the default font of Times New Roman.

How to make particular word colored : coming from localized string file of iOS

I would like to make particular word colored like this :
Text in English :-
Same place text in French :-
These text are came from localized string file. And I found solution with the string range property NSMakeRange(0,3) for attributed text from here. This I don't want since text will differ for Localized language string. FYI, I placed text in UITextView.
So how can I achieve this by simple way? Is there any solution that can fix problem in Localized string file itself.
I know this answer is a few years late, but I solved the problem by allowing HTML in my localizable strings. For example:
"emergency" = "This is a test of the <span style=\"color:red\">emergency</span> broadcast system.";
Of course, this alone won't solve the problem, because the localizable string files in IOS don't natively support any sort of HTML/XML annotation. So, you need to process the HTML yourself when you extract the string in your swift code. Here is how I did it:
// Get the localized string
let localized = NSLocalizedString("emergency", comment: "")
// Prepend an html header to the string select the primary font and color.
// The settings on the UITextView or UILabel will be ignored, so you need to do this
let htmlString = "<span style=\"color:yellow;font-size:24px;font-family:-apple-system\">" + localized
// perform the magic to turn the html string into an NSAttributedString
let data = NSString(string: htmlString).data(using: String.Encoding.utf8.rawValue)
do {
let attributedString = try NSAttributedString(data: data!,
options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
// Show the NSAttributedString in your help UITextView (or anywhere else)
help.attributedText = attributedString
} catch {
print("There was a problem")
}
This example gives you yellow, 24-point, apple system font text. The localizable string marked the word "emergency" as red, and it will be displayed in red in the final display.
Below snippet might be used for achieving yourrequirement
UITextView *textView = //YourTextView
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString: textView.attributedText];
[attributedText addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:[textView.text rangeOfString:blueString]];
[attributedText addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:[textView.text rangeOfString:redString]];
[textView setAttributedText: attributedText];

Resources