简体   繁体   English

在客观的c中从uilabel获取文本

[英]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. 我有一个项目,我试图从已经从Web服务填充的uilabel中获取文本。 I can grab and manipulate the text just fine but I need to send certain characters from the string to another web service call. 我可以抓住并操纵文本,但我需要将字符串中的某些字符发送到另一个Web服务调用。 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. 我既是Objective-C的新手也是编程,所以任何帮助都会非常感激。

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: 或者您可以将unichar转换为字符串,如下所示:

NSString * x = [NSString stringWithFormat:@"%C", last]; 

Per this it should be fairly easy: 这个应该相当容易:

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

textLabel.text = @"Foo";

Where textLabel is instance of UILabel textLabelUILabel实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM