2014-05-12 144 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