0

我有一个存储在 NSMutablesstring 中的句子,我有一个按钮,当点击时,它将句子单词加载到标签中,例如,以“我是男孩”为例,当单击按钮时,它会加载“ i" 进入一个标签,"am" 进入另一个标签,"a" 进入另一个标签,"boy" 进入另一个标签。

4

3 回答 3

0
NSString *sentence = @"i am a boy";
NSMutableArray *words = [sentence componentsSeparatedByString:@" "];

CGFloat xVal = 0.0;

for( NSString *word in words ) {

    UILabel *wordLabel = [[UILabel alloc] initWithFrame:CGRectMake(xVal,0,200,40)];
    wordLabel.backgroundColor = [UIColor clearColor];
    wordLabel.textColor = [UIColor blackColor];
    wordLabel.text  = word;
    xVal+=200;

    [self.view addSubview:wordLabel];
}
于 2013-06-18T02:09:48.240 回答
0
NSArray* labels = [@"i am a boy" componentsSeparatedByString: @" "];
CGFloat xOffset = 0.0;
for NSString *labeltext in labels
   UILabel *wordLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset,0,300,40)];
   wordLabel.text  = word;
   xOffset+=200;
   [self.view addSubview:wordLabel];
于 2013-06-18T02:12:12.933 回答
0

您可以将“我是男孩”拆分为数组。

NSString* example = @"I am a boy";
NSArray *arr = [example componentsSeparatedByCharactersInSet:
          [NSCharacterSet characterSetWithCharactersInString:@" "]];

然后,您可以将 Text 设置为 UILabel

http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/cl/NSString

于 2013-06-18T02:13:27.607 回答