iphone: detecting and replacing a URL in an NSString - iphone

I currently have an NSString which can take in a message body similar to a tweet.
E.g.
NSString *sampleText = "This text contains the link http://www.google.com"
I need to write a function that can take in this text, detect that a url exists in the string, and be able to replace the url with a placeholder text.
E.g. after the function is used, the text should equal:
sampleText = "This contains contains a LINK"
Can someone please tell me how I can do this? Do I need to use RegEx?

iOS 4 has [NSRegularExpression replaceMatchesInString:options:range:withTemplate:] which looks like a good bet. (It expects an NSMutableString.)

Related

Different font for parameters in NSLocalizedString

I want to give a label a text that have multiple fonts in it. This can be accomplished by creating a NSMutableAttributedString. However, I am not sure how I format the following case:
String(format: NSLocalizedString("%# has replied in '%#'", comment: ""), username, conversationTitle)
I want to give the username and conversation title a separate font. I want to do this in the less buggiest way. What I mean by this:
I do not want to find out the username later on in the string by using a substring. This is causing issues when the conversationTitle is the same as the username, or the conversationTitle is in the username etc. etc..
I do not want to build up the string, as seen here: https://stackoverflow.com/a/37992022/7715250. This is just bad when creating NSLocalizedString's, I think the translators are going to have a bad time when string are created like that.
Questions like: Making text bold using attributed string in swift, Are there approaches for using attributed strings in combination with localization? and others are mostly string literals without NSLocalizedString or NSLocalizedString with parameters.
First, you should have in your .strings a much more generic and readble key, something like:
"_REPLIED_IN_" = "%# has replied in %#";
Do not confuse keys and values as you seem to do in your example.
Also, it's easier later to see when there is an hardcoded string not localized in your code.
Now, there is an issue, because in English, it might be in that order, but not necessarily in other languages.
So instead:
"_REPLIED_IN_" = "%1$# has replied in %$2#";
Now, I'll use the bold sample, because it's easier, but what you could do is use some custom tags to tell you that it needs to be bold, like HTML, MarkDown, etc.
In HTML:
"_REPLIED_IN_" = "<b>%1$#</b> has replied in <b>%$2#</b>";
You need to parse it into a NSAttributedString:
let translation = String(format: NSLocalizedString(format: "_REPLIED_IN_", comment: nil), userName, conversationTitle)
let attributedText = NSAttributedString.someMethodThatParseYourTags(translation)
It's up to you to choose the easiest tag format), according to your needs: easy to understand by translators, and easy to parse (CocoaTouch already has a HTML parser, etc.).

Posting any text with ampersand "&" back to web service is causing issues [duplicate]

This question already has answers here:
Ampersand in POST request causing havoc
(4 answers)
Closed 9 years ago.
I am posting text with ampersand "&" back to web service and it is causing issues in my Objective C application.
I have to escape it somehow.
Any ideas?... I am very new to Objective C and iPhone programming at the moment...
This happens when you try to pass some values in the URL and the value contains & which is used to separate parameters in the URL, hence the web server spilts the content into parts wherever it encounters an & rather than taking it as a single continuos value.
You can url encode the string to resolve the problem.
NSString *valueToSubmit = #"This is my value which contains & in it.";
NSString *urlEncodedString = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)valueToSubmit,NULL,(CFStringRef)#"!*'();:#&=+$,/?%#[]",kCFStringEncodingUTF8));
NSLog(#"Non-Encoded: %# \n Encoded: %#",valueToSubmit,urlEncodedString];
The output will be something like this:
Non-Encoded: This is my value which contains & in it.
URL Encoded: This%20is%20my%20value%20which%20contains%20%26%20in%20it.
I take it you are posting something like "bread & butter" then what you see in your web service is missing the & and possibly a few characters after it or even a weird character in its place. Try replacing & with & So in the example above, you'd post "bread &butter". You will find similar problems if you try to send a < or > and a few other characters. If you google NSString HTML escape you will find Objective C categories that can help you do this.
For an URL like www.mysite/foo&bar.html you would want to replace they & with %26 So www.mysite/foo%26bar.html. The same would be true in a query string. Replace the & with %26

Localizing dynamic NSString

Going through number of examples regarding to NSLocalizedString, what I found was we need to pre-define all the string in Localized.string file for what-ever language you want to localize. But, is it possible to localize dynamic string. My idea was, I am displaying few text in UILabel that i get after web request. It means the string is now dynamic in nature.
Declare in Localizable.strings
"SAMPLE_LOCALIZE_STRING" = "This is sample dynamic localize string for %#.";
Use it like this
NSString *dynamicStr = #"Test";
label.Text = [NSString stringWithFormat:NSLocalizedString(#"SAMPLE_LOCALIZE_STRING", nil), dynamicStr];
If those strings are fixed ones (I mean a limited number of options) then pre-store them in the localized string file.
If not, I would suggest to add a parameter to your request that would indicate the language and then the server would return string in that language.
I have handled this situation as follows,
Include language in the request. For ex: http://yourIp/language/notesandcondition
The webservice should be designed to handle for different languages.
[NSString stringWithFormat:NSLocalizedString(#"Table View Cell Row %d", #""), indexpath.row];

iPhone app: emoji unicode

I have a Json return that has a string that sometimes inludes something like \Uf604 in the array (IE memo = "\Uf604";). I need to convert it to \U0001F604 if possible.
I tried to do something like stringByReplacingOccurrencesOfString but at that point when its in a string and it's been converted to ÔòÑ which I think it needs to be üòÑ to be displayed as a emoticon. I also tried
[str stringByReplacingOccurrencesOfString:#"Ô" withString:#"ü"];
But that didn't change anything. It still gets returned as ÔòÑ.
Any help would be appreciated!
I believe str = [str stringByReplacingOccurrencesOfString:#"Ô" withString:#"ü"]; is what you are trying to do.
stringByReplacingOccurrencesOfString:withString: doesn't change the string you are calling on but returns a new string with replaced characters.

Parsing XML, details and attributes, iphone

I'm not sure about parsing an xml with attributes. have seen a similar question here
But it shows to get an attribute of intValue. But i need to get the attributes of string type,How to do that?? Images of xml and the relevant portions are given in the following links
Click here for xml and here for required data
This answer to the linked question should work for you as well. The contents of attributeDict are already NStrings. All that is going on extra in the linked answer that they are calling the intValue method on the returned NSString to parse that string into an int. In your case, you don't need this little bit of an extra step. If you just do this:
NSString * stringValue = [attributeDict objectForKey:#"attribute"];
you'll have the value of the attribute called "attribute" in a string.