Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 单击按钮时将句子中的单词放入不同的透明标签中_Ios_Objective C_Uilabel_Nsmutablestring - Fatal编程技术网

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

Ios 单击按钮时将句子中的单词放入不同的透明标签中,ios,objective-c,uilabel,nsmutablestring,Ios,Objective C,Uilabel,Nsmutablestring,我有一个句子,我存储在一个NSMutablesstring中,我有一个按钮,当点击时,它会将句子单词加载到标签中,例如,以“我是男孩”为例,当点击按钮时,它会将“我”加载到标签中,“am”加载到另一个标签中,“a”加载到另一个标签中,“boy”加载到另一个标签中。您可以拆分“我是男孩”进入阵列 NSString *sentence = @"i am a boy"; NSMutableArray *words = [sentence componentsSeparatedByString:@" "

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

您可以拆分“我是男孩”进入阵列

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];
}
NSString* example = @"I am a boy";
NSArray *arr = [example componentsSeparatedByCharactersInSet:
          [NSCharacterSet characterSetWithCharactersInString:@" "]];
然后,可以将文本设置为UILabel


你在问什么…?@David在单击按钮时,将句子中的单词分隔并将其放入标签。这不起作用,当我单击包含此方法的按钮时,应用程序崩溃
NSString* example = @"I am a boy";
NSArray *arr = [example componentsSeparatedByCharactersInSet:
          [NSCharacterSet characterSetWithCharactersInString:@" "]];