单击按钮时,将句子的单词放入不同的透明标签中

时间:2013-06-18 02:03:08

标签: ios objective-c uilabel nsmutablestring

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

3 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分: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];

答案 2 :(得分: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