简体   繁体   中英

how to set shadow of UItextview border?

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

在此处输入图片说明

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

在此处输入图片说明

Your code has two issues:

1) Border

Your desired output does not have a border. So do not set one.

2) View clips shadow

By default a UIView clips its content to its bounds . As a result, you can not see anything drawn outside the bounds (your shadow). Set clipsToBounds to false .

Working example:

// Test view setup
let parent = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 200.0, height: 200.0))
parent.backgroundColor = UIColor.white
let tv_comments = UITextView(frame: CGRect(x: 50.0, y: 50.0, width: 100.0, height: 100.0))
tv_comments.text = "Test Test Test Test Test Test "
tv_comments.backgroundColor = UIColor.white
parent.addSubview(tv_comments)

// replace your code with the code below
tv_comments.clipsToBounds = false

tv_comments.layer.shadowRadius = 5.0
tv_comments.layer.shadowColor = UIColor.gray.cgColor
tv_comments.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
tv_comments.layer.shadowOpacity = 0.8
tv_comments.textColor = UIColor.black

Result:

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

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.

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