charlie johnston
charlie johnston

Reputation: 67

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.

Upvotes: 2

Views: 9501

Answers (2)

Ondrej Peterka
Ondrej Peterka

Reputation: 3427

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]; 

Upvotes: 8

ant
ant

Reputation: 22948

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

Upvotes: 1

Related Questions