Willy2414
Willy2414

Reputation: 119

Text Label Word Wrapping

I am using a text label to display previously entered user information that has been saved. I am having trouble fitting the whole date and time in one line. Any information that goes beyond a specific length does not appear in the label. I attempted to do two things: 1) make the label larger (this didn't do anything to help with keeping the text within the label) 2) change the "line breaks" option under the attributes inspector to "word wrap". This didn't solve my problem either

How do I make it so that I can include all of the date/time information in the same label, just with wrapped text, so that the text that goes beyond the edge of the screen wraps to the next line?

The code below shows the creation of the view, the screenshots capture what it looks like in the app (some text in "date and time" section not included) and the settings I have set in the storyboard for the label "date and time"

Creating the view:

- (void)configureView
{
    Event *theEvent = self.event;

    static NSDateFormatter *formatter = nil;
    if (formatter == nil) {
        formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"cccc, MMMM dd, yyyy hh:mm a"];
    }
    if (theEvent) {
        self.detailLabel.text = theEvent.detail;
        self.locationLabel.text = theEvent.location;
        self.dateTimeLabel.text = [formatter stringFromDate:(NSDate *)theEvent.date];
    }
}

enter image description here enter image description here

Upvotes: 3

Views: 4899

Answers (1)

Jim
Jim

Reputation: 73936

The line breaks option only works when you set the number of lines to zero and the label is tall enough to contain more than one line. You can call sizeToThatFits: to get an appropriate size for the label.

You can also use the autoshrink option to automatically make the text smaller when needed.

Upvotes: 5

Related Questions