2

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.

4

2 に答える 2

8

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]; 
于 2012-04-30T16:51:02.977 に答える
1

これにより、それはかなり簡単なはずです:

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

textLabel.text = @"Foo";

textLabelのインスタンスはどこにありますかUILabel

于 2012-04-30T16:51:11.933 に答える