ios - UILabel 下边框和右边框

标签 ios swift uilabel border

我尝试为 uilabel 添加边框,但我只想有顶部、右侧和底部边框。

像这样:

                      |
        I am a label  |
                      |
       ----------------

我尝试使用这些代码,但它默认添加所有 4 个边

myLabel.layer.borderWidth = 1;
myLabel.layer.borderColor = UIColorCode.init(hexString: "#666666")

最佳答案

创建 UILabel 的子类并添加以下代码。这将根据您的需要绘制边框。

 override func drawRect(rect: CGRect) {

        let outerBorder = UIColor.blackColor()
        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 bottomLeft   = CGPoint(x: insetRect.minX, y: insetRect.maxY)
        let bottomRight     = CGPoint(x: insetRect.maxX, y: insetRect.maxY)


        let path = UIBezierPath()
        path.moveToPoint(startingTopPoint)
        path.addLineToPoint(endingTopPoint)
        path.lineWidth = 2.0
        path.addLineToPoint(bottomRight)
        path.addLineToPoint(bottomLeft)


        outerBorder.setStroke()
        path.stroke()
}

关于ios - UILabel 下边框和右边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38562962/

相关文章:

iOS,带有调整类和 IBInspectables 的界面生成器错误?

ios - Segue 在 map 注释按钮上不起作用 - 看不出有什么问题

ios - 如何处理空 PFFile 对象(解析后端,Swift)

ios - swift 3/4 : SearchBar not filtering results properly in TableView

ios - 检测 UILabel 文本的变化

ios - 不同宽度的 UITableViewCell 自动布局

ios - 使用多行 UILabel 自动调整自定义 View 的大小

ios - map 离线缓存

iOS - 使用 Paypal iOS SDK 的支付问题

ios - 添加完成/旁边到数字键盘