user2019279
user2019279

Reputation: 163

Text not displaying on UILabel when the text is too large.

I have a text (Story of a book). I am taking UILabel to display it. But it is not showing me on view. I am using the following code:

    CGSize labelsize;
    UILabel *commentsTextLabel = [[UILabel alloc] init];;
    [commentsTextLabel setNumberOfLines:0];
    commentsTextLabel.textColor = [UIColor blackColor];
    [commentsTextLabel setBackgroundColor:[UIColor clearColor]];
    [commentsTextLabel setFont:[UIFont fontWithName:@"ACaslonPro-Regular"size:17]];
    labelsize=[story sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(280, 15000) lineBreakMode:NSLineBreakByWordWrapping];
    commentsTextLabel.frame=CGRectMake(20, 200, 280, labelsize.height);
   commentsTextLabel.text = story;// more than 1000 lines

   [self.view addSubview:commentsTextLabel];

When i debug my code, i found labelsize.height is coming out in my case 13145.Still it is not showing. If i descrease 15000 to 11000 , then text is showing on view with .... at last.

labelsize=[story sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(280, 15000) lineBreakMode:NSLineBreakByWordWrapping];

Please help me out. Thanks

Upvotes: 8

Views: 10284

Answers (6)

user6286625
user6286625

Reputation:

You need to add test wrapping to the label or it will flow off the edges.

self.label.lineBreakMode = NSLineBreakByWordWrapping;

Upvotes: 1

Kundan
Kundan

Reputation: 3082

you can put multiple lines of text in UILabel to solve your issue.

textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
textLabel.adjustsFontSizeToFitWidth = YES;

Courtesy:-https://stackoverflow.com/a/990244/1865424

It may help you.

Upvotes: -2

Mike Weller
Mike Weller

Reputation: 45598

UILabel will render all its text at once into a backing buffer. iOS devices only have a limited amount of graphics memory to do this work, so it will fail once the graphics data goes above a certain size.

For large amounts of text you should use Core Text or something like UITextView which renders its text on-demand much more efficiently.

Upvotes: 13

hpp
hpp

Reputation: 619

If u want to display more than 1000 lines of text then its better to use UITextView instead of Uilabel. you can use the following code for UITextView

UITextView *commentsTextLabel = [[UITextView alloc] init];
    commentsTextLabel.frame=CGRectMake(20, 20, 280,100);
    commentsTextLabel.text = story;
    commentsTextLabel.editable=NO;
    commentsTextLabel.scrollEnabled=YES;
    //commentsTextLabel.numberOfLines=1;
    commentsTextLabel.textColor = [UIColor blackColor];
    //[commentsTextLabel setBackgroundColor:[UIColor clearColor]];
    //[commentsTextLabel setFont:[UIFont systemFontOfSize:17]];
    commentsTextLabel.font=[UIFont systemFontOfSize:17];
    commentsTextLabel.backgroundColor=[UIColor clearColor];
    [self.view addSubview:commentsTextLabel];

hope this will help you.

Upvotes: 0

Chris Chen
Chris Chen

Reputation: 5407

I experienced the same thing lately, the UILabel size is allocated correctly, it's just the UILabel itself cannot hold too many characters, the maximum character limit depends on font and font size.

For the font I used, the limit is around 13k characters. My work around is to truncate the string before hand, it's not ideal though.

Upvotes: 2

08442
08442

Reputation: 521

    CGSize labelsize;
    UILabel *commentsTextLabel = [[UILabel alloc] init];;
    [commentsTextLabel setNumberOfLines:0];
    commentsTextLabel.textColor = [UIColor blackColor];
    commentsTextLabel.text = @"The sediment-free enrichment (3) was cultivated anaerobically either in a mineral medium as described previously (19) with yeastextract (0.5%) as a carbon source or in autoclaved (1218C for 40 min) spentenrichment culture medium adjusted to pH 8.3 with sterile anaerobic 2 N NaOH.The spent culture medium was obtained by centrifugation (with anaerobic centrifuge tubes) of the enrichment culture grown in 0.5% yeast extract medium.The pH was kept within 8.0 to 8.5 by adjustment with anaerobic sterile 2 NNaOH. D. dehalogenans JW/IU-DC1 was grown in the base medium describedpreviously (19) supplemented as indicated in the text at 378C and pH 7.5.";
    [commentsTextLabel setBackgroundColor:[UIColor clearColor]];
    [commentsTextLabel setFont:[UIFont fontWithName:@"ACaslonPro-Regular"size:17]];
    labelsize=[commentsTextLabel.text sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(280, 15000) lineBreakMode:UILineBreakModeWordWrap];
    commentsTextLabel.frame=CGRectMake(20, 200, 280, labelsize.height);
        [self.view addSubview:commentsTextLabel];

Upvotes: 0

Related Questions