Why does my app draw 'á' as 'á' in a UILabel.
I parse the text off a webpage and then draw the text into a label.
Is there something I am missing?
Many Thanks
-Code
This is because the data in the web page is HTML entity encoded so that á is expressed asá. However, as the UILabel doesn't parse/display HTML, it simply displays the content as-is.
As such, you'd need to entity decode the data (to convert á back to á) prior to displaying it. The existing HTML character decoding in Objective-C / Cocoa Touch question/answers (and other questions it links to) should be of some assistance.
Related
I have an iOS app that is pulling data from a Restful web service. A portion of the content I am receiving is being loaded into a UITextView. The portion that will be going into the text view is coming in as HTML format. I need to convert it from HTML to plain text while using the paragraph tags to format the text view properly.
Here is what the HTML format looks like
<p data-seq="1"><span class="paragraph">Content of paragraph 1</span></p><p data-seq="2"><span class="paragraph">Content of paragraph 2</span></p>
You can see that <p data-seq="2"><span class="paragraph">....</span></p> designates the start and end of the paragraph.
I initially tried using NSScanner from this example, How to convert NSString HTML markup to plain text NSString?. This was quick to implement but it strips all tags and and parses the text as one long paragraph.
I have added libXml2 to my code. I started following this tutorial for implementation but after I started working through it I wasn't sure how to format the output into paragraphs.
I have also seen recommendations for the DTCoreText library but I didn't see a lot of info on it.
Could someone possibly throw up a snippet using any of the above three options or one of their own on how to parse html into plain text while maintaining the paragraphs?
SOLUTION
Per lxt's recommendation I investigated DTCoreText. Once I managed to get it installed in my app (definitely recommend cocoa pods for that). It was easy as #import "DTCoreText.h" in my detailViewController and then the lines below to add it to the UITextView.
NSDictionary *options = #{DTUseiOS6Attributes: [NSNumber numberWithBool:YES]};
NSData *htmlData = [self.htmlString dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *stringArticle = [[NSAttributedString alloc] initWithHTMLData:htmlData options:options documentAttributes:NULL];
self.newsDetailText.attributedText = stringArticle;
The first build failed because I didn't include the DTUseiOS6Attributes line. The second build succeeded and the detail view was perfectly formatted. It was a fist pump moment! Thanks again for the recommendation lxt!
I would honestly recommend using DTCoreText rather than writing your own parser. There's no real benefit reinventing the wheel, and it's also a widely used library with a large user base.
I am surprised you had trouble finding info about it, the library has very good documentation available, and the author is also pretty active on Twitter (#cocoanetics).
You can use the nifty DTAttributedTextView class provided in place of your UITextView. The library also provides a category that extends NSAttributedString with a initWithHTMLData:documentAttributes: method. This will let you create your attributed string and plug it into your view. It's really no more than a couple of lines of code.
I have an app that retrieves a user's stream and puts it into a UITableView. However, some include emojis (those little smile faces in iOS), and they just return as boxes in my UILabel.
I've done some research, and I still can't figure it out. Does the text type need to be changed?
Thanks!
Please explain your problem better,from the piece that i understood,notice that emojis have native support in iPhone,so any app can display an emoji in a text box,i recommend you to check the methods you're using to add text to the uitableview and the text encoding from the string which is being displayed,also you could check this link
use NSString drawinrect to display emoji instead of drawGlyphsForGlyphRange.
In my iPhone application I'm parsing RSS feed to get html and keep it. Afterwards I display this html in UIWebView. The problem is that html contains urls of images, if there is network connection everything is ok, UIWebView loads and displays these images, but if there is no connection it shows text but in place of images shows frame with blue square inside
How can I get rid of it?
Thank you
Check programmatically to make sure that you have an internet connection.
See this question and its accepted answer on how to do that.
If you don't have an internet connection, then you have a couple of options. You can either:
Parse through your html, replace the <img> tags with blanks (this will completely get rid of the images and their associated blue question mark boxes.
Parse through your html, replace the src part of <img src="somewebsite"> with a reference to an image placeholder in the project bundle.
XPath is your friend when it comes to parsing html. Although if you wanted to, you could do all of this with NSSTring.
I'm using NSXMLParser in iphone app that I'm working on.
Later I'm displaying the text in a view.
All is well when I'm using english language in my XML.
But my XML is in Herbrew language. I'm not able to read the text properly and display it.Please advice me what change do I've to make in XML.
if the XML file is in UTF-8 and you are decoding it using NSUTF8Encoding you should have no problems.
when displaying the strings in UI, remember to set the correct alignment, or the right-to-left will not look correct.
I have a text which looks like the following,
the url of the page is http://www.myurl.com, and the phone # is (999)999-9999, blah blah blah...
And I want to show it in a way such that the URL and the phone # are both in different color and bolded. Can I do it using just one UILabel control, or I need to parse them out and put them onto separate UILabel controls. (Note that the text itself could span multiple lines.) How can I do it?
Sorry I forgot to mention that this is for iPhone (CoCoa Touch), where the NSAttributedString is not available.
You might want to check out TTTAttributedLabel. It supports automatic data detection for things like Phone Numbers and URLs, as well as mixed styles with NSAttributedStrings.
You could use an NSAttributeString to decorate your string. There is a good explanation of how to use them here Change the background color of a substring of a drawn NSString?
You could try using a UIWebView, which detects both URLs and phone numbers.