<分区>
我想创建一个像这个框一样的 View 或 TextView 。 我已经尝试使用 div 创建 html 作为 nsattributedtext 但我无法为段落设置边框
<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>
有什么建议吗?
最佳答案
我建议在用户界面中创建 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
}
关于ios - 用 swift 在段落周围画框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37091207/
相关文章:
ios - 在 iOS 中的 Instagram 上分享多张图片
ios - 在 AppDelegate 中创建全局委托(delegate)类
iphone - 如何将 mp4 文件从文档复制到照片库(在 iPhone 中)?
ios - MFMailComposeViewController 不关闭
ios - UISwipeGestureRecognizer 不适用于呈现的 VC 和 View
ios - 从 (NSDate) -> NSTimeInterval 转换为不相关的类型 'NSTimeInterval 总是失败