当UILabel在iOS中的表格单元格中被截断时,添加“查看更多”文本

时间:2023-01-29 22:18:08

I have news feed page kind of facebook. I wanted to add "see more" text to my UILabel(feed content) when its get truncated. I wanted to have action recognition on "see more" text too. These feed content are table cell. So when i tap on "see more" text cell height should also get adjusted according to content.

我有Facebook的新闻提要页面。当它被截断时,我想在我的UILabel(feed内容)中添加“see more”文本。我想对“看到更多”文字进行动作识别。这些饲料含量是表格单元格。因此,当我点击“查看更多”时,文本单元格高度也应根据内容进行调整。

I have attach image for better clarification. Hope one of you guys will have a solution.

我附上图片以便更好地澄清。希望你们其中一个人能有一个解决方案。

当UILabel在iOS中的表格单元格中被截断时,添加“查看更多”文本

1 个解决方案

#1


1  

You can test if the UILabel.sizeThatFits() is greater than your layout space.

您可以测试UILabel.sizeThatFits()是否大于布局空间。

Assuming your cell height is fixed, in the cellForRowAtIndexPath method, do the following

假设您的单元格高度是固定的,请在cellForRowAtIndexPath方法中执行以下操作

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

        //Configure your cell
        let yourLabel : UILabel! //reference to the label which will truncate your text
        let neededSpace = yourLabel.sizeThatFits(CGSize(width: yourLabel.frame.width, height: CGFloat.greatestFiniteMagnitude))

        if neededSpace.height > yourLabel.frame.height {
            //Text will be truncated, show your button
        } else {
            //Text will be fully presented, hide the button
        }

        //your stuff

    }

#1


1  

You can test if the UILabel.sizeThatFits() is greater than your layout space.

您可以测试UILabel.sizeThatFits()是否大于布局空间。

Assuming your cell height is fixed, in the cellForRowAtIndexPath method, do the following

假设您的单元格高度是固定的,请在cellForRowAtIndexPath方法中执行以下操作

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

        //Configure your cell
        let yourLabel : UILabel! //reference to the label which will truncate your text
        let neededSpace = yourLabel.sizeThatFits(CGSize(width: yourLabel.frame.width, height: CGFloat.greatestFiniteMagnitude))

        if neededSpace.height > yourLabel.frame.height {
            //Text will be truncated, show your button
        } else {
            //Text will be fully presented, hide the button
        }

        //your stuff

    }