8

我有一个文本(一本书的故事)。我正在使用 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 ,那么文本最终会显示在视图中。

标签大小=[故事 sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(280, 15000) lineBreakMode:NSLineBreakByWordWrapping];

请帮帮我。谢谢

4

6 回答 6

13

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

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

于 2013-07-19T11:25:32.033 回答
2

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

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

于 2013-07-19T00:33:39.030 回答
1

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

self.label.lineBreakMode = NSLineBreakByWordWrapping;
于 2017-02-21T15:53:33.577 回答
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];
于 2013-04-02T13:23:03.313 回答
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];

希望这会帮助你。

于 2013-07-19T06:46:34.767 回答
-1

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

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

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

它可能会帮助你。

于 2013-07-19T10:18:13.380 回答