Unicode not working properly in iOS - ios

My unicode is \u20AC when I set it on UILabel but I'm getting unicode on label. Some time it is printing euro but mostly it is printing unicode on label.
My code is over here
NSLog(#"Currecy %#",currencySymbol);
UILabel *priceLbl = [[UILabel alloc] initWithFrame:CGRectMake(180, 10, 45, 25)];
priceLbl.textColor = [UIColor whiteColor];
priceLbl.textAlignment = NSTextAlignmentCenter;
priceLbl.font = [UIFont fontWithName:#"ProximaNova-Bold" size:15];
priceLbl.tag = 1001;
fair = [NSString stringWithFormat:#"%d",arc4random()%50];
priceLbl.text = [NSString stringWithFormat:#"%# %#",fair,currencySymbol];
Output
Currecy \u20AC
Printing description of priceLbl:
<UILabel: 0x7faade3095a0; frame = (185 10; 50 25); text = '\u20AC'; userInteractionEnabled = NO; tag = 1001; layer = <_UILabelLayer: 0x7faadbd91bb0>>
And I'm trying to set at my end getting output as I would like. for example
Getting server response
{
currency = "\\u20AC";
description = "You have been successfully logged in.";
}
and the currency symbol replacing "\\" with "\"
NSString *currency = [response[#"currency"] stringByReplacingOccurrencesOfString:#"\\\\" withString:#"\\"];

NGUYEN MINH answer worked for me as follow:
NSString *currencySymbol = #"\\u20AC";
NSString *fair = #"1.99 ";
NSString *convertedString = currencySymbol;
CFStringRef transform = CFSTR("Any-Hex/Java");
CFStringTransform((__bridge CFMutableStringRef)convertedString, NULL, transform, YES);
label.text = [NSString stringWithFormat:#"%# %#", fair, convertedString];
The problem is that your currencySymbol contains: #"\\u20AC" which is a string of 6 characters not the one character string #"\u20AC"
Another working solution:
NSString *currencySymbol = #"\\u20AC";
NSString *fair = #"1.99 ";
currencySymbol = [NSString
stringWithCString:[currencySymbol cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
label.text = [NSString stringWithFormat:#"%# %#", fair, currencySymbol];

please try:
NSString *convertedString = #"some text"; //Your unicode string here
CFStringRef transform = CFSTR("Any-Hex/Java");
CFStringTransform((__bridge CFMutableStringRef)convertedString, NULL, transform, YES);
yourLabel.text = convertedString;

You are confusing the output of NSLog with the real data.
\u20ac is how NSLog displays a Euro symbol, because the Euro symbol is the Unicode character u20ac.

Related

Concatenation Values in IOS

How to concatenate the value from UITextField and value from button and display
them together in Objective-C? I'm trying but not getting it.
you can try this
NSString *strConcate = [NSString stringWithFormat:#"%# %#",textFeild.text,button.titleLabel.text];
NSString* aString = [NSMutableString stringWithFormat:#"String with two ints %d - %d", myInt, myInt];
NSLog(#"%#",aString);
NSString* aString = [NSMutableString stringWithFormat:#"String with two ints %d - ... now has another int: %d", myInt, myInt];
NSLog(#"%#",aString);
//suppose - int one = 1; int two = 2; int three = 3
NSString *oneTwoThree = [NSString stringWithFormat:#"%d/%d/%d", one, two, three];
NSLog(#"%#",oneTwoThree);
// suppose
//UITextField *tf = [[UITextField alloc] init];
//tf.text = "text field text"
//UIButton *btn = [[UIButton alloc] init];
//btn.titleLabel.text = "button text"
NSString* concatedString = [NSMutableString stringWithFormat:#"%# - %#", tf.text, btn.titleLabel.text];
NSLog(#"%#",concatedString); //result: "text field text = button text"

iOS: Truncate a large string , and appending a suffix string

I need to display some text in two lines , e.g.
| a very large string string string |
| string string ... - a suffix string |
The whole text contains two part
a large description string, need be truncated
"-a suffix string", won't be truncated
How to implement it in iOS ?
If you set it to NSLineBreakByTruncatingMiddleit will truncate in the middle.
You have to set the lineBreakMode. You can either do that from Interface Builder or programmatically as follows
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
another Solution
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, 21)];
NSString *string = #"The Dark Knight Rises at 7:45pm";
NSString *substring = #"at";
CGFloat pix = 120.0;
NSString *result = [self truncatedStringFrom:string toFit:label atPixel:120.0 atPhrase:#"at"];
label.text = result;
My first idea would be two labels side-by-side both with fixed width,
but I'll assume you've ruled that out for some unstated reason.
Alternatively, compute the truncation manually, like this ...
- (NSString *)truncatedStringFrom:(NSString *)string toFit:(UILabel *)label
atPixel:(CGFloat)pixel atPhrase:(NSString *)substring {
// truncate the part of string before substring until it fits pixel
// width in label
NSArray *components = [string componentsSeparatedByString:substring];
NSString *firstComponent = [components objectAtIndex:0];
CGSize size = [firstComponent sizeWithFont:label.font];
NSString *truncatedFirstComponent = firstComponent;
while (size.width > pixel) {
firstComponent = [firstComponent substringToIndex:[firstComponent length] - 1];
truncatedFirstComponent = [firstComponent stringByAppendingString:#"..."];
size = [truncatedFirstComponent sizeWithFont:label.font];
}
NSArray *newComponents = [NSArray arrayWithObjects:truncatedFirstComponent, [components lastObject], nil];
return [newComponents componentsJoinedByString:substring];
}
Just do this and let me know..

lineBreakMode for multiline attributed string

I have a multiline label that will contain an attributed string. This is the code of the attributed string:
NSString * titleString = #"Title";
NSString * informationString = self.myObject.content;
NSString * allString = [NSString stringWithFormat:#"%#\n\n%#",titleString,informationString];
NSMutableAttributedString* attributedMessage = [[NSMutableAttributedString alloc]initWithString:allString];
NSMutableParagraphStyle* style=[[NSMutableParagraphStyle alloc]init];
style.alignment = NSTextAlignmentLeft;
NSDictionary *attributesTitle = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName,[UIColor blackColor],NSForegroundColorAttributeName,[UIColor clearColor],NSBackgroundColorAttributeName,Font(#"OpenSans-Semibold", 17+(int)((self.view.bounds.size.width-290.)/15.)),NSFontAttributeName,nil];
NSMutableParagraphStyle* styleText=[[NSMutableParagraphStyle alloc]init];
styleText.alignment = NSTextAlignmentLeft;
styleText.lineBreakMode = NSLineBreakByTruncatingTail;
NSDictionary *attributesInformation = [NSDictionary dictionaryWithObjectsAndKeys:styleText,NSParagraphStyleAttributeName,[UIColor colorWithRed:91./255 green:91./255 blue:91./255 alpha:1.],NSForegroundColorAttributeName,[UIColor clearColor],NSBackgroundColorAttributeName,[UIFont fontWithName:#"Roboto-Light" size:13.+(int)((self.view.bounds.size.width-290.)/15.)],NSFontAttributeName,nil];
[attributedMessage setAttributes:attributesTitle range:[allString rangeOfString:titleString]];
[attributedMessage setAttributes:attributesInformation range:[allString rangeOfString:informationString]];
In my case the label is shown with ellipse "..." in all the lines. how can i resolve this issue. I want that will be shown only in the last visible line.
The solution that i adopted is
while (newHeight>=heightOfLabel){
remove some letter from the end of the string
calculate new height
}
replace the last three charachter with "..."
and it is done :)
I know that it is not a clean solution but it resolve my issue until finding a clean one.

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.

Degree Fahrenheit/Celsius Symbols ASCII NSString

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;

Resources