SkyFloatingLabelTextField
SkyFloatingLabelTextField copied to clipboard
Add left padding for the Texts
I can't find a way to add a padding to the title and the text while I keep the underline stuck to the left.
Something like that
Hey @Shawnees this isn't currently possible
This would be a great, any chance of it getting implemented? I could try making a PR but have no knowledge of the code for this library.
I have same problem, this is my current solution. Hope it helps.
class CustomTextField: SkyFloatingLabelTextField {
private let leftPadding = CGFloat(6)
override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
let rect = CGRect(
x: leftPadding,
y: titleHeight(),
width: bounds.size.width,
height: bounds.size.height - titleHeight() - selectedLineHeight
)
return rect
}
override func titleLabelRectForBounds(_ bounds: CGRect, editing: Bool) -> CGRect {
if editing {
return CGRect(x: leftPadding, y: 0, width: bounds.size.width, height: titleHeight())
}
return CGRect(x: leftPadding, y: titleHeight(), width: bounds.size.width, height: titleHeight())
}
}
@alex02rt thanks for sharing this - I have adjusted it slightly for my use case by adding right padding and moving down the title label. The result and source code is attached below in case anyone is interested:
class TomsFloatingLabelTextField: SkyFloatingLabelTextField {
private let leftPadding = CGFloat(6)
private let rightPadding = CGFloat(6)
override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
let rect = CGRect(
x: leftPadding,
y: titleHeight(),
width: bounds.size.width - rightPadding,
height: bounds.size.height - titleHeight() - selectedLineHeight
)
return rect
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
let rect = CGRect(
x: leftPadding,
y: titleHeight(),
width: bounds.size.width - rightPadding,
height: bounds.size.height - titleHeight() - selectedLineHeight
)
return rect
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
let rect = CGRect(
x: leftPadding,
y: titleHeight(),
width: bounds.size.width - rightPadding,
height: bounds.size.height - titleHeight() - selectedLineHeight
)
return rect
}
override func titleLabelRectForBounds(_ bounds: CGRect, editing: Bool) -> CGRect {
if editing {
return CGRect(x: leftPadding, y: 5, width: bounds.size.width, height: titleHeight())
}
return CGRect(x: leftPadding, y: titleHeight(), width: bounds.size.width, height: titleHeight())
}
}
The only issue is that I would like to centre the placeholder (which I can do) however I don't know how to move it to the location where the user will type (further down) when the text view becomes the first responder.
Any ideas?