2

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?

3 Answers 3

7

Use this:

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

Will Display:

One
Two
Three
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

I think I kind of get it, thank you anyway for the explanation!
-1

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

1 Comment

You're creating multiple labels and adding them over top of each other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.