卡片可以限制视野宽度吗?
Can card be restricted to width of view?
我正在尝试显示一张卡片,但是当 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)
}
}
我在示例项目中尝试了您的代码片段,似乎一切正常。我使用了这个代码
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 响应正确。
所以我怀疑您的 "view" 可能太宽了。你能确认你的 "width" of the ViewController 最多是设备的宽度吗?
所以问题是您使用的是 UITableViewController。当你将CardView添加到你的视图属性时,它实际上是将它添加到UITableView,这将导致它在你滚动tableView时滚动。它不能正确扩展的另一个问题是由于“|” AutoLayout 中的值,放在 TableView 中时似乎会中断。这可能是 TableView 的数学设置方式的结果。
无论哪种方式,为了避免滚动问题,您都需要使用 UIViewController 并将 UITableView 添加为它的子视图,或者如果您热衷于使用 UITableViewController,它应该作为子项 ViewController 添加到父项 ViewController,您也可以添加 CardView。这最终也将解决您的边界问题。
在 Material repo -> Examples/Programmatic/SideViewController 示例项目中,您可以找到一个添加为子视图的 tableView,在那里,您可以像之前那样放置 CardView 代码在您当前的项目中这样做。
我正在尝试显示一张卡片,但是当 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)
}
}
我在示例项目中尝试了您的代码片段,似乎一切正常。我使用了这个代码
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 响应正确。
所以我怀疑您的 "view" 可能太宽了。你能确认你的 "width" of the ViewController 最多是设备的宽度吗?
所以问题是您使用的是 UITableViewController。当你将CardView添加到你的视图属性时,它实际上是将它添加到UITableView,这将导致它在你滚动tableView时滚动。它不能正确扩展的另一个问题是由于“|” AutoLayout 中的值,放在 TableView 中时似乎会中断。这可能是 TableView 的数学设置方式的结果。
无论哪种方式,为了避免滚动问题,您都需要使用 UIViewController 并将 UITableView 添加为它的子视图,或者如果您热衷于使用 UITableViewController,它应该作为子项 ViewController 添加到父项 ViewController,您也可以添加 CardView。这最终也将解决您的边界问题。
在 Material repo -> Examples/Programmatic/SideViewController 示例项目中,您可以找到一个添加为子视图的 tableView,在那里,您可以像之前那样放置 CardView 代码在您当前的项目中这样做。