2016-11-18 4 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

UITextViewをUIViewでラップして、代わりにUIViewの影を設定することができます –

答えて

1

あなたのコードでは、2つの問題があります。

1)ボーダー

あなたの所望の出力は持っていません境界。だから1つを設定しないでください。

2)閲覧クリップはそのboundsUIViewクリップその内容デフォルトで

をシャドウ。結果として、範囲外に描画されたもの(あなたの影)を見ることはできません。 clipsToBoundsfalseに設定します。

の作業例:

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

テスト済みです。 「あなたのために」働かないものは何ですか? – shallowThought

+0

私は、制約が追加されたテキストビューを持っています。 – Techiee

1

はあなたUITextView背景色がクリアカラーありますか?はいの場合は、背景色をUITextViewまたはUITextViewの背景色に設定します。 設定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 
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はレイヤーの外にテキストをスクロールします。

これが問題の場合は、別のUIViewUITextViewの下に追加し、そのレイヤーを影を表示するように設定します。

関連する問題