簡體   English   中英

如何設置UItextview邊框的陰影?

[英]how to set shadow of UItextview border?

嗨,我想像下面的圖像一樣設置UItextView的陰影。

在此處輸入圖片說明

我試過下面的代碼,但它不會給我相同的結果,而是還會使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

上面的代碼使我不需要此視圖

在此處輸入圖片說明

您的代碼有兩個問題:

1)邊框

您所需的輸出沒有邊框。 所以不要設置一個。

2)查看片段陰影

默認情況下, UIView其內容UIView到其bounds 結果,您看不到任何超出邊界的內容(您的陰影)。 clipsToBounds設置為false

工作示例:

// 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

結果:

您的UITextView背景色是透明色嗎? 如果是,則設置UITextViewUITextView圖層背景色的背景色。 因為設置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

下面的代碼工作正常

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

但是,當masksToBounds = false ,任何超出該層邊界的子層都是可見的。 因此, UITextField文本滾動到圖層之外。

如果這對您來說是個問題,只需在UITextView下添加另一個UIView並將其圖層設置為顯示陰影即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM