Just do with a logic..
- Get no. of lines N
- if N = 1, do NONE
- if N = 2, characters in line 2 > X, chop & X-3 and append “…”
- if N = 3, characters in line 3 > X, chop & X-3 and append “…”
Assume X Should be your max allowed char..
here i attach sample and 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];
Here i wrote code for added "..." 2 and 3 line having more than 80% char displayed..
if you want make it as 50% just change the X value ..