有没有办法在 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)
这导致:
最佳答案
原因: Clips to bounds = true
和 shadow
在同一个 View 上不能同时工作。因此,要解决这个问题,您必须遵循。
- 添加您的
TextView
在 UIView 中(说viewTextBG
)。 -
TextView
约束:顶部底部、前导、尾随 = 0 wrt。viewTextBG
. 将圆角半径设为
textview
和viewTextBG
textview.cornerRadius = 10 viewTextBG.cornerRadius = 10
将 clipsToBounds 赋给
textview
和viewTextBG
textview.clipsToBounds = True viewTextBG.clipsToBounds = false
现在给
viewTextBG
添加阴影.
现在一切正常,仅此而已。
关于ios - 在 UITextView 的边框周围放置阴影以在 iOS/swift 中赋予它 3d 效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46860793/