2014-05-12 145 views
1

我对Xcode还很陌生,所以请耐心等待。无论如何,我尝试在UILabel中显示数组的全部内容时遇到了一些麻烦。我可以简单地使用代码在文本标签中显示数组

wordList.text = [NSString stringWithFormat:@"List of Words:\n %@", listA]; 

但是一旦运行,以显示它的标签最终显示括号并在自己的行话说,以及周围的话引号,并结束引号和每个单词之间的一行逗号。例如:

List of Words: 
( 
"apple 
", 
"banana 
", 
"etc. 

虽然我希望在自己的行要显示的话,我不希望被显示在一个单独的线括号和右引号和逗号。我也希望一起删除括号,引号和逗号,但如果我无法做到,我不会介意太多。

任何人都可以请解释为什么它显示为这样,并帮助我正确地显示在UILabel自己的行中的数组的每个单词?

回答

7

使用此:

NSArray *listOfWords = @[@"One", @"Two", @"Three"]; 
NSString * stringToDisplay = [listOfWords componentsJoinedByString:@"\n"]; 
wordList.text = stringToDisplay; 

显示:

One 
Two 
Three 
+0

是的,这似乎解决了它,非常感谢你! – user3577761

-1

您可以使用此代码

NSArray *listOfWords = [NSArray arrayWithObjects: 
         @"one.", 
         @"two.", 
         nil]; 

for (NSString *stringToDisplay in matters) 
{ 
     //frame, setting 
     labelFrame.origin.x = 20.0f; 
     UILabel *stringToDisplayLabel = [[UILabel alloc] initWithFrame:labelFrame]; 
     stringToDisplayLabel.backgroundColor = [UIColor clearColor]; 
     stringToDisplayLabel.font = [UIFont boldSystemFontOfSize:12.0f]; 
     stringToDisplayLabel.lineBreakMode = NSLineBreakByWordWrapping; 
     stringToDisplayLabel.numberOfLines = 0; 

     stringToDisplayLabel.textColor = [UIColor whiteColor]; 
     stringToDisplayLabel.textAlignment = NSTextAlignmentLeft; 

     //set up text 
     stringToDisplayLabel.text = stringToDisplay; 

     //edit frame 
     [stringToDisplayLabel sizeToFit]; 
     labelFrame.origin.y += stringToDisplayLabel.frame.size.height + 10.0f; 

     [self.view addSubview:stringToDisplayLabel]; 
     [matterLabel release]; 
} 
+0

您正在创建多个标签并将它们相互叠加。 – Logan

+0

哦。我错过了代码。我将编辑代码。 – user3619441

0

括号,引号,和正在增加,因为提供了一个逗号数组作为格式说明符%@的参数导致-(NSString *)description方法发送到数组。 NSArray覆盖NSObject的执行description并返回一个字符串,该字符串表示数组内容,格式为属性列表。 (而不是只返回一个字符串与数组的内存地址。)因此,额外的字符。

+0

我想我有点理解,谢谢你的解释! – user3577761