Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 917
user2019279 Avatar asked Apr 02 '13 12:04

user2019279


People also ask

How do I make my text bold on UILabel?

Write the following text in the label “Bold Regular”Double Click on Bold to select it, and then right click on it to see more options. Select font > Bold from that option. It should do the task.


2 Answers

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.

like image 198
Mike Weller Avatar answered Oct 26 '22 08:10

Mike Weller


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.

like image 38
Chris Chen Avatar answered Oct 26 '22 07:10

Chris Chen