myLabel.layer.borderWidth = 0.5
myLabel.layer.borderColor = UIColor.green.cgColor
这是绘制边框的简单答案。但问题是我需要只在右边和底部绘制边框。我还需要另一个标签来在顶部和右侧绘制边框。如何画边到特定边,而不是所有4边?
答案 0 :(得分:1)
添加形状图层并在该图层中绘制线条。请注意,图层不参与自动布局,因此您需要将代码放在viewDidLayoutSubviews或子类uilabel中,并在布局子视图中执行此操作。这是一个使用UILabel子类的游乐场示例:
import PlaygroundSupport
import UIKit
class L: UILabel {
var strokeColor = UIColor.blue
var strokeWidth = CGFloat(0.5)
private lazy var labelBorderLayer:CAShapeLayer = {
let shapeLayer = CAShapeLayer()
self.layer.addSublayer(shapeLayer)
return shapeLayer
}()
override func layoutSubviews() {
super.layoutSubviews()
let path = CGMutablePath()
path.move(to: CGPoint(x: 0, y: bounds.size.height))
path.addLine(to: CGPoint(x: bounds.size.width, y: bounds.size.height))
path.addLine(to: CGPoint(x: bounds.size.width, y: 0))
labelBorderLayer.path = path
labelBorderLayer.strokeColor = strokeColor.cgColor
labelBorderLayer.lineWidth = strokeWidth
labelBorderLayer.fillColor = UIColor.clear.cgColor
}
}
let v = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
let l = L(frame: v.frame.insetBy(dx: 50, dy: 80))
v.addSubview(l)
l.textColor = .white
l.textAlignment = .center
l.text = "gjgkjgjgjgj"
v.backgroundColor = .red
PlaygroundPage.current.liveView = v
答案 1 :(得分:0)
我不确定你是否可以用实际的标签边框来做到这一点。但是你可以做类似的事情
sum(if year([Date]) == 2017 and MONTH([Date]) == 1 then [profit] END)
对于UIButton来说,但你可以轻松地适应它。
来源:https://gist.github.com/Isuru-Nanayakkara/496d5713e61125bddcf5
希望这有帮助