TIMEX
TIMEX

Reputation: 271934

How do I add a bottom and top border to UITextView?

   let bottomBorder = CALayer()
        let borderWidth = CGFloat(1.0)
        bottomBorder.borderColor = UIColor(hex: 0xf5f5f5).CGColor
        bottomBorder.frame = CGRect(x: 0, y: summary.frame.size.height - borderWidth, width:  summary.frame.size.width, height: summary.frame.size.height)
        bottomBorder.borderWidth = borderWidth


        self.summary.layer.addSublayer(bottomBorder)
        self.summary.layer.masksToBounds = true

I know how to add a bottom border, but when I apply the same principle to "topBorder", one of them disappears.

Upvotes: 0

Views: 2396

Answers (3)

Urvish Modi
Urvish Modi

Reputation: 1138

var bottomBorder = CALayer()

bottomBorder.frame = CGRect(x:0.0, y:self.descriptionTextView.frame.size.height - 1, width:self.descriptionTextView.frame.size.width, height: 1.0)

bottomBorder.backgroundColor = UIColor(hex:K.NODD_RED_HEX).cgColor

self.descriptionTextView.layer.addSublayer(bottomBorder)

Upvotes: 0

Jatin Patel - JP
Jatin Patel - JP

Reputation: 3733

You can add top and bottom boarder using following code. you code have just framing issue.

 let topBorder = CALayer()
        let topHeight = CGFloat(1.0)
        topBorder.borderColor = UIColor.blackColor().CGColor
        topBorder.frame = CGRect(x: 0, y: 0, width:  summary.frame.size.width, height: topHeight)
        topBorder.borderWidth = summary.frame.size.width
        self.summary.layer.addSublayer(topBorder)



        let bottomBorder = CALayer()
        let borderHeight = CGFloat(1.0)
        bottomBorder.borderColor = UIColor.blackColor().CGColor
        bottomBorder.frame = CGRect(x: 0, y: summary.frame.size.height - borderHeight, width:  summary.frame.size.width, height: borderHeight)
        bottomBorder.borderWidth = summary.frame.size.width
        self.summary.layer.addSublayer(bottomBorder)
        self.summary.layer.masksToBounds = true

Upvotes: 2

Frankie
Frankie

Reputation: 11928

I think it's your frames, review and modify this as necessary:

*Note this function was added as an extension to UITextView

func addBorders(color:UIColor) {

    let bottomBorder = CALayer()
    bottomBorder.backgroundColor = color.CGColor
    bottomBorder.frame = CGRectMake(0, CGRectGetHeight(bounds) - 1, CGRectGetWidth(bounds), 1)
    bottomBorder.name = "BottomBorder"
    layer.addSublayer(bottomBorder)

    let topBorder = CALayer()
    topBorder.backgroundColor = color.CGColor
    topBorder.frame = CGRectMake(0, 0, CGRectGetWidth(bounds), 1)
    topBorder.name = "TopBorder"
    layer.addSublayer(topBorder)
}

Upvotes: 0

Related Questions