UILabel-左,上,右圆角上的自定义边框

时间:2019-02-05 09:47:48

标签: swift border calayer layer radius

我正在考虑在UILabels的左上方和右上方添加边框。 哪种工作方案是最好的?

rounded tabs uilabels

我尝试过:

func draw(_ rect: CGRect) {

        let cgContext = UIGraphicsGetCurrentContext()
        cgContext?.move(to: CGPoint(x: rect.minX, y: rect.minY))
        cgContext?.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))
        cgContext?.setStrokeColor(UIColor.red.cgColor)
        cgContext?.setLineWidth(2.0)
        cgContext?.strokePath()
    }

    func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
        let border = CALayer()

        switch edge {
        case .top:
        border.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: thickness)
        case .left:
        border.frame = CGRect(x:0, y: self.frame.height - thickness, width: self.frame.width, height: thickness)
        case .bottom:
        border.frame = CGRect(x: 0, y: 0, width: thickness, height: self.frame.height)
        case .right:
        border.frame = CGRect(x: self.frame.width - thickness, y: 0, width: thickness, height: self.frame.height)
        default: break
        }

        border.backgroundColor = color.cgColor

        self.addSublayer(border)
    }

    func addBorder2(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor

        //top
        border.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: 2)

        //left
        border.frame = CGRect(x: 0, y: 0, width: thickness, height: 2)

        //right
        border.frame = CGRect(x: self.frame.width - thickness, y: 0, width: thickness, height: 2)

        self.addSublayer(border)
        /* SWITCH CASE NOT WORKING !!!
        switch edge {
            case .top:
                border.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: 2)
            case .bottom:
                border.frame = CGRect(x: 0, y: self.frame.height - thickness, width: self.frame.width, height:2)
            case .left:
                border.frame = CGRect(x: 0, y: 0, width: thickness, height: 2)
            case .right:
                border.frame = CGRect(x: self.frame.width - thickness, y: 0, width: thickness, height: 2)
            default: break
            }
        */
    }

最终结果应如下所示: 左上方和右上方需要边框。底部仍然没有边框。

final result expected

1 个答案:

答案 0 :(得分:0)

您可以使用difficulty = -1 class Application(Frame): def __init__(self, master): super().__init__(master) self.difficulty = -1 self.grid() self.login = self.create_main() self.read = None def changeVariable1(self): self.difficulty = 12 def changeVariable2(self): self.difficulty = 16 def changeVariable3(self): self.difficulty = 20 def diff(self): global radius if difficulty == 12: radius = (30) elif difficulty == 16: radius = (20) elif difficulty == 20: radius = (10) def create_read(self): read = Toplevel() Button(read, text="Easy", font='Helvetica 10 bold', command=self.changeVariable1()).grid(row=3, column=2) Button(read, text="Medium", font='Helvetica 10 bold', command=self.changeVariable2()).grid(row=3, column=3) Button(read, text="Hard", font='Helvetica 10 bold', command=self.changeVariable3()).grid(row=3, column=4) return read root = Tk() app = Application(root) root.mainloop() UIView的圆角设置为圆度。

UIBezierPath

功能

setRoundness(ToCorners: [.topLeft, .bottomLeft], for: yourView, withRadius: customRadius)