简体   繁体   中英

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? The textView has rounded corners.

I don't want to drop a shadow for the content of the textView (ie text). Just around the textView border only.

I need to clipToBounds on the textView for the rounded corners.

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. So to sort out this you have to do follow.

  1. Add your TextView in a UIView (says viewTextBG ).
  2. TextView Constraints : top bottom, leading, trailing = 0 wrt. viewTextBG .
  3. Give corner radius to textview and viewTextBG

    textview.cornerRadius = 10 viewTextBG.cornerRadius = 10
  4. Give clipsToBounds to textview and viewTextBG

     textview.clipsToBounds = True viewTextBG.clipsToBounds = false
  5. Now give shadow to viewTextBG .

Now everything works, thats all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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