如何在多行中显示带有长文本的tableview节标题? [英] How to display tableview section headers with long text in multiple rows?

查看:80
本文介绍了如何在多行中显示带有长文本的tableview节标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个具有多个部分的表视图控制器,其中一些部分具有长文本.当我使用titleForHeaderSection时,如果文本的长度大于tableview框架,则文本将被截断.我想在下一行显示文本.

I am using a table view controller that has multiple sections, with a few sections having long text. When I use titleForHeaderSection, text is getting truncated if length of the text is more than the tableview frame. I want to display the text in the next row.

推荐答案

而不是使用titleForHeaderInSection,请尝试使用viewForHeaderInSection.此方法要求您返回视图,但是由于UILabelUIView的子类,因此您还可以返回标签!

Instead of using titleForHeaderInSection, try using viewForHeaderInSection. This method asks for you to return a view, but since UILabel is a subclass of UIView, you can also return a label!

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let label = UILabel()
    label.backgroundColor = UIColor.black
    label.textColor = UIColor.white
    label.lineBreakMode = .byWordWrapping
    label.numberOfLines = 0
    label.text = "Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They're not fond of rules. And they have no respect for the status quo. You can quote them, disagree with them, glorify or vilify them. About the only thing you can't do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do."
    return label
}

您会注意到,使用这种方法,文本一直到表格视图的左右边缘.在大多数情况下,这看起来不太好,尤其是当您的表格视图占据了设备的整个宽度时.

You'll notice that using this approach, the text goes all the way to the left and right edges of the table view. In most cases, this doesn't look very good, especially if your table view takes up the full width of your device.

一个简单的解决方法:创建UILabel的自定义子类(我将其命名为TableViewHeaderLabel).在此子类内,我们将向标签添加插入项.我们还可以将自动换行,背景色等移入此标签,以使我们的ViewController文件更加整洁!

A simple fix for this: create a custom subclass of UILabel (I'll call mine TableViewHeaderLabel). Inside this subclass, we'll add insets to the label. We can also move the word wrapping, background color, etc. into this label, in order to make our ViewController file even cleaner!

这是TableViewHeaderLabel.swift的样子:

import UIKit

class TableViewHeaderLabel: UILabel {

    var topInset:       CGFloat = 0
    var rightInset:     CGFloat = 12
    var bottomInset:    CGFloat = 0
    var leftInset:      CGFloat = 12

    override func drawText(in rect: CGRect) {

        let insets: UIEdgeInsets = UIEdgeInsets(top: self.topInset, left: self.leftInset, bottom: self.bottomInset, right: self.rightInset)
        self.setNeedsLayout()

        self.textColor = UIColor.white
        self.backgroundColor = UIColor.black

        self.lineBreakMode = .byWordWrapping
        self.numberOfLines = 0

        return super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
    }

}

然后使用它看起来像这样:

And then using it would look like this:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let label = TableViewHeaderLabel()
    label.text = "Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They're not fond of rules. And they have no respect for the status quo. You can quote them, disagree with them, glorify or vilify them. About the only thing you can't do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do."
    return label
}

这篇关于如何在多行中显示带有长文本的tableview节标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆