2016-11-18 205 views
2

嗨,我想在圖像中設置下面的UItextView的陰影。如何設置UItextview邊框的陰影?

enter image description here

我曾嘗試下面的代碼,但它並沒有給我同樣的結果,而這也使得他的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 

上面的代碼導致我這個觀點不要求

enter image description here

+2

你可以嘗試用UIView包裝UITextView並設置UIView的陰影 –

回答

1

你的代碼有兩個問題:

1)邊境

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

2)查看剪輯陰影

缺省情況下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 

結果:

+0

對我不起作用 – Techiee

+0

在Playground中測試。什麼「不適合」你? – shallowThought

+0

我有文本視圖,約束添加。 – Techiee

1

是您UITextView背景顏色爲清晰的彩色?如果是,則將背景色設置爲UITextViewUITextView圖層背景色。由於設定UITextView的背景顏色NIK將設置它的圖層的背景顏色,以零。所以

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 
1

下面的代碼工作正常

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並將其設置爲顯示陰影。