【问题标题】:How to fit label according to its content in UItableView如何根据标签在 UItableView 中的内容适合标签
【发布时间】:2016-09-28 14:01:52
【问题描述】:

我有表格视图,我必须在单元格中显示产品

索引路径中的产品可以是单个或多个

示例:

 ["Burger","SandWitch"]

所以我需要根据 tableview 中的文本修复我的标签。

它不工作。检查我的屏幕截图

我希望单元格高度应该根据标签的内容

检查屏幕截图文本在标签中是否很大,因此它不会在单元格中修复

如何做到这一点?

【问题讨论】:

  • 根据内容搜索uilabel的动态高度和uitableViewCell动态高度..
  • 提供一些你已经实现的代码。

标签: ios swift uitableview


【解决方案1】:

最简单的解决方案:Object-C

tableView.rowHeight = UITableViewAutomaticDimension;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.text = @"text";
    cell.textLabel.numberOfLines = 0;
    return cell;
}

也许你需要这样的东西。斯威夫特 3.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let tableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 50
        tableView.frame = view.bounds
        view.addSubview(tableView)
    }    

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "some text ;)"
        let cell: CustomTableViewCell

        if let cel = tableView.dequeueReusableCell(withIdentifier: identifier) as? CustomTableViewCell {
            cell = cel
        } else {
            cell = CustomTableViewCell(style: .default, reuseIdentifier: identifier)
        }

        cell.labelValue = "what u want"

        return cell
    }
}

你必须继承 UITableViewCell:

class CustomTableViewCell: UITableViewCell {

    var labelValue: String? {
        didSet { update() }
    }
    private let label = UILabel()

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        install()
    }

    private func install() {
        let padding: CGFloat = 6
        label.textColor = .black
        label.numberOfLines = 5
        //label.font = Fonts.Default(.Normal).getFont(17, forDifferentDevice: false)
        label.adjustsFontSizeToFitWidth = true
        label.minimumScaleFactor = 0.7
        label.lineBreakMode = .byTruncatingTail
        label.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(label)

        let cnt1 = NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: contentView, attribute: .leading, multiplier: 1, constant: padding)
        let cnt2 = NSLayoutConstraint(item: contentView, attribute: .trailing, relatedBy: .equal, toItem: label, attribute: .trailing, multiplier: 1, constant: padding)
        let cnt3 = NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1, constant: padding)
        let cnt4 = NSLayoutConstraint(item: contentView, attribute: .bottom, relatedBy: .equal, toItem: label, attribute: .bottom, multiplier: 1, constant: padding)
        contentView.addConstraints([cnt1, cnt2, cnt3, cnt4])
    }

    private func update() {
        label.text = labelValue
    }

}

【讨论】:

  • 第二..时刻
  • 不支持语法
  • 更新到 Swift 3.0。再试一次。
  • didSet 条件不满足
  • 没办法。 Xcode 8. Swift 3. 一切正常
【解决方案2】:

使用此功能可以根据文字设置标签

 func heightForView(text:String, #font:UIFont, #width:CGFloat) -> CGFloat{
        let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.ByWordWrapping
        label.font = font
        label.text = text

        label.sizeToFit()
        return label.frame.height
    }

    let font = UIFont(name: "Helvetica", size: 20.0)

    var height = heightForView("This is just a load of text", font: font, width: 100.0)

【讨论】:

  • 比cell.lbl.size.frame.height = height这样吗?
  • 如何结合你给出的两个答案
  • 这一项根据测试设置标签,并将单元格设置为文本大小
【解决方案3】:

使用heightForRowAt 中的NSAttributedString 计算行的高度:

func tableView(tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
     let attString = NSAttributedString(string:"YOUR TEXT", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(15.0)])
     var r: CGRect = attString.boundingRectWithSize(CGSizeMake(self.tableView.bounds.size.width, CGFLOAT_MAX), options: .UsesLineFragmentOrigin, context: nil)

     return r.size.height
}

【讨论】:

    猜你喜欢
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多