swift label only border left

前端 未结 6 1257
[愿得一人]
[愿得一人] 2021-02-03 12:13

good morning together,

i have a tableview like this:

Example: in cell one i have got an red text label on the right side. left from it i include an image like a

6条回答
  •  北海茫月
    2021-02-03 12:58

    These solutions worked for me, however I was using tableview rows and the height of the row was dynamically set based on how much content was in the rows.

    For the above solutions I found that if the size of the row was dynamically increased because of the content within the row, the border's size didn't increase. So I had an incomplete border on rows that were larger than normal.

    I came across this solution from @Debaprio B at this link (Oct 11, 2017 post): UIView set only side borders

    His solution worked for me, and the border's sized changed appropriately when my row's size increased dynamically. I'm sharing @Debaprio's answer below for brevity:


    public extension UIView {
    // Border type and arbitrary tag values to identify UIView borders as subviews
    
    public enum BorderType: Int {
    case left = 20000
    case right = 20001
    case top = 20002
    case bottom = 20003
    }
    
    public func addBorder(borderType: BorderType, width: CGFloat, color: UIColor {
    // figure out frame and resizing based on border type
    var autoresizingMask: UIViewAutoresizing
    var layerFrame: CGRect
    
    switch borderType {
    
    case .left:
        layerFrame = CGRect(x: 0, y: 0, width: width, height: self.bounds.height)
        autoresizingMask = [ .flexibleHeight, .flexibleRightMargin ]
    case .right:
        layerFrame = CGRect(x: self.bounds.width - width, y: 0, width: width, height: self.bounds.height)
        autoresizingMask = [ .flexibleHeight, .flexibleLeftMargin ]
    case .top:
        layerFrame = CGRect(x: 0, y: 0, width: self.bounds.width, height: width)
        autoresizingMask = [ .flexibleWidth, .flexibleBottomMargin ]
    case .bottom:
        layerFrame = CGRect(x: 0, y: self.bounds.height - width, width: self.bounds.width, height: width)
        autoresizingMask = [ .flexibleWidth, .flexibleTopMargin ]
    }
    
    // look for the existing border in subviews
    var newView: UIView?
    for eachSubview in self.subviews {
        if eachSubview.tag == borderType.rawValue {
            newView = eachSubview
            break
        }
    }
    

提交回复
热议问题