Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting text from uilabel in objective-c

I have a project where I am trying to grab text from a uilabel that has been populated from a web service. I can grab and manipulate the text just fine but I need to send certain characters from the string to another web service call. I can not figure out how to grab the first, second and last characters in my string. I am both new to Objective-C as well as programming so any help would be much appreciated.

like image 478
charlie johnston Avatar asked Jun 03 '26 01:06

charlie johnston


2 Answers

You can do it like this:

UILabel * l = [[UILabel alloc] init];
l.text = @"abcdef"; //set text to uilabel
[self.view addSubview:l];

NSString * text = l.text; //get text from uilabel
unichar first = [text characterAtIndex:0]; //get first char
unichar second = [text characterAtIndex:1];
unichar last = [text characterAtIndex:text.length -1];

If you need results as strings you can use:

NSString * firstAsString = [text substringWithRange:NSMakeRange(0, 1)]; //first character as string

or you can convert the unichar to string like this:

NSString * x = [NSString stringWithFormat:@"%C", last]; 
like image 83
Ondrej Peterka Avatar answered Jun 05 '26 14:06

Ondrej Peterka


Per this it should be fairly easy:

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UILabel_Class/Reference/UILabel.html

textLabel.text = @"Foo";

Where textLabel is instance of UILabel

like image 38
ant Avatar answered Jun 05 '26 15:06

ant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!