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.
TextView
in a UIView (says viewTextBG
).TextView
Constraints : top bottom, leading, trailing = 0 wrt. viewTextBG
. Give corner radius to textview
and viewTextBG
textview.cornerRadius = 10 viewTextBG.cornerRadius = 10
Give clipsToBounds to textview
and viewTextBG
textview.clipsToBounds = True viewTextBG.clipsToBounds = false
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.