如何向UITextView添加底部和顶部边框?

时间:2015-07-16 05:57:58

标签: ios swift 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

我知道如何添加底部边框,但是当我将相同的原则应用于" topBorder"时,其中一个消失了。

3 个答案:

答案 0 :(得分:1)

您可以使用以下代码添加顶部和底部边框。你的代码只是框架问题。

 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

答案 1 :(得分:0)

我认为这是你的框架,根据需要进行审查和修改:

*请注意,此功能已添加为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)
}

答案 2 :(得分:0)

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)