用swift在段落周围画框

时间:2016-05-07 16:50:11

标签: ios swift

我想像这个盒子一样创建一个视图或文本视图。 我已经尝试使用div作为nsattributedtext创建html但我无法设置段落的边框

<div style='background-color:#4fa5d5; color:white; font-weight: bold'>title</div><div style='border: 5px solid black;'><p style='margin-left: 5px'>content</p></div>

任何建议?

I want to make a view like this image

1 个答案:

答案 0 :(得分:1)

我建议在用户界面中创建2个UILabel,一个在另一个上面。将他们限制在他们的位置。然后添加此扩展名:

extension UILabel {
    func roundCorners(corners: UIRectCorner, radius: CGFloat) {
       var bounds: CGRect = self.bounds
       var maskPath: UIBezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSizeMake(radius, radius))
       var maskLayer: CAShapeLayer = CAShapeLayer()
       maskLayer.frame = bounds
       maskLayer.path = maskPath.CGPath
       self.layer.mask = maskLayer
       var frameLayer: CAShapeLayer = CAShapeLayer()
       frameLayer.frame = bounds
       frameLayer.path = maskPath.CGPath
       frameLayer.strokeColor = UIColor.redColor().CGColor
       frameLayer.fillColor = nil
       self.layer.addSublayer(frameLayer)
   }
}

然后围绕顶部标签的顶角

topLabel.roundCorners(UIRectCorner.TopLeft, radius: 10)
topLabel.roundCorners(UIRectCorner.TopRight, radius: 10)

然后围绕底部标签的底角

bottomLabel.roundCorners(UIRectCorner.BottomLeft, radius: 10)
bottomLabel.roundCorners(UIRectCorner.BottomRight, radius: 10)

最后,添加边框:

for label in [topLabel, bottomLabel] {
    label.layer.borderColor = UIColor.blueColor.CGColor
    label.layer.borderWidth = 1
}