简体   繁体   English

如何设置UItextview边框的阴影?

[英]how to set shadow of UItextview border?

Hi i want to set shadow of UItextView like below in image. 嗨,我想像下面的图像一样设置UItextView的阴影。

在此处输入图片说明

I have tried below code but it does not give me same result rather it also make the text of UITextView as shadow. 我试过下面的代码,但它不会给我相同的结果,而是还会使UITextView的文本成为阴影。

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 1)边框

Your desired output does not have a border. 您所需的输出没有边框。 So do not set one. 所以不要设置一个。

2) View clips shadow 2)查看片段阴影

By default a UIView clips its content to its bounds . 默认情况下, UIView其内容UIView到其bounds As a result, you can not see anything drawn outside the bounds (your shadow). 结果,您看不到任何超出边界的内容(您的阴影)。 Set clipsToBounds to false . clipsToBounds设置为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 ? 您的UITextView背景色是透明色吗? If yes, then set the background color of UITextView or UITextView layer background color. 如果是,则设置UITextViewUITextView图层背景色的背景色。 Because setting UITextView background color nik will set it's layer's background color to nil .So 因为设置UITextView背景色nik会将其图层的背景色设置为nil,所以

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. 但是,当masksToBounds = false ,任何超出该层边界的子层都是可见的。 So UITextField scroll text outside the layer. 因此, UITextField文本滚动到图层之外。

If this is a problem for you, just add another UIView under your UITextView and set it's layer to display shadow. 如果这对您来说是个问题,只需在UITextView下添加另一个UIView并将其图层设置为显示阴影即可。

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

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