我有一个单词的文本文件,我可以在项目中很好地读取它,现在我想将单词字符串切成两半,并在我认为的多个标签上随机显示。
NSString *myfilePath = [[NSBundle mainBundle] pathForResource:@"textFile" ofType:@"txt"];
NSString *linesFromFile = [[NSString alloc] initWithContentsOfFile:myfilePath encoding:NSUTF8StringEncoding error:nil];
myWords = [NSArray alloc];
myWords = [linesFromFile componentsSeparatedByString:@"\n"];
NSLog(@"%@", myWords);
我制作了 5 个标签 如何让我的文本文件单词在这五个标签上拆分并随机出现?
AS -Sergey 推荐我的问题解决了一半,我能够在标签上显示文本文件的字符串。从代码中删除此标签 [i].text 并调用
NSMutableArray<UILabel*>* labels = [NSMutableArray array];
NSMutableArray *words = [myWords mutableCopy];
for (int i = 0; i<5; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 70*i, 100, 40)];
label.textColor = [UIColor whiteColor];
//whatever other properties
NSInteger index = arc4random()%words.count;
label.text = words[index];
[words removeObjectAtIndex:index];
[self.view addSubview:label];
[labels addObject: label];
}
这显示了一个完整的词串,现在我想将一个词串分成两部分,例如,如果我有一个词 Apple,它应该被拆分为 App 和 le 并显示它随机出现在不同的标签上,任何帮助表示赞赏。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句