文本太大时,文本不显示在UILabel上。

我有一个文本(一本书的故事)。 我正在带UILabel来展示它。 但它没有让我看到。 我使用以下代码:

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

当我调试我的代码时,我发现labelize.height出现在我的情况下13145.Still它没有显示。 如果我减少15000到11000,那么文本最终会显示在….

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

请帮帮我。 谢谢

UILabel会立即将其所有文本呈现到后备缓冲区中。 iOS设备只有有限的图形内存才能完成这项工作,因此一旦图形数据超过一定大小,它就会失败。

对于大量文本,您应该使用Core Text或UITextView东西,它可以更有效地按需呈现文本。

使用此代码,它将看到符合您标签大小的标签

 label.adjustsFontSizeToFitWidth = YES; 

我最近经历过同样的事情,UILabel大小分配正确,它只是UILabel本身不能容纳太多字符,最大字符数限制取决于字体和字体大小。

对于我使用的字体,限制大约是13k个字符。 我的工作是手工截断字符串,但这并不理想。

您需要在标签上添加测试包装,否则它将从边缘流出。

 self.label.lineBreakMode = NSLineBreakByWordWrapping; 
  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]; 

如果你想显示超过1000行文本,那么最好使用UITextView而不是Uilabel。 您可以将以下代码用于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]; 

希望这会帮助你。

您可以在UILabel中放置多行文本来解决您的问题。

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

礼貌: – https://stackoverflow.com/a/990244/1865424

它可能会帮助你。