【问题标题】:Swift - label corner have different radius in tableviewcellSwift - 标签角在 tableviewcell 中具有不同的半径
【发布时间】: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


    【解决方案1】:

    试试这个:

    在情节提要中将约束应用于标签(w.r.to 单元格视图,如果您添加它):

    1. 顶部布局,
    2. 领先,
    3. 图像之间的水平间距。

    【讨论】:

      猜你喜欢
      • 2022-10-17
      • 2012-08-27
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      相关资源
      最近更新 更多