【问题标题】:IOS how to truncate with ellipsis the last visible line of a paragraph?IOS如何用省略号截断段落的最后一个可见行?
【发布时间】:2016-11-20 20:33:59
【问题描述】:

我想绘制一个段落(所以我想使用 CTFrameDraw 或 ctlinedraw),但如果文本不适合定义的区域(矩形),我还需要截断文本。通常我们通过附加省略号来修剪最后一行(即字符'...') 怎么做?如何使用特殊字符(如 '...[+]'

另外,我想知道如何为第一行指定 缩进(以像素为单位)以及是否可以定义自定义 linespacing

最后,我需要知道完全适合整个段落的最终矩形(它可能低于定义的区域)

【问题讨论】:

    标签: ios objective-c cocoa-touch core-graphics core-text


    【解决方案1】:
     NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.lineBreakMode = NSLineBreakByTruncatingTail;
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, [attributedString length])];
        descriptionLabel.attributedText = attributedString;
    

    使用上面的代码和UIelement 这样的标签或按钮。U 将在最后一行得到省略号。而且我也尝试在末尾附加字符串,但我没有。因为在有限的区域内,标签只显示适合信息的剩余隐藏。在那条线上意味着除了省略号之外我尝试了一些文本。抱歉提供的信息较少。

    【讨论】:

      【解决方案2】:

      将以下代码复制到测试项目中的viewDidLoad。

      // Set the size of the area the paragraph will be drawn in.
      CGSize sizeOfTextArea = CGSizeMake(200.0f, 100.0f);
      
      // Sample text and views.
      NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.";
      UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, sizeOfTextArea.width, sizeOfTextArea.height)];
      imageView.backgroundColor = [UIColor whiteColor];
      [self.view addSubview:imageView];
      self.view.backgroundColor = [UIColor blackColor];
      CGRect textRect = imageView.frame;
      UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
      moreBtn.frame = CGRectMake(CGRectGetMaxX(textRect)-45, CGRectGetMaxY(textRect), 45, 20);
      [moreBtn setTitle:@"more" forState:UIControlStateNormal];
      [self.view addSubview:moreBtn];
      
      // Create a paragraph style and add it to attributed text options.
      NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
      style.firstLineHeadIndent = 10.0f;  // <- CHANGE THIS TO ADJUST INDENT
      style.lineSpacing = 10.0f;          // <- CHANGE THIS TO ADJUST LINE SPACING
      NSDictionary *attributes = @{NSParagraphStyleAttributeName : style};
      
      // Render the text
      // The options set the text to word-wrap and to add an ellipsis if needed.
      UIGraphicsBeginImageContext(sizeOfTextArea);
      NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
      [attributedText drawWithRect:CGRectMake(0, 0, sizeOfTextArea.width, sizeOfTextArea.height)
                           options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine
                           context:nil];
      UIImage *renderedText = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      
      // Display the rendered text.
      imageView.image = renderedText;
      
      // Calculate the rect for the full text.  We fix the width and let iOS calculate the height.
      CGRect fullTextRect = [attributedText boundingRectWithSize:CGSizeMake(sizeOfTextArea.width, CGFLOAT_MAX)
                                                         options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                                         context:nil];
      NSLog(@"full rect: %@", NSStringFromCGRect(fullTextRect));
      
      // Show/hide more button depending on whether there's more text to show
      moreBtn.hidden = CGRectGetHeight(fullTextRect) <= sizeOfTextArea.height;
      

      至于省略号后的[+],在显示或隐藏的文本之后添加一个“更多”按钮,具体取决于是否有更多文本要显示(请参阅上面的代码以了解我的意思)。

      【讨论】:

      • 感谢 jp2g,但我不明白如何将 '...' 替换为 '...[+]' 或 "... more" 之类的东西?
      • @lok​​i :我更新了示例代码以包含一个“更多”按钮。
      • 哦,我知道你是怎么做的,实际上你把按钮放在了文本的顶部:(这是一个想法,但这不会返回一个漂亮的结果,因为文本会在任何地方被截断,就像在字符中间的例子:(
      猜你喜欢
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 2014-02-15
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多