【问题标题】:Text not displaying on UILabel when the text is too large.文本太大时,UILabel 上不显示文本。
【发布时间】:2013-04-02 12:42:05
【问题描述】:

我有一个文本(一本书的故事)。我正在使用 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 中出现。但它仍然没有显示。 如果我将 15000 减少到 11000 ,那么文本最终会显示在视图中。

labelsize=[故事 sizeWithFont:cmetsTextLabel.font constrainedToSize:CGSizeMake(280, 15000) lineBreakMode:NSLineBreakByWordWrapping];

请帮帮我。 谢谢

【问题讨论】:

  • 你为什么不用 UITextView....?
  • 使用不可编辑的UITextView不是更好吗?否则,请尝试在计算标签大小时将 MAXFLOAT 设置为高度约束。
  • 这里的labelsize是指什么类型的变量?
  • 我不想在某些部分使用滚动,我的要求是滚动整个视图。这就是我不使用 UITextView 的原因。
  • 我用一些较小的文本来调试 labelize.height 是正确的。有人可以在你最后实现吗?

标签: iphone ios objective-c uilabel


【解决方案1】:

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

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

【讨论】:

  • 我给予信心。我遇到了同样的问题,并一起使用了几个UILabels 解决了它。当我在 iPad 2 上尝试时,内存不足,应用程序崩溃了。
【解决方案2】:

我最近也遇到了同样的情况,UILabel大小分配正确,只是UILabel本身不能容纳太多字符,最大字符限制取决于字体和字体大小。

对于我使用的字体,限制为大约 13k 个字符。我的解决方法是事先截断字符串,但这并不理想。

【讨论】:

  • mem 还是值得关注的,所以截断是明智的。
  • UILabel 将立即将所有内容呈现到后备缓冲区中。 iOS 设备只有有限数量的图形内存来完成这项工作,所以看起来它在一定数量的数据后会失败。使用核心文本或类似 UITextView 的东西,旨在更有效地绘制内容。
【解决方案3】:

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

self.label.lineBreakMode = NSLineBreakByWordWrapping;

【讨论】:

    【解决方案4】:
        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】:

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

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

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

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

      也许对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-18
        • 2015-09-27
        • 1970-01-01
        相关资源
        最近更新 更多