ios - 用 swift 在段落周围画框

标签 ios swift

<分区>


想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post .

关闭 7 年前

我想创建一个像这个框一样的 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>

有什么建议吗?

I want to make a view like this image

最佳答案

我建议在用户界面中创建 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 - 即使更新了表格 View 单元格,如何在表格 View 中保留表格 View 单元格的编辑模式?

下一篇:ios - 服务器收不到iOS发送的数据

相关文章:

ios - 在 iOS 中的 Instagram 上分享多张图片

ios - 在 AppDelegate 中创建全局委托(delegate)类

swift - 中心 CAShapeLayer

iphone - 如何将 mp4 文件从文档复制到照片库(在 iPhone 中)?

ios - MFMailComposeViewController 不关闭

ios - 自动布局的 ScrollView

ios - UISwipeGestureRecognizer 不适用于呈现的 VC 和 View

ios - 从 (NSDate) -> NSTimeInterval 转换为不相关的类型 'NSTimeInterval 总是失败

core-data - Core Data 使用 Swift 保存数据

swift - 使用 RxSwift 的通用转换方法的问题