嘗試下面的代碼實現對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。從實際設備
輸出:
爲了實現陰影效果:(像其他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
輸出:
來源
2017-10-04 14:07:06
Joe
這增加了** **邊界,而不是一個影子。 – the4kman
yaa ..但如何添加陰影?? @ the4kman –
[UITextField文字上的陰影]可能的重複(https://stackoverflow.com/questions/1274168/drop-shadow-on-uitextfield-text) –