2017-10-04 74 views
0

我想實現一個陰影UITextField圓角像下面的圖像:enter image description here如何在圓角的UITextField上添加陰影?

我的代碼如下:

override func viewDidLoad() { 
     super.viewDidLoad() 
     textField.layer.cornerRadius =    textField.frame.size.height/2 
     textField.layer.borderWidth = 1.0 
     textField.layer.borderColor = UIColor(white: 0.5, alpha: 0.3).cgColor 
     textField.layer.shadowOpacity = 1 
     textField.layer.shadowRadius = 4.0 
     textField.layer.shadowColor = UIColor.black.cgColor 
    } 

,但是,我覺得缺了點什麼......

輸出: enter image description here

在此先感謝!

+0

這增加了** **邊界,而不是一個影子。 – the4kman

+0

yaa ..但如何添加陰影?? @ the4kman –

+0

[UITextField文字上的陰影]可能的重複(https://stackoverflow.com/questions/1274168/drop-shadow-on-uitextfield-text) –

回答

1

嘗試下面的代碼實現對roundRect文本框的陰影效果。

//Basic texfield Setup 
    textField.borderStyle = .none 
    textField.backgroundColor = UIColor.groupTableViewBackground // Use anycolor that give you a 2d look. 

    //To apply corner radius 
    textField.layer.cornerRadius = textField.frame.size.height/2 

    //To apply border 
    textField.layer.borderWidth = 0.25 
    textField.layer.borderColor = UIColor.white.cgColor 

    //To apply Shadow 
    textField.layer.shadowOpacity = 1 
    textField.layer.shadowRadius = 3.0 
    textField.layer.shadowOffset = CGSize.zero // Use any CGSize 
    textField.layer.shadowColor = UIColor.gray.cgColor 

    //To apply padding 
    let paddingView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: textField.frame.height)) 
    textField.leftView = paddingView 
    textField.leftViewMode = UITextFieldViewMode.always 

注:出於某種原因textField.borderStyle = .none未生效,即使設置代碼viewWillLayoutSubviews()viewDidLayoutSubviews()。所以,我建議你通過故事板文本框Attributes inspector設置的borderStyle。從實際設備

enter image description here

輸出:

enter image description here

爲了實現陰影效果:(像其他SO帖)

textField.layer.borderColor = UIColor.black.withAlphaComponent(0.25).cgColor 
    textField.layer.shadowOffset = CGSize(width: 0, height: 3) 
    textField.layer.shadowColor = UIColor.black.cgColor //Any dark color 

輸出:

enter image description here

+0

很棒!謝謝你! –

0

試試這個:

textField.layer.masksToBounds = false 
textField.layer.shadowRadius = 4.0 
textField.layer.shadowColor = UIColor.black.cgColor 
textField.layer.shadowOffset = CGSize(width: 1.0, height: 1.0) 
textField.layer.shadowOpacity = 1.0 
0

您可以添加此擴展名,然後使用方法「addShadow」到陰影添加到您文本字段,標籤,TextView的等..

extension UIView {  
func addShadow(shadowColor: CGColor = UIColor.black.cgColor, 
        shadowOffset: CGSize = CGSize(width: 1.0, height: 2.0), 
        shadowOpacity: Float = 0.4, 
        shadowRadius: CGFloat = 3.0) { 
     layer.shadowColor = shadowColor 
     layer.shadowOffset = shadowOffset 
     layer.shadowOpacity = shadowOpacity 
     layer.shadowRadius = shadowRadius 
     layer.masksToBounds = false 
    } 
} 
+0

不工作,因爲maskToBound屬性爲true。 –

0

使用這下面的代碼

textfield.layer.cornerRadius = textfield.frame.size.height/2 
    textfield.clipsToBounds = false 
    textfield.layer.shadowOpacity=0.4 
    textfield.layer.shadowOffset = CGSize(width: 0, height: 0) 

輸出:

enter image description here

+0

其顯示textfield的外行...檢查我的輸出。 –

+0

@UdayBabariya刪除邊框寬度和顏色,並使用textfield白色,並在textfield後面查看白色,以便您可以清晰地查看。 –