【发布时间】:2017-05-01 06:49:33
【问题描述】:
我有一个带有标签的tableView 单元格。
我想制作带有圆角半径的标签。
我知道这样调用函数:
label.layer.cornerRadius = 6
带角的标签如下:
func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let mask = CAShapeLayer()
mask.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
layer.mask = mask
}
但我称这个函数为标签消失。
我的表格视图单元格有什么问题。
标签消失:
我的代码:
class ContentLabel:UILabel {
override init(frame: CGRect) {
super.init(frame:frame)
translatesAutoresizingMaskIntoConstraints = false
numberOfLines = 0
isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func drawText(in rect: CGRect) {
let insets: UIEdgeInsets = UIEdgeInsets(top: defaultContentPadding, left: defaultPadding, bottom: defaultContentPadding, right: defaultPadding)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
class TableViewCell: UITableViewCell {
var labelView:UILabel = { ()->UILabel in
let label:UILabel = ContentLabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byCharWrapping
//label.layer.cornerRadius = 6
label.roundCorners(corners: [.topLeft,.topRight,.bottomLeft], radius: 6)
//What's wrong here!!!!!!!
label.layer.masksToBounds = true
return label
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
loadView()
}
func loadView() {
contentView.addSubview(labelView)
}
override func layoutSubviews() {
super.layoutSubviews()
loadConstraints()
}
func loadConstraints() {
let labelViewWidth:CGFloat = contentView.bounds.width*0.6
labelView.frame = CGRect(x: 0, y: 0, width: labelViewWidth, height: CGFloat.greatestFiniteMagnitude)
labelView.sizeToFit()
let views = DictionaryOfInstanceVariables(self, objects: "labelView","icon")
let metrics = ["padding":10]
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[labelView]-padding-[icon]-padding-|", options: [], metrics: metrics, views: views))
}
}
我也尝试在funclayoutSubviews()中添加代码,我成功换了角,但是当我滚动tableview时,单元格内容消失了,滚动很卡。
【问题讨论】:
标签: ios swift uitableview