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

时间:2013-04-02 12:42:06

标签: iphone ios objective-c 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];

请帮帮我。 感谢

7 个答案:

答案 0 :(得分:9)

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

对于大量文本,您应该使用核心文本或类似UITextView的内容,以便更有效地呈现其文本。

答案 1 :(得分:3)

使用此代码,它会看到标签符合标签尺寸

label.adjustsFontSizeToFitWidth = YES;

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

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

self.label.lineBreakMode = NSLineBreakByWordWrapping;

答案 4 :(得分:0)

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

答案 5 :(得分:0)

如果你想显示超过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];

希望这会对你有所帮助。

答案 6 :(得分:-1)

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

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

提供者: - https://stackoverflow.com/a/990244/1865424

它可能对你有帮助。