Escape bad characters for iOS UILabel? - ios

I'm feeding some NSString data (forum posts queried from my website) into a UILabel as part of my first app. Thing is, sometimes, depending on the content of the post, the Label goes entirely blank. I've tinkered enough to discern that there are certain characters that cause the problem, but I can't quite pin down the full set.
Is there a collected list of character types to watch out for with this kind of thing? And even better, is there a method for escaping them, or automatically converting them into something more acceptable?
Thank you for helping out a n00b!

Looks like you have whitespace or new line in your string:
Try this:
NSString* labelText = [stringFromWebsite stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]

I am trying to give you some clue even you haven't put piece of code here in your question.
You should firstly try to encode that coming String by using appropriate method like encoding:NSUTF8StringEncoding and still and try to set decoded string to your UIlabel.
Another alternative is that you can create some regex for filtering purpose of that coming string. you can find many of similar thread over the google.

Related

Compare variable to text disregarding caps [duplicate]

Here is the thing. I am trying to convert a string in lowercase in Lua, but it's not working. I have done this
String = String:lower()
but it doesn't like it. I am sure that is the way to do it, I've seen it done before. A few sites suggest it might be a problem caused by a wrong version of the interpreter.
Any ideas?
You're right, this is one of the ways to do it. It would only not work and throw errors if your "String" variable is not a string.
Personally, i usually prefer to use something like..
myString = string.lower(myString)
But its really the same as doing
myString = myString:lower()
assuming that myString is actually a string, however.
The "long" version has one advantage, it actually works if myString is a number, while the second one errors in that case.

\u0092 is not printed in UILabel

I have a local json file with some descriptions of an app and I have found a weird behaviour when parsing \u0092 and \u0091 characters.
When json file contains these characters, the corresponding parsed NSString is printed like "?" and in UIlabel it dissapears completely.
Example "L\u2019H\u00e9r." is showed as "LHér." instead of "L'Hér."
If I replace this characters with \u2019, then I can see the caracter ' in UILabel
Does anybody any clue about this?
EDIT: For the moment I will substitute both of them with character \u2019, it is also a ' and there is no problem confusing it with a control character. Thank you all!
This answer is a little speculative, but I hope it gets you on the right tracks.
Your best bet may be to give up and substitute \u0091 and \u0092 for something else as a preprocessing step before string display. These are control characters and are unprintable in most encodings. But:
If rest of the file is proper UTF, your json file probably has problems: encoding is wrong (CP-1250?) while you read the file as UTF, some error has been made when converting the file, or a similar issue. So another solution is of course fixing your file.
If you're not sure about how your file is encoded, it may simply be encoded in CP-1250 - so reading the file using NSWindowsCP1250StringEncoding might fix your problem.
BTW, if you hardcode a string #"\u0091", you'll get a compilation time error Universal character name refers to a control character. Yes, not even a warning, it's that much unprintable in Unicode ;)

Formatting NSString for superscript and subscript

I am writing a utility app for some coworkers. The app is essentially a custom notepad, with buttons that represent the shorthand they use to transcribe a task. All of the buttons add a string to arrays that I have set up to hold the transcript, and I add the strings to the row arrays like this.
[currentRow addObject:#"("];
Some of the shorthand needs to be written in subscript, and some in superscript. There are not Unicode characters for all of the characters that I need, so I have been trying to sort through the code around Attributed Strings,but I'm not quite getting it. Does anyone have advice on this or some sample code?
Also, after this transcript is printed to the screen during transcription, I send it to an email message body.. so I assume I'll need to worry about formatting there as well. I am currently using plain text, but the email could be HTML. Thanks!
If you display the text in a WebView you can use html tags to set superscript. It also has the advantage to run on older iOS versions and you can reuse the text in your mail.
NSString *myText=#"This text contains <sub>subscript</sub> and <sup>superscript</sup> text.";
[self.myWebView loadHTMLString:myText baseURL:nil];

iOS: How to avoid special characters being entered in a search bar?

I may be looking at this incorrectly, but right now, if a user enters certain special characters in a search bar, the app will crash. These include \ and '.
The apostrophe is a problem, in that words like "isn't" and "doesn't" will cause a crash
How should I go about working around this problem?
Many thanks
Before doing a search using the search string, pre-filter it, like this:
NSString *safeSearchString = [[searchString componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:#""];
Why don't you escape the characters and actually search for what the user wants instead of ignoring or removing what they want ?
Are you using SQLite ? If so then you can escape a single quote by another single quote.
The UISearchBar utilizes the UISearchBarDelegate protocol. To stop users entering \ and ' all you need to do is to override the searchBar:shouldChangeTextInRange:replacementText: method and if you sense the special characters in the text, you simply return NO.
But handling (rather than ignoring) these special chars in your program is a better user experience. After all why are you users even searching for "isn't" and "doesn't" in the first place?

Clean up & style characters from text

I am getting text from a feed that has alot of characters like:
Insignia&#153; 2.0 Stereo Computer Speaker System (2-Piece) - Black
4th-Generation Apple® iPod® touch
Is there an easy way to get rid of these, or do I have to anticipate which characters I want to delete and use the delete method to remove them? Also, when I try to remove
&
with
str.delete("&")
It leaves behind "amp;" Is there a better way to delete this type of character? Do I need to re-encode the text?
String#delete is certainly not what you want, as it works on characters, not the string as a whole.
Try
str.gsub /&/, ""
You may also want to try replacing the & with a literal ampersand, such as:
str.gsub /&/, "&"
If this is closer to what you really want, you may get the best results unescaping the HTML string. If so try this:
CGI::unescapeHTML(str)
Details of the unescapeHTML method are here.
If you are getting data from a 'feed', aka RSS XML, then you should be using an XML parser like Nokogiri to process the XML. This will automatically unescape HTML entities and allow you to get the proper string representation directly.
For removing try to use gsub method, something like this:
text = "foo&bar"
text.gsub /\b&\b/, "" #=> foobar

Resources