swift - 边框不覆盖背景

标签 swift uibutton uilabel border

我有一个 UILabel 正在使用与背景颜色相同的边框,它是半遮蔽的,以创建一个很好的视觉效果。然而,问题是在边框的 OUTSIDE 上仍然有一小部分但很明显的标签背景颜色。

边框没有覆盖整个标签!

遗憾的是,更改边框宽度也不会改变任何东西。

这是正在发生的事情的图片,放大以便您可以看到它:

enter image description here

我的代码如下:

        iconLbl.frame = CGRectMake(theWidth/2-20, bottomView.frame.minY-20, 40, 40)
        iconLbl.font = UIFont.fontAwesomeOfSize(23)
        iconLbl.text = String.fontAwesomeIconWithName(.Info)
        iconLbl.layer.masksToBounds = true
        iconLbl.layer.cornerRadius = iconLbl.frame.size.width/2
        iconLbl.layer.borderWidth = 5
        iconLbl.layer.borderColor = topBackgroundColor.CGColor
        iconLbl.backgroundColor = UIColor.cyanColor()
        iconLbl.textColor = UIColor.whiteColor()

有什么我想念的吗?
还是我必须想出另一个来达到这种效果?

谢谢!

编辑:
到目前为止我尝试过的事情 list !
  • 更改 layer.borderWidth
  • 大惊小怪的 clipsToBounds/MasksToBounds
  • 在 layer.frame 周围播放
  • 玩弄整体框架

  • 编辑2:

    没有找到修复!我通过将此方法扩展到我的 UIViewController 使用了一种解决方法
    func makeFakeBorder(inputView:UIView,width:CGFloat,color:UIColor) -> UIView {
            let fakeBorder = UIView()
            fakeBorder.frame = CGRectMake(inputView.frame.origin.x-width, inputView.frame.origin.y-width, inputView.frame.size.width+width*2, inputView.frame.size.height+width*2)
            fakeBorder.backgroundColor = color
            fakeBorder.clipsToBounds = true
            fakeBorder.layer.cornerRadius = fakeBorder.frame.size.width/2
            fakeBorder.addSubview(inputView)
            inputView.center = CGPointMake(fakeBorder.frame.size.width/2, fakeBorder.frame.size.height/2)
            return fakeBorder
        }
    

    最佳答案

    我相信这是在 iOS 中将边框绘制到图层的方式。在文件中它说:

    When this value is greater than 0.0, the layer draws a border using the current borderColor value. The border is drawn inset from the receiver’s bounds by the value specified in this property. It is composited above the receiver’s contents and sublayers and includes the effects of the cornerRadius property.


    解决此问题的一种方法是将蒙版应用于 View 的图层,但我发现即使如此,在进行快照测试时,我们仍然可以在 View 周围看到一条细小的线条。所以为了修复它,我把这段代码放到 layoutSubviews
    class MyView: UIView {
    
        override func layoutSubviews() {
            super.layoutSubviews()
        
            let maskInset: CGFloat = 1
            // Extends the layer's frame.
            layer.frame = layer.frame.inset(dx: -maskInset, dy: -maskInset)
            // Increase the border width
            layer.borderWidth = layer.borderWidth + maskInset
        
            layer.cornerRadius = bounds.height / 2
            layer.maskToBounds = true
        
            // Create a circle shape layer with true bounds.
            let mask = CAShapeLayer()
            mask.path = UIBezierPath(ovalIn: bounds.inset(dx: maskInset, dy: maskInset)).cgPath
        
            layer.mask = mask
         }
     }
    
    CALayer's mask

    关于swift - 边框不覆盖背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33350689/

    相关文章:

    ios - 无效的 Swift 支持

    ios - 线程 1 : Signal sigabrt. 不知道如何解决这个问题

    快速转换为具有约束的通用类型

    ios - 从 UIView 蒙版中剪切 UIImage

    iphone - 两个UITableViewCellStyleDefault单元格之间的一个像素-无法在其上绘制

    iphone - 打乱 NSMutableArray 而不重复并显示在 UIButton 中

    objective-c - 触摸时按钮不突出显示

    iphone - 如何在uilabel中设置有限的字符数

    iphone - 如何仅将 UILabel 的顶部两个角四舍五入?

    ios - 如何在 iOS 编程中保持方法执行的顺序?