0

I am trying to implement following idea.

enter image description here

The description should be shown in a dynamically sized tableview cell with height not exceeding 4 lines. In case it exceeds 4 lines, only part of it should be shown ending it with ellipsis i.e (...)

I used solution from this stack overflow link

I am getting output like following screen

enter image description here

What I want is that, tableView cell has to show/display dynamic size. The title is fixed 1 line and the description label has to max 4 lines if it has that data and of that data/text exceeds 4 lines ...then we have to show (...) at the end of label with 4 lines. Please check following image

enter image description here

If description text contains 1 or 2 lines data it has to show normal text like following.

enter image description here

How I can fix this issue ? or is there any option to implement it?

Here is the project

Nikita Patil
  • 674
  • 1
  • 7
  • 17

1 Answers1

2

Here i have tried with different solution. I have created a function which returns you first 4 lines of label in String. Then append (...) at the end of string.

*Set label's numberOfLines to 0

*Assuming leading and trailing space of label = 20.

func getLinesFromLabel(label:UILabel) -> String? {

        let text:NSString = label.text! as NSString
        let font:UIFont = label.font

        let myFont:CTFont = CTFontCreateWithName(font.fontName as CFString, font.pointSize, nil)
        let attStr:NSMutableAttributedString = NSMutableAttributedString(string: text as String)
        attStr.addAttribute(NSAttributedString.Key(rawValue: String(kCTFontAttributeName)), value:myFont, range: NSMakeRange(0, attStr.length))
        let frameSetter:CTFramesetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
        let path:CGMutablePath = CGMutablePath()
        //set width of label here (i assumed leading and trailing is 20)
        path.addRect(CGRect(x:0, y:0, width:UIScreen.main.bounds.width - 50, height:100000))

        let frame:CTFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
        let lines = CTFrameGetLines(frame) as NSArray
        // if lines are more than 4 then return first 4 lines
        if lines.count > 4 {
            var str = ""
            for line in 0..<4 {
                let lineRange = CTLineGetStringRange(lines[line] as! CTLine)
                let range:NSRange = NSMakeRange(lineRange.location, lineRange.length)
                let lineString = text.substring(with: range)
                str += lineString
            }
            let updatedStr = str.suffix(17)
            str = String(str.dropLast(17))
            let strArr = updatedStr.components(separatedBy: " ")

            if strArr.count > 2 {
                if strArr[0].count < 5 {
                    str += strArr[0]
                }
            }
            return str
        } else {
            return nil
        }
}

your cellForRowAt method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "NoticeListTableViewCell") as! NoticeListTableViewCell

    cell.titleLabel.text = titleArray[indexPath.row]
    cell.descriptionLabel.text = descriptionArray[indexPath.row]

    let readmoreFont = UIFont(name: "Helvetica Neue", size: 16.0)!
    let readmoreFontColor = UIColor.blue

    let lines = getLinesFromLabel(label: cell.descriptionLabel)

    if let first4line = lines {
        print(first4line)
        let answerAttributed = NSMutableAttributedString(string: first4line, attributes: [NSAttributedString.Key.font: cell.descriptionLabel.font!])
        let readMoreAttributed = NSMutableAttributedString(string: " (...)", attributes: [NSAttributedString.Key.font: readmoreFont, NSAttributedString.Key.foregroundColor: readmoreFontColor])
        answerAttributed.append(readMoreAttributed)
        cell.descriptionLabel.attributedText = answerAttributed
    }

    return cell
}

enter image description here

Download Demo Project From Here

Saifan Nadaf
  • 1,715
  • 1
  • 17
  • 28