Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set shadow of UItextview border?

Hi i want to set shadow of UItextView like below in image.

enter image description here

I have tried below code but it does not give me same result rather it also make the text of UITextView as shadow.

self.tv_comments.layer.shadowRadius = 5.0
self.tv_comments.layer.borderColor = UIColor.gray.cgColor
self.tv_comments.layer.borderWidth = 1
self.tv_comments.layer.shadowColor = UIColor.gray.cgColor
self.tv_comments.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
self.tv_comments.layer.shadowOpacity = 1.0
self.tv_comments.textColor = UIColor.black

Above code results me this view which is not required

enter image description here

like image 955
TechChain Avatar asked Jan 16 '26 23:01

TechChain


2 Answers

The below code works fine

self.tv_comments.layer.shadowColor = UIColor.black.cgColor;
self.tv_comments.layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
self.tv_comments.layer.shadowOpacity = 1.0
self.tv_comments.layer.shadowRadius = 5.0
self.tv_comments.layer.masksToBounds = false

However, when masksToBounds = false, any sublayers that extend outside the layer's boundaries will be visible. So UITextField scroll text outside the layer.

If this is a problem for you, just add another UIView under your UITextView and set it's layer to display shadow.

like image 200
Shamsudheen TK Avatar answered Jan 19 '26 17:01

Shamsudheen TK


Is your UITextView background color is clear color ? If yes, then set the background color of UITextView or UITextView layer background color. Because setting UITextView background color nik will set it's layer's background color to nil.So

self.tv_comments.backgroundColor = UIColor.white
//or self.tv_comments.backgroundColor = UIColor.clear
//self.tv_comments.layer.backgroundColor = UIColor.white

self.tv_comments.layer.shadowRadius = 5.0
self.tv_comments.layer.borderColor = UIColor.gray.cgColor
self.tv_comments.layer.borderWidth = 1
self.tv_comments.layer.shadowColor = UIColor.gray.cgColor
self.tv_comments.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
self.tv_comments.layer.shadowOpacity = 1.0
self.tv_comments.textColor = UIColor.black
like image 26
Rohit Khandelwal Avatar answered Jan 19 '26 18:01

Rohit Khandelwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!