Degree Fahrenheit/Celsius Symbols ASCII NSString - ios

How can I represent the Degree Fahrenheit symbol in an NSString? The following code will produce a degree symbol and then the capital letter F, but is there an actual as Fahrenheit itself? Similarly, is there one for Degree Celsius?
NSString *fahrenheit = [NSString stringWithFormat:#"%#F", #"\u00B0"];
NSString *celsius = [NSString stringWithFormat:#"%#C", #"\u00B0"];

Your code is correct, and that is how you would represent the two temperature ranges. It can be cut down slightly as you don't need to use stringWithFormat:
NSString *fahrenheit = #"\u00B0F";
NSString *celsius = #"\u00B0C";
But you might use stringWithFormat to format the actual temperatures along with the symbols etc:
float tempInFahrenheit = 23.4f;
float tempInCelsius = 56.8f;
NSString *fahrenheit = [NSString stringWithFormat:#"%f \u00B0F", tempInFahrenheit];
NSString *celsius = [NSString stringWithFormat:#"%f \u00B0C", tempInCelsius];

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"80\u00b0c"];
[attributedString setAttributes:#{NSFontAttributeName : [UIFont fontWithName:#"Helvetica-light" size:40.0]
, NSBaselineOffsetAttributeName : #22} range:NSMakeRange(2, 2)];
//asign this as a
examplelabel.attributedtext = attributedString;

Related

Objective C, Trim a float

I have float like 3500,435232123. All I want to know if exists (in Objective C) a function that let me keep just the last 4 digits in my case is 2123.
You can use NSNumberFormatter
NSNumberFormatter *format = [[NSNumberFormatter alloc]init];
[format setNumberStyle:NSNumberFormatterDecimalStyle];
[format setRoundingMode:NSNumberFormatterRoundHalfUp];
[format setMaximumFractionDigits:4];
[format setMinimumFractionDigits:4];
string = [NSString stringWithFormat:#"%#",[format stringFromNumber:[NSNumber numberWithFloat:65.50055]] ;
Or simply
NSString *string = [NSString stringWithFormat:#"%.04f", floatValue];
If you want only last four digits, convert the float to a string
NSString *string = [NSString stringWithFormat:#"%f", floatValue];
and get the last four characters
NSString *lastFour = [string substringFromIndex: [string length] - 4];
It you want to get the decimal part, you can do x - floor(x). For instance:
float x = 3500,435232123;
NSString *string = [NSString stringWithFormat:#"%.04f", x - floor(x)];
And to get 4 decimal digits do what Fawad Masud says.
No there is no such function, as far as i know. But here is a way to achieve exactly what you want.
First you have to round it to four digits after point:
NSString *exampleString = [NSString stringWithFormat:#"%.04f", valueToRound];
Then you get the location for the comma inside the exampleString:
NSRange commaRange = [valueString rangeOfString:#","];
Finally you create the finalString with the values from that NSRange. The substring starts at commaRange.location+commaRange.lengthbecause thats the index directly after the comma.
NSString *finalString = [valueString substringWithRange:NSMakeRange(commaRange.location+commaRange.length,valueString.length-commaRange.location-commaRange.length)];
Hope that helps you.
I think is no predefined function for that.
and the solution i thought of is:
float floatNum = 3500.435232123;
converting float number to string and trim/substring the string, like for example:
NSString *stringFloat = [NSString stringWithFormat:#"%f", floatNum];
NSString *newString = [stringFloat substringWithRange:NSMakeRange(stringFloat.length - 4, stringFloat.length)];
NSLog(#"%#", newString);
another is something like:
NSString *stringFloat = [NSString stringWithFormat:#"%f", floatNum];
//separates the floating number to
arr[0] = whole number
arr[1] = decimals
NSArray *arr=[str componentsSeparatedByString:#"."];
since you just want to work on the decimal, i think arr[1] is what you need..
NSString *stringDecimals = (NSString *)arr[1];
if ( stringDecimals.length > 4) //check the length of the decimals then cut if exceeds 4 character..
{
stringDecimals = [stringDecimals substringWithRange:NSMakeRange(stringDecimals.length - 4, stringDecimals.length)];
}
NSLog(#"stringDecimals: %#", stringDecimals);

How do I easily separate a set of strings stored in a larger NSString?

I had a UILabel with the following text:
Medium, Black
What I intended to do was grab the words in the string and insert each into a mutable array so I could use each title later on to identify something.
With the help of Stackoverflow I done it like this:
NSMutableArray *chosenOptions = [[[[cell tapToEditLabel] text] componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#" ,"]] mutableCopy];
[chosenOptions removeObject:#""];
Now I can access this objects and they return the correct strings:
NSString *size = [chosenOptions objectAtIndex:0]; //Medium
NSString *colour = [chosenOptions objectAtIndex:1]; //Black
This is fine. But the problem starts when dealing with female sizes instead of males which are displayed like this:
[8 UK], [10 UK], [12 UK], [14 UK]
Let us say I now have a UILabel with the following text:
[8 UK], Black
Using the same code above my NSLog here:
NSLog(#"size label-> %#", size);
NSLog(#"colour label-> %#", colour);
Reads back:
size label-> [8
colour label-> UK]
Would appreciate a simple solution in code please.
The code that does the stripping doesn't take into account the way my female sizes are set in a string. I need a solution that will work with both male and female size string styles.
Thanks for your time.
Simply use componentsSeparatedByString:#", ".
NSArray *chosenOptions = [[[cell tapToEditLabel] text] componentsSeparatedByString:#", "];
Another way to do this that works with trailing commas with or without whitespace.
NSString *tag = #"Medium, Black";
NSMutableArray *options = [[tag componentsSeparatedByString:#","] mutableCopy];
NSString *size = [[options objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *color = [[options objectAtIndex:1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(#"%#:%#", size, color);
We first split by just , and then remove all extra whitespace.
Here is some sample data:
NSArray *tags = #[#"[8 UK], Black", #"Medium, Black", #"Large, Red, ", #"Small, Black,"];
for (NSString *tag in tags) {
NSMutableArray *options = [[tag componentsSeparatedByString:#","] mutableCopy];
NSString *size = [[options objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *color = [[options objectAtIndex:1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(#"%#:%#", size, color);
}

How to change case of an NSMutableAttributedString in Objective-C?

I have a NSMutableAttributedString that contain values in lower case letters. I need to convert all the lowercase letters to uppercase. We can solve this using uppercaseString for normal string as:
[string uppercaseString];
How can I change my case for NSMutableAttributedString? Thanks!
Hope the below code snippet may help you
NSMutableAttributedString * linkString = [[NSMutableAttributedString alloc]initWithString:#"Google"];
NSString * strings = [[linkString string]uppercaseString];
[linkString replaceCharactersInRange:NSMakeRange(0, [linkString length]) withString:strings];
NSLog(#"UpperCase %#",linkString);
NSMutableAttributedString class doesn't have uppercaseString method. So you can use like this..
NSString *str = #"objective-c";
str = [str uppercaseString];
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:str];
NSLog(#"Attributed String %#",attStr);
And You wanna upper latter in particular range then do something like this...
[attStr replaceCharactersInRange:NSMakeRange(0, 1) withString:#"O"];

How to get a character before last letter in NSString?

I have NSString with input Value from keyboard.
Eg.
NSString *myText = #"Apple";
In my case , i want to get a word before last letter.
For above eg , i want to get only l letter before e letter.
How can i get it?
NSString *text = #"Apple";
unichar c = [text characterAtIndex:text.length - 2];
If you need a NSString
NSString *character = [NSString stringWithCharacters:&c length:1];
that may be a useful implenentaion as well:
NSString *_string = #"string";
NSString *_letter = nil;
if (_string.length > 1) {
[_string substringWithRange:NSMakeRange(_string.length - 2, 1)];
}
it does not crash either, when the string is not long enough.

Removing last characters of NSString until it hits a separator

I've got a string that shows the stock amount using "-" as separators.
It's built up like this: localStock-wareHouseStock-supplierStock
Now I want to update the supplierStock at the end of the string, but as you can see in the code below it goes wrong when the original string returns more than a single-space value (such as 20).
Is there a way to remove all characters until the last "-" (or remove characters after the second "-")?
NSMutableString *string1 = [NSMutableString stringWithString: p1.colorStock];
NSLog(#"string1: %#",string1);
NSString *newString = [string1 substringToIndex:[string1 length]-2];
NSLog(#"newString: %#",newString);
NSString *colorStock = [NSString stringWithFormat:#"%#-%#",newString,p2.supplierStock];
NSLog(#"colorstock: %#",colorStock);
p1.colorStock = colorStock;
NSLog1
string1: 0-0-0
newString: 0-0
colorstock: 0-0-20
NSLog2
string1: 0-0-20
newString: 0-0-
colorstock: 0-0--20
EDIT: Got it working thanks to Srikar!
NSString *string1 = [NSString stringWithString: p1.colorStock];
NSLog(#"string1: %#",string1);
NSString *finalString = [string1 stringByReplacingOccurrencesOfString:[[string1 componentsSeparatedByString:#"-"] lastObject] withString:p2.supplierStock.stringValue];
NSLog(#"finalString: %#",finalString);
p1.colorStock = finalString;
Why not use componentsSeparatedByString followed by lastObject ?
NSString *supplierStock = [[string1 componentsSeparatedByString:#"-"] lastObject];
The above works if the "stock amount" is always in sets of 3's separated by a "-". Also since you always want supplierStock, lastObject is perfect for your needs.
Of course after splitting string1 with - you get a NSArray instance and you can access the individual components using objectAtIndex:index. So if you want localStock you can get by
NSString *localStock = [[string1 componentsSeparatedByString:#"-"] objectAtIndex:0];
I would suggest splitting the string into the 3 parts using [NSString componentsSeparatedByString:#"-"] and then building it back up again:
NSArray *components = [p1.colorStock componentsSeparatedByString:#"-"];
p1.colorStock = [NSString stringWithFormat:#"%#-%#-%#",
[components objectAtIndex:0],
[components objectAtIndex:1],
p2.supplierStock];
With a string that looks like
NSString *myString = #"Hello-World";
you can separate it with the componentsSeparatedByString: method of the NSString object as
NSArray *myWords = [myString componentsSeparatedByString:#"-"];
The myWords - array will then contain the two NSString objects Hello and World.
To access the strings:
NSString *theHelloString = [myWords objectAtIndex:0];
NSString *theWorldString = [myWords objectAtIndex:1];
Hope it helps!
None of these examples show how to do this if you are unaware of how many of these separator occurrences you're going to have in the original string.
Here's what I believe the correct the correct code should be for dismantling the original string and rebuilding it until you reach the final separator, regardless of how many separators it contains.
NSString *seperator = #" ";
NSString *everythingBeforeLastSeperator;
NSArray *stringComponents = [originalString componentsSeparatedByString:seperator];
if (stringComponents.count!=0) {
everythingBeforeLastSeperator = [stringComponents objectAtIndex:0];
for (int a = 1 ; a < (stringComponents.count - 1) ; a++) {
everythingBeforeLastSeperator = [NSString stringWithFormat:#"%#%#%#", everythingBeforeLastSeperator, seperator, [stringComponents objectAtIndex:a]];
}
}
return everythingBeforeLastSeperator;

Resources