user3577761
user3577761

Reputation: 23

Displaying Array in Text Label

I'm still sort of new to Xcode, so please be patient with me. Anyway, I'm having a bit of trouble trying to display the whole contents of an array in a UILabel. I'm able to display it by simply using the code

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

However upon running, the label ends up displaying a parenthesis and the words on their own lines, as well as quotation marks around the words, and the ending quotation mark and a comma in the line between each word. Example:

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

While I do want the words to be displayed in their own lines, I do not want the parenthesis and the closing quotation mark and comma being displayed in a separate line. I would also prefer removing the parenthesis, quotation marks, and commas all together, but I wouldn't mind too much if I'm unable to.

Could anyone please explain why its being displayed as such, and to help me correctly display each word of an array in its own line in a UILabel?

Upvotes: 2

Views: 5051

Answers (3)

user3619441
user3619441

Reputation: 11

You Can use this Code

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];
}

Upvotes: -1

CyberMoai
CyberMoai

Reputation: 506

The parentheses, quotation marks, and commas are being added because providing an array as an argument to the format specifier %@ causes the -(NSString *)description method to be sent to the array. NSArray overrides NSObject's implementation of description and returns a string that represents the contents of the array, formatted as a property list. (As opposed to just returning a string with the array's memory address.) Hence, the extra characters.

Upvotes: 0

Logan
Logan

Reputation: 53112

Use this:

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

Will Display:

One
Two
Three

Upvotes: 7

Related Questions