Concatenation Values in IOS - 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"

Related

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..

Unicode not working properly in 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.

Time Format so there's only 4 digits with a ":" in the middle

I want the concatenated NSString I have to be output in the format "00:00", the 0s being the digits in the concatenated string. And if there are not enough characters in the NSString, the other digits are made to be 0.
And if there are more than 4 digits than I want to only have the furthest right digits.
I have done this in Java before, I am assuming it's possible in Objective-C as well.
UIButton *button = sender;
NSString *concatenated = [self.input stringByAppendingString: button.titleLabel.text];
self.input = concatenated;
self.userOutput.text = self.input;
For example, I might get "89" as my concatenated string. I then want, self.input = 00:89.
OR
if I get 89374374 from my concatenated string, I then want self.input = 43:74.
I hope I am being clear
The following method should give the desired output:
- (NSString *)getFormattedTimeStringFromString:(NSString *)string
{
int input = [string intValue];
int mins = input % 100;
input /= 100;
int hours = input % 100;
return [NSString stringWithFormat:#"%02d:%02d", hours, mins];
}
You can use this by calling
self.input = [self getFormattedTimeStringFromString:concatenated];
Like this:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm"];
NSString *dateTimeStr = [df stringFromDate:[NSDate date]];
if ([concatenated length] == 2) {
self.input = [NSString stringWithFormat:#"00:%#",concatenated];
}
else
{
NSString *test = [concatenated substringFromIndex:[concatenated length] -4];
self.input = [NSString stringWithFormat:#"%#:%#",[test substringToIndex:2],[test substringFromIndex:[test length]-2]];
}
Please try above code it will fail if [concatenated length] is 3 or 1 , modify it accordingly

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.

How can I show all of objects in NSMutable Array on text field?

I want to show all objects in mutable array on to textfield, label, something else except NSLog
- (IBAction)purchasePressed:(id)sender {
NSMutableArray *addItem = [[NSMutableArray alloc] init];
[addItem addObject:#"Almond"];
[addItem addObject:#"Choc"];
"number" is my label (I'm not sure that all of Objects in MutableArray can be showed on textfield or not?) i can do it only with NSLog.
for (i = 0;i < [addItem count] ; i++ )
{
NSLog(#"%#", addItem);
NSString *test1=(#"%#", addItem);
number.text=test1;
}
Every time you set the text of a label you replace the previous text.
Try replacing your whole loop with something like:
number.text = [addItem componentsJoinedByString:#", "];
Which will create a single string from all of the strings in the array and add that to the label. You could do something similar in your loop if you want to.
If you want a string with all the values concatenated:
NSString *mainString = [NSString alloc] init];
for (NSString *item in addItem) {
mainString = [mainString stringByAppendingString:item];
}
number.text = mainString;
EDIT: Using NSMutableString
NSMutableString *mainString = [[NSMutableString alloc] init];
for (NSString *item in addItem) {
[mainString appendString:item];
}
number.text = mainString;

Resources