xcode 在objective-c中从uilabel获取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10387319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:08:44  来源:igfitidea点击:

getting text from uilabel in objective-c

objective-ciosxcodeuitextfielduilabel

提问by charlie johnston

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.

我有一个项目,我试图从 Web 服务填充的 uilabel 中获取文本。我可以很好地抓取和操作文本,但我需要将字符串中的某些字符发送到另一个 Web 服务调用。我不知道如何获取字符串中的第一个、第二个和最后一个字符。我对 Objective-C 和编程都很陌生,所以任何帮助都将不胜感激。

回答by Ondrej Peterka

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

回答by ant

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 textLabelis instance of UILabel

textLabel实例在哪里UILabel