I have a string which hold a varying part in the middle according to some cases.
Example: You lost 255 points.
The point value "255" is the varying part and I want to hold the non-varying part in my string file. However I don't want to have two entries in my strings file like.
"string_start" = "You lost"
"string_end" = "points."
Btw the points part which is (255) in the example is an NSMutableAttributedString to support a different color and font style.
Thanks in Advance.
I would do it like this:
[NSString stringWithFormat:NSLocalizedString(#"You lost %d points", nil), 255]
and you localize #"You lost %d points" in whatever language you want.
Related
I have a label which shows an expression:
(x+y)
But I want to show it in label like this:
(x+y)^2
(But with degree, I can't do it here, because I have too low reputation to insert images)
So, I want to show expression's degree in UIlabel.
Is it possible with single UILabel?
You can use Unicode characters of superscript two \u00B2, it it's always \u followed by the character code.
NSString *equation = [NSString stringWithFormat:#"(x+y)%#", #"\u00B2"];
Swift:
var equation = NSString(format:"(x+y)%#", "\u{00B2}") as String
Result:
http://unicode-table.com/en/
Strings and Characters (Apple iOS Developer Library )
Strings in Swift
I think you are looking for powers e.g. (x + y)⁹.
For this, You have to use unicodes.
you can take list of unicodes character list from here;
http://www.fileformat.info/info/unicode/category/No/list.htm
In code, you will use;
print("(x+y)\u{00B2}");
I have been having a lot of trouble with NSString's stringWithFormat: method as of late. I have written an object that allows you to align N lines of text (separated by new lines) either centered, right, or left. At the core of my logic is NSString's stringWithFormat. I use this function to pad my strings with spaces on the left or right of individual lines to produce the alignment I want. Here is an example:
NSString *str = #"$3.00" --> 3 dollars
[NSString stringWithFormat:#"%8s", [str cStringUsingEncoding:NSUnicodeStringEncoding]] --> returns --> " $3.00"
As you can see the above example works great, I padded 3 spaces on the left and the resulting text is right aligned/justified. Problems begin to arise when I start to pass in foreign currency symbols, the formatting just straight up does not work. It either adds extra symbols or just returns garbage.
NSString *str = #"Kč1.00" --> 3 Czech Koruna (Czech republic's currency)
[NSString stringWithFormat:#"%8s", [str cStringUsingEncoding:NSUnicodeStringEncoding]] --> returns --> " Kč1.00"
The above is just flat out wrong... Now I am not a string encoding expert but I do know NSString uses the international standardized unicode encoding for special characters well outside basic ASCII domain.
How can I fix my problem? What encoding should I use? I have tried so many different encoding enums I have lost count, everything from NSMACOSRomanEncoding to NSUTF32UnicodeBigEndian.. My last resort will be to just completely ditch using stringWithFormat all together, maybe it was only meant for simple UTF8Strings and basic symbols.
If you want to represent currency, is a lot better if you use a NSNumberFormatter with currency style (NSNumberFormatterCurrencyStyle). It reads the currentLocale and shows the currency based on it. You just need to ask its string representation and append to a string.
It will be a lot easier than managing unicode formats, check a tutorial here
This will give you the required result
NSString *str = #"Kč1.00";
str=[NSString stringWithFormat:#"%#%8#",[#" " stringByPaddingToLength:3 withString:#" " startingAtIndex:0],str];
Out Put : #" Kč1.00";
Just one more trick to achieve this -
If you like use it :)
[NSString stringWithFormat:#"%8s%#",[#"" cStringUsingEncoding:NSUTF8StringEncoding],str];
This will work too.
I would like to create a label with some unicode text and a music note. The notes are shown below:
I have tried:
titleLabel.text = #" title + ♫";
but that results in:
I must be doing something dumb.. Any advice welcome.
The number column in your table actually contains HTML/SGML/XML entities with decimal values. A unicode escape sequence in NSString takes the hexadecimal value, so your note ♫ would be the hex value 0x266b to be used like this
titleLabel.text = #" title \u266b";
Hit cmd+cntrl+space in Xcode, and search for 'note'. There are some u may use. Just double click one and it will be written where your cursor is in the code.
If I have a string in Objective-C called text that is a really long, such as:
NSString *text = [NSString stringWithFormat:#"%#", _sometext];
How can I scan _sometext and add a line break to the string every hundred characters (letters)?
So if I had the _sometext as
It was November. Although it was not yet late, the sky was dark when I
turned into Laundress Passage. Father had finished for the day,
switched off the shop lights and closed the shutters; but so I would
not come home to darkness he had left on the light over the stairs to
the flat.
How can I make it so it puts a line break after
It was November. Although it was not yet late, the sky was dark when I
turned into Laundress Passage
and after
. Father had finished for the day, switched off the shop lights and
closed the shutters; but so I wo
(since those were 100 characters)?
But instead of stopping in the middle of the word, I could it would skip the word and end at the previous word. Example: if the sentence ended at "but so I wo" and it cut of the word "would", it would stop at this instead of "but so I".
The easiest solution I can think of off the top of my head is a two step process.
Step one involves breaking the original string into an array of 100 character strings.
Step two involves joining that array of strings with the newline character.
NSMutableArray *lines = [NSMutableArray array];
while ([originalString length] > 100) {
[lines addObject:[originalString substringToIndex:100]];
originalString = [originalString substringFromIndex:100];
}
[lines addObject: originalString];
NSString *reformattedString = [lines componentsJoinedByString:#"\n"];
You could write code that would use the method rangeOfString:options:range:
to do that.
You'd create an NSRange for the first 100 characters of your string. Then you'd search backwards in that range for a space. When you found a space, you'd add from the beginning of the string to the space to your output string, plus a line break. Then you'd set your search range to the next 100 characters of your string after the space, and again search backwards for a space. Repeat until you've processed the entire string.
See the NSString class reference for the details on the rangeOfString:options:range: method.
So as I work my way through understanding string methods, I came across this useful class
NSCharacterSet
which is defined in this post quite well as being similar to a string excpet it is used for holding the char in an unordered set
What is differnce between NSString and NSCharacterset?
So then I came across the useful method invertedSet, and it bacame a little less clear what was happening exactly. Also I a read page a fter page on it, they all sort of glossed over the basics of what was happening and jumped into advanced explainations. So if you wanted to know what this is and why we use It SIMPLY put, it was not so easy instead you get statements like this from the apple documentation: "A character set containing only characters that don’t exist in the receiver." - and how do I use this exactly???
So here is what i understand to be the use. PLEASE provide in simple terms if I have explained this incorrectly.
Example Use:
Create a list of Characters in a NSCharacterSetyou want to limit a string to contain.
NSString *validNumberChars = #"0123456789"; //Only these are valid.
//Now assign to a NSCharacter object to use for searching and comparing later
validCharSet = [NSCharacterSet characterSetWithCharactersInString:validNumberChars ];
//Now create an inverteds set OF the validCharSet.
NSCharacterSet *invertedValidCharSet = [validCharSet invertedSet];
//Now scrub your input string of bad character, those characters not in the validCharSet
NSString *scrubbedString = [inputString stringByTrimmingCharactersInSet:invertedValidCharSet];
//By passing in the inverted invertedValidCharSet as the characters to trim out, then you are left with only characters that are in the original set. captured here in scrubbedString.
So is this how to use this feature properly, or did I miss anything?
Thanks
Steve
A character set is a just that - a set of characters. When you invert a character set you get a new set that has every character except those from the original set.
In your example you start with a character set containing the 10 standard digits. When you invert the set you get a set that has every character except the 10 digits.
validCharSet = [NSCharacterSet characterSetWithCharactersInString:validNumberChars];
This creates a character set containing the 10 characters 0, 1, ..., 9.
invertedValidCharSet = [validCharSet invertedSet];
This creates the inverted character set, i.e. the set of all Unicode characters without
the 10 characters from above.
scrubbedString = [inputString stringByTrimmingCharactersInSet:invertedValidCharSet];
This removes from the start and end of inputString all characters that are in
the invertedValidCharSet. For example, if
inputString = #"abc123d€f567ghj😄"
then
scrubbedString = #"123d€f567"
Is does not, as you perhaps expect, remove all characters from the given set.
One way to achieve that is (copied from NSString - replacing characters from NSCharacterSet):
scrubbedString = [[inputString componentsSeparatedByCharactersInSet:invertedValidCharSet] componentsJoinedByString:#""]
This is probably not the most effective method, but as your question was about understanding
NSCharacterSet I hope that it helps.