且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在多行中显示带有长文本的tableview节标题?

更新时间:2023-01-16 15:16:59

而不是使用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
}