首页 文章

在UITextView的边框周围投下阴影,在iOS / swift中为它提供3d效果

提问于
浏览
1

有没有办法在UITextView的边框周围放一个阴影给它一个3D效果? textView具有圆角 .

我不想为textView(即文本)的内容删除阴影 . 仅在textView边框周围 .

我需要在textView上使用clipToBounds作为圆角 .

到目前为止,这是我尝试过的:

let shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: textView.bounds, cornerRadius: 15).cgPath
shadowLayer.fillColor = UIColor.clear.cgColor
shadowLayer.shadowColor = UIColor.black.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = CGSize(width: 2, height: 2)
shadowLayer.shadowOpacity = 1
shadowLayer.shadowRadius = 2
textView.layer.addSublayer(shadowLayer)

这导致:

enter image description here

1 回答

  • 1

    Reason : Clips to bounds = trueshadow 在同一视图上,doest不能同时工作 . 所以要理清这一点,你必须遵循 .

    • 在UIView中添加 TextView (说 viewTextBG ) .

    • TextView 约束:顶部底部,前导,尾部= 0 wrt . viewTextBG .

    • 将圆角半径设为 textviewviewTextBG

    textview.cornerRadius = 10
    viewTextBG.cornerRadius = 10
    
    • 将clipsToBounds提供给 textviewviewTextBG
    textview.clipsToBounds = True
    viewTextBG.clipsToBounds = false
    
    • 现在给 viewTextBG 阴影 .

    现在一切正常,就是这样 .

相关问题