我需要在UILabel
中使用wordwrap
显示两行半行文本,然后在其中添加三个点“...”。
我在UILabel
中有一个UITableViewCell
,当前显示三行文字然后用“...”截断文本但现在我需要显示最后一行减半意味着2.5行大文中的文字。
答案 0 :(得分:0)
答案 1 :(得分:0)
如果你在tableView中有标签
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
下面一行显示你点到底
cell.textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
答案 2 :(得分:0)
只是做一个逻辑..
假设X应该是你允许的最大字符数。
这里我附上样本和X - > 22
#import <CoreText/CoreText.h>
+ (NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label {
NSString *text = [label text];
UIFont *font = [label font];
CGRect rect = [label frame];
CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
[attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)];
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attStr);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
NSMutableArray *linesArray = [[NSMutableArray alloc]init];
for (id line in lines)
{
CTLineRef lineRef = (__bridge CTLineRef )line;
CFRange lineRange = CTLineGetStringRange(lineRef);
NSRange range = NSMakeRange(lineRange.location, lineRange.length);
NSString *lineString = [text substringWithRange:range];
[linesArray addObject:lineString];
}
return (NSArray *)linesArray;
}
+(void) trimUILableForPopupNG:(UILabel*) inputLabel
{
BOOL isTruncated = false;
NSInteger lineCount = 0;
CGSize textSize = CGSizeMake(inputLabel.frame.size.width, MAXFLOAT);
int rHeight = lroundf([inputLabel sizeThatFits:textSize].height);
int charSize = lroundf(inputLabel.font.lineHeight);
lineCount = rHeight/charSize;
if (lineCount > 1) {
// performActionDesc.lineBreakMode = NSLineBreakByTruncatingMiddle;
NSArray *linesArray = [AppData getLinesArrayOfStringInLabel:inputLabel];
NSString *texttoTrim = @"",*texttoAdd = @"";
if (linesArray.count >= 3) {
for (int x = 0; x < 3; x++) {
texttoTrim = [linesArray objectAtIndex:x];
if (x == 2 && texttoTrim.length >= 22) {
int find70_Length = [texttoTrim length]*0.65;
texttoTrim = [[linesArray objectAtIndex:x] substringToIndex:find70_Length];
isTruncated = true;
}
texttoAdd = [texttoAdd stringByAppendingString:texttoTrim];
}
}
else if (linesArray.count >= 2)
{
for (int x = 0; x < 2; x++) {
texttoTrim = [linesArray objectAtIndex:x];
if (x == 1 && texttoTrim.length >= 22) {
int find70_Length = [texttoTrim length]*0.7;
texttoTrim = [[linesArray objectAtIndex:x] substringToIndex:find70_Length];
isTruncated = true;
}
texttoAdd = [texttoAdd stringByAppendingString:texttoTrim];
}
}
if (isTruncated) {
texttoAdd = [texttoAdd stringByAppendingString:@"..."];
inputLabel.text = @"";
inputLabel.text = texttoAdd;
}
}
}
[ClassName trimUILableForPopupNG:yourLabel];
在这里,我编写了添加的代码&#34; ...&#34;显示超过80%字符的2和3行..
如果你想让它为50%只需改变X值..