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

标签 ios swift

这就是我想要实现的。

enter image description here

我尝试自己编写代码,但未显示第一个外部 View 边框。

这是我的代码:

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()

    }
}

有顶部和底部的外边框。但是只有最下面的那个出现了。我不知道我哪里出错了。

最佳答案

我对您的代码做了一些修改。试试它是否适合你。

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()
    }

关于ios - Swift - 如何制作包含标签的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38560218/

相关文章:

c# - 我能从这样的崩溃信息中得到有用的信息吗?

objective-c - 调试 SIGSEGV 崩溃

swift - 从 dateFormat 获取错误的日期

ios - 带有异步 UIImageView 的自动布局 UITableViewCell 不工作

iphone - 如何使用 MKStoreKit 恢复购买

iphone - 安装 Xcode 3.2.4,获取 "Base SDK Missing"

ios - 导出 Ad Hoc 配置文件?

ios - 遍历电话号码,创建自定义 View 并 swift 将电话号码传递给它

ios - 检查 AVFoundation 的资源何时可用

ios - 在 Swift 中的 5 个标签上显示递减的分钟和秒