Swift - 如何制作包含标签的表格

时间:2016-07-25 05:22:26

标签: ios swift

这就是我想要实现的目标。

enter image description here

我自己尝试编码,但第一个外视图边框没有出现。

这是我的代码:

import UIKit

class InfoTableView: UIView {

    override func drawRect(rect: CGRect) {

        self.backgroundColor = UIColor.whiteColor()

        let outerBorder = UIColorCode.init(hexString: "#666666")
    let startingTopPoint   = CGPoint(x: rect.minX, y: rect.minY)
    let endingTopPoint  = CGPoint(x: rect.maxX, y: rect.minY)

    let startingPoint   = CGPoint(x: rect.minX, y: rect.maxY)
    let endingPoint     = CGPoint(x: rect.maxX, y: rect.maxY)

        // top
        let tpPath = UIBezierPath()
        tpPath.moveToPoint(startingPoint)
        tpPath.addLineToPoint(endingTopPoint)
        tpPath.lineWidth = 2.0

        outerBorder.setStroke()
        tpPath.stroke()

        // bottom
        let btPath = UIBezierPath()

        btPath.moveToPoint(startingPoint)
        btPath.addLineToPoint(endingPoint)
        btPath.lineWidth = 2.0

        outerBorder.setStroke()
        btPath.stroke()

    }
}

顶部和底部有外边框。但只有底部出现。我不知道哪里出错了。

1 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些编辑。试试它是否适合你。

override func drawRect(rect: CGRect) {
        // Drawing code
        self.backgroundColor = UIColor.whiteColor()
        let outerBorder = UIColor.redColor()
        let lineWidth : CGFloat = 2.0
        let insetRect = rect.insetBy(dx: lineWidth/2, dy: lineWidth/2)
        let startingTopPoint   = CGPointMake(insetRect.origin.x,insetRect.origin.y)
        let endingTopPoint  = CGPoint(x: insetRect.maxX, y: insetRect.minY)

        let startingPoint   = CGPoint(x: insetRect.minX, y: insetRect.maxY)
        let endingPoint     = CGPoint(x: insetRect.maxX, y: insetRect.maxY)

        // top
        let tpPath = UIBezierPath()
        tpPath.moveToPoint(startingTopPoint)
        tpPath.addLineToPoint(endingTopPoint)
        tpPath.lineWidth = 2.0

        outerBorder.setStroke()
        tpPath.stroke()

        // bottom
        let btPath = UIBezierPath()

        btPath.moveToPoint(startingPoint)
        btPath.addLineToPoint(endingPoint)
        btPath.lineWidth = 2.0

        outerBorder.setStroke()
        btPath.stroke()
    }