1

I need to show two and half line text in UILabel with wordwrap and then append three dots "..." to it.

I have a UILabel in UITableViewCell which is currently showing three lines of text and then truncating the text with "..." but now I need to show the last line halved means 2.5 line of text out of a big text.

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41

3 Answers3

0

you should go to interface builder, or do it but code, but what you need to achieve is done by changing the number of lines from 1 or 0 to 3, like this image: enter image description here

Is that what you need right?

Mago Nicolas Palacios
  • 2,501
  • 1
  • 15
  • 27
0

If you have label in tableView

cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;    

Below line shows you dot at end

cell.textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
user3182143
  • 9,459
  • 3
  • 32
  • 39
0

Just do with a logic..

  1. Get no. of lines N
  2. if N = 1, do NONE
  3. if N = 2, characters in line 2 > X, chop & X-3 and append “…”
  4. 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 ..

abdul sathar
  • 2,395
  • 2
  • 28
  • 38