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.
Related
I have a list of Text views that include a year saved as an int. I'm displaying it in an interpolated string:
Text("\($0.property) \($0.year) \($0.etc)")
The problem, it adds a comma to the int, for example it displays 1,944 instead of the year 1944. I'm sure this is a simple fix but i've been unable to figure out how to remove the comma. Thanks!
There is explicit Text(verbatim:) constructor to render string without localization formatting, so a solution for your case is
Text(verbatim: "\($0.property) \($0.year) \($0.etc)")
Use Text(String(yourIntValue)) if you use interpolation you need to cast it as a string directly. If you allow the int to handle it, it shows with a ,.
So to recap.
let yourIntValue = 1234
Text(String(yourIntValue)) // will return `1234`.
Text("\(yourIntValue)") // will return `1,234`.
I use the built-in format parameter. It's useful for formatting well beyond just this one specific usage (no commas).
Text("Disk Cache \(URLCache.shared.currentDiskUsage,
format: .number.grouping(.never))"))
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.).
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];
How do I add a single character such as a '$' or a '+' at the beginning of an existing string?
I tried using the appendingString method but that adds the $ or + at the end of the string.
I know I can always save the $ or + in a new string and then append the other string, but I just want to know if there is a better way to do it.
Thank you.
This is actually very simple:
[#"+" stringByAppendingString:existingString];
This should definitely work for you :) And the + will be at the beginning.
Strings aren't mutable. You're creating a new string when you use stringByAppendingString: In order to prepend, you would have to make a mutable version of your existing string and then use insertString:atIndex: like so:
[[NSMutableString stringWithString:myString] insertString:#"$" atIndex:0];
Why there isn't a stringByPrependingString:, I don't know.
The best solution is the one you've already mentioned:
[#"$" stringByAppendingString:myString];
As long as the string is mutable, i.e. it is an NSMutableString you can use.
[str insertString:#"$" atIndex:0];
Have a read of the docs here, https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsmutablestring_Class/Reference/Reference.html
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.)