简体   繁体   English

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

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

Is there a way to drop a shadow around the border of a UITextView to give it a 3D effect?有没有办法在 UITextView 的边框周围放置阴影以使其具有 3D 效果? The textView has rounded corners. textView 有圆角。

I don't want to drop a shadow for the content of the textView (ie text).我不想为 textView 的内容(即文本)添加阴影。 Just around the textView border only.仅在 textView 边框周围。

I need to clipToBounds on the textView for the rounded corners.我需要在圆角的 textView 上使用 clipToBounds 。

So far this is what I have tried:到目前为止,这是我尝试过的:

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)

This results in:这导致:

在此处输入图片说明

Reason : Clips to bounds = true and shadow on the same view doest not work simultaneous.原因: Clips to bounds = true和同一视图上的shadow不能同时工作。 So to sort out this you have to do follow.所以要解决这个问题,你必须遵循。

  1. Add your TextView in a UIView (says viewTextBG ).在 UIView 中添加您的TextView (说viewTextBG )。
  2. TextView Constraints : top bottom, leading, trailing = 0 wrt. TextView约束:顶部底部,前导,尾随 = 0 wrt。 viewTextBG . viewTextBG
  3. Give corner radius to textview and viewTextBGtextviewviewTextBG赋予角半径

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

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

Now everything works, thats all.现在一切正常,仅此而已。

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

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