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

ε祈祈猫儿з 提交于 2019-12-29 07:35:28

问题


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


回答1:


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.




回答2:


Use this code it will saw the label in fit to size of your label

label.adjustsFontSizeToFitWidth = YES;



回答3:


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.




回答4:


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

self.label.lineBreakMode = NSLineBreakByWordWrapping;



回答5:


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



回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/15764402/text-not-displaying-on-uilabel-when-the-text-is-too-large

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!