I need to implement outline textfield with placeholder in my swift project one like the Material UI. I tried googling for the same but could not found anything. Anyone know how to achieve it in swift? Reference Images:
Asked
Active
Viewed 1.3k times
3
-
Somthing like this? https://github.com/raginmari/RAGTextField – Andreas Oetjen Feb 20 '20 at 12:02
-
@AndreasOetjen yes but not exactly, is has type outline which is out of textfield border. – Niraj Feb 20 '20 at 12:04
-
you can check this:https://github.com/material-components/material-components-ios and use `pod 'MaterialComponents/TextFields'` and `pod 'MaterialComponents/TextFields+ColorThemer'` – Kishan Bhatiya Feb 20 '20 at 12:14
-
1Something like this? https://github.com/material-components/material-components-ios/tree/stable/components/TextFields/styling – Tanjima Kothiya Feb 20 '20 at 12:14
-
That can only be achieved by programmatically right? I mean to not from the `Storyboard`. – Niraj Feb 20 '20 at 12:17
-
It is android style, don't do it on iOS :) Prefer native solution :) – Michał Ziobro Aug 11 '22 at 14:36
3 Answers
5
Using this pod, you can get the same design
- Take
UIView
on Storyboard and set constraints - Make class which is a subclass of
UIView
and import podsMaterialComponents.MaterialTextFields
andMaterialComponents.MaterialTextFields_ColorThemer
class CustomOutlinedTxtField: UIView {
private var textFieldControllerFloating: MDCTextInputControllerOutlined!
var textField: MDCTextField!
@IBInspectable var placeHolder: String!
@IBInspectable var value: String!
@IBInspectable var primaryColor: UIColor! = .purple
override open func draw(_ rect: CGRect) {
super.draw(rect)
textField.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
}
open override func awakeFromNib() {
super.awakeFromNib()
setUpProperty()
}
func setUpProperty() {
//Change this properties to change the propperties of text
textField = MDCTextField(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
textField.placeholder = placeHolder
textField.text = value
//Change this properties to change the colors of border around text
textFieldControllerFloating = MDCTextInputControllerOutlined(textInput: textField)
textFieldControllerFloating.activeColor = primaryColor
textFieldControllerFloating.floatingPlaceholderActiveColor = primaryColor
textFieldControllerFloating.normalColor = UIColor.lightGray
textFieldControllerFloating.inlinePlaceholderColor = UIColor.lightGray
//Change this font to make borderRect bigger
textFieldControllerFloating.inlinePlaceholderFont = UIFont.systemFont(ofSize: 14)
textFieldControllerFloating.textInsets(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
self.addSubview(textField)
}
}
- Assign that custom class to
UIView
Result

Kishan Bhatiya
- 2,175
- 8
- 14
-
-
-
1
-
Hey, how can I change the height of textfield? I tried but not able to do that. – Niraj Feb 25 '20 at 04:36
-
You are assigning a custom class to view, from storyboard you can change the height – Kishan Bhatiya Feb 25 '20 at 04:41
-
-
For reference see the mobile no field: https://prnt.sc/r77m5f and view hierarchy: https://prnt.sc/r77mhw – Niraj Feb 25 '20 at 05:23
-
Please check comment in the answer above line `textFieldControllerFloating.inlinePlaceholderFont` and see last comments https://github.com/material-components/material-components-ios/issues/6253 – Kishan Bhatiya Feb 25 '20 at 05:32
-
Yes I have checked that, is there any other way I can set height of the textfield and keeping the same placeholder font? – Niraj Feb 25 '20 at 06:01
-
-
Do you know why the constraints of the other components do not work when we add this pod's custom textfield? The components look ok with the constraints in the storyboard but when the app is running on the device, the text fields overlap the other components. – Binaya Thapa Magar Nov 20 '21 at 11:14
-
Sorry, but without seeing code I'm not able to say anything about this – Kishan Bhatiya Nov 22 '21 at 05:34
1
You can use Material Components:-
It will be looking like this below.
https://material.io/components/text-fields/
Github repo - https://github.com/material-components/material-components-ios

Vipul Kumar
- 893
- 9
- 19
-
@niraj You can follow this github repo and you can find this exact solution – Vipul Kumar Feb 20 '20 at 13:12
1
you can also try this framework SkyFloatingLabelTextField
, TextFieldEffects
or TweeTextField
https://github.com/Skyscanner/SkyFloatingLabelTextField

Shivam Parmar
- 1,520
- 11
- 27