动态显示标签并在标签末尾添加 "read more" 后缀时出现问题

Issue while displaying label dynamically and "read more" suffix at end of label

我正在尝试实现以下想法。

说明应显示在高度不超过 4 行的动态大小的表格视图单元格中。如果它超过 4 行,则只显示其中的一部分并以省略号结尾,即 (...)

我得到如下屏幕所示的输出

我想要的是,tableView 单元格必须 show/display 动态大小。标题固定为 1 行,描述标签必须最多 4 行,如果它有该数据并且 data/text 超过 4 行 ...那么我们必须显示 (...) 在标签的末尾有 4 行。请检查下图

如果描述文本包含 1 行或 2 行数据,则必须显示如下所示的普通文本。

我该如何解决这个问题?或者是否有任何实施方案?

Here is the project

在这里我尝试了不同的解决方案。我创建了一个函数,returns 你在 String 中的前 4 行标签。然后在字符串末尾添加 (...)

*将标签的 numberOfLines 设置为 0

*假设 leadingtrailing space 标签 = 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
        }
}

你的cellForRowAt方法

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
}

Here

下载演示项目