0

我正在尝试显示一张卡片,但是当卡片detailTextLabel过长时,它会导致卡片超出视图。我不确定这是否是一个错误。我已将其调整.numberOfLines为各种数字,但这似乎没有效果。

    // Detail label.
    let detailLabel: UILabel = UILabel()
    detailLabel.text = "When this text gets too long it does not wrap, it will extend off the page"
    detailLabel.font = UIFont(name: "Roboto-Thin", size: 18)
    detailLabel.numberOfLines = 0
    cardView.detailLabel = detailLabel

超出页面的卡片示例。

编辑

这是cardView的完整代码。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cardView: CardView = CardView()

    // Title label.
    let titleLabel: UILabel = UILabel()
    titleLabel.text = self.type[indexPath.row].capitalizedString
    titleLabel.textColor = MaterialColor.blue.darken1
    titleLabel.font = UIFont(name: "Roboto-Medium", size: 23)
    cardView.titleLabel = titleLabel

    // Detail label.
    let detailLabel: UILabel = UILabel()
    detailLabel.text = "When this text gets too long it does not wrap, it will extend off the page"
    detailLabel.font = UIFont(name: "Roboto-Thin", size: 18)
    detailLabel.numberOfLines = 100
    cardView.detailLabel = detailLabel

    // Yes button.
    let btn1: FlatButton = FlatButton()
    btn1.pulseColor = MaterialColor.blue.lighten1
    btn1.pulseScale = false
    btn1.setTitle("Ok", forState: .Normal)
    btn1.setTitleColor(MaterialColor.blue.darken1, forState: .Normal)


    // Add buttons to left side.
    cardView.leftButtons = [btn1]

    // To support orientation changes, use MaterialLayout.
    view.addSubview(cardView)
    cardView.translatesAutoresizingMaskIntoConstraints = false
    MaterialLayout.alignFromTop(view, child: cardView, top: self.view.frame.height / 4)
    MaterialLayout.alignToParentHorizontally(view, child: cardView, left: 10, right: 10)
}

}

4

2 回答 2

1

So the issue is that you are using a UITableViewController. When you add the CardView to your view property, it is actually adding it to the UITableView, which will cause it to scroll when you scroll the tableView. The other issue of it not expanding correctly is due to the "|" value in AutoLayout, that seems to be breaking when placed in a TableView. This is probably a result of how the TableView is setup mathematically.

Either way, to avoid the scrolling issue, you will need to use a UIViewController and add a UITableView as a child view to it, or if you are keen on using the UITableViewController, it should be added as a child ViewController to a parent ViewController that you would add your CardView too. This will ultimately solve your bounds issue as well.

在 Material repo -> Examples/Programmatic/SideViewController 示例项目中,您可以找到一个添加为子视图的 tableView,在那里,您可以像在当前项目中那样放置 CardView 代码。

于 2016-02-13T16:57:07.677 回答
0

我在示例项目中尝试了您的代码片段,一切似乎都有效。我用了这段代码

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cardView: CardView = CardView()

    // Title label.
    let titleLabel: UILabel = UILabel()
    titleLabel.text = "Activities dhajshd ajshd ashd kjashd jkasd jk asjkd asjk d"
    titleLabel.textColor = MaterialColor.blue.darken1
    titleLabel.font = UIFont(name: "Roboto-Medium", size: 23)
    cardView.titleLabel = titleLabel

    // Detail label.
    let detailLabel: UILabel = UILabel()
    detailLabel.text = "When this text gets too long it does not wrap, it will extend off the page"
    detailLabel.font = UIFont(name: "Roboto-Thin", size: 18)
    detailLabel.numberOfLines = 100
    cardView.detailLabel = detailLabel

    // Yes button.
    let btn1: FlatButton = FlatButton()
    btn1.pulseColor = MaterialColor.blue.lighten1
    btn1.pulseScale = false
    btn1.setTitle("Ok", forState: .Normal)
    btn1.setTitleColor(MaterialColor.blue.darken1, forState: .Normal)


    // Add buttons to left side.
    cardView.leftButtons = [btn1]

    // To support orientation changes, use MaterialLayout.
    view.addSubview(cardView)
    cardView.translatesAutoresizingMaskIntoConstraints = false
    MaterialLayout.alignFromTop(view, child: cardView, top: self.view.frame.height / 4)
    MaterialLayout.alignToParentHorizontally(view, child: cardView, left: 10, right: 10)
}

标题和详细信息标签对于单行来说都太长,并且 CardView 会做出适当的响应。

在此处输入图像描述

所以我怀疑你的“观点”可能太广泛了。你能确认你的 ViewController 的“宽度”最多是设备的宽度吗?

于 2016-02-12T14:38:10.587 回答