IOS如何用省略号截断段落的最后一行?

lok*_*oki 5 cocoa-touch core-graphics objective-c core-text ios

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

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

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

jp2*_*p2g 0

将以下代码复制到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;
Run Code Online (Sandbox Code Playgroud)

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