在UILabel

时间:2016-10-19 11:32:01

标签: ios objective-c swift uilabel

我需要在UILabel中使用wordwrap显示两行半行文本,然后在其中添加三个点“...”。

我在UILabel中有一个UITableViewCell,当前显示三行文字然后用“...”截断文本但现在我需要显示最后一行减半意味着2.5行大文中的文字。

3 个答案:

答案 0 :(得分:0)

你应该去界面构建器,或者去做代码,但你需要实现的是通过将行数从1或0更改为3来完成,如下图所示:enter image description here

这就是你需要的吗?

答案 1 :(得分:0)

如果你在tableView中有标签

cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;    

下面一行显示你点到底

cell.textLabel.lineBreakMode = NSLineBreakByTruncatingTail;

答案 2 :(得分:0)

只是做一个逻辑..

  1. 得不到。 N行
  2. 如果N = 1,请执行NONE
  3. 如果N = 2,则第2行中的字符> X,chop& X-3并追加“......”
  4. 如果N = 3,则第3行中的字符> X,chop& X-3并追加“......”
  5. 假设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值..