繁体   English   中英

在 UITextView 的边框周围放置阴影以在 iOS/swift 中赋予它 3d 效果

[英]drop shadow around border of UITextView to give it 3d effect in iOS/swift

有没有办法在 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不能同时工作。 所以要解决这个问题,你必须遵循。

  1. 在 UIView 中添加您的TextView (说viewTextBG )。
  2. TextView约束:顶部底部,前导,尾随 = 0 wrt。 viewTextBG
  3. textviewviewTextBG赋予角半径

    textview.cornerRadius = 10 viewTextBG.cornerRadius = 10
  4. 将 clipsToBounds 赋予textviewviewTextBG

     textview.clipsToBounds = True viewTextBG.clipsToBounds = false
  5. 现在给viewTextBG提供阴影。

现在一切正常,仅此而已。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM