Skip to content

Instantly share code, notes, and snippets.

@JasonCanCode
Created October 8, 2018 18:32
Show Gist options
  • Save JasonCanCode/aa0dbb097bffb8ca1ec7ebb435db8681 to your computer and use it in GitHub Desktop.
Save JasonCanCode/aa0dbb097bffb8ca1ec7ebb435db8681 to your computer and use it in GitHub Desktop.
Have trailing text persist in a text field.
import UIKit
class PostFixTextField: UITextField {
@IBInspectable var postfixColor: UIColor?
@IBInspectable var shouldRemovePostfixOnEditing: Bool = false
@IBInspectable var postfixText: String = "" {
willSet {
removePostFix()
}
didSet {
textWasChanged()
}
}
private var shouldUpdateWithPostfix: Bool = false
private var originalTextColor: UIColor!
var originalText: String? {
return self.text?.replacingOccurrences(of: postfixText, with: "")
}
var originalTextCount: Int {
return originalText?.count ?? 0
}
func setText(_ newText: String?) {
guard let text = newText,
!text.isEmpty else {
self.text = nil
return
}
self.text = text
addPostFix()
attributeText()
}
override func awakeFromNib() {
super.awakeFromNib()
self.originalTextColor = textColor ?? .black
self.addTarget(self, action: #selector(textWillChange), for: .editingDidBegin)
self.addTarget(self, action: #selector(textChanged), for: .editingChanged)
self.addTarget(self, action: #selector(textWasChanged), for: .editingDidEnd)
}
@objc func textWillChange() {
if shouldRemovePostfixOnEditing {
removePostFix()
textColor = originalTextColor
} else {
setCursorPosition(input: self, position: originalTextCount)
}
}
@objc func textChanged() {
if !shouldRemovePostfixOnEditing {
textWasChanged()
setCursorPosition(input: self, position: originalTextCount)
}
}
@objc func textWasChanged() {
guard originalTextCount > 0 else {
self.text = nil
return
}
removePostFix()
addPostFix()
attributeText()
}
private func setCursorPosition(input: UITextField, position: Int) {
guard let position = input.position(from: input.beginningOfDocument, offset: position) else {
return
}
input.selectedTextRange = input.textRange(from: position, to: position)
}
private func addPostFix() {
guard let text = self.text,
!text.isEmpty else {
return
}
self.text = text + postfixText
}
private func removePostFix() {
self.text = originalText
}
private func attributeText() {
guard originalTextCount > 0,
let originalText = originalText,
let postfixColor = postfixColor else {
return
}
let originalTextAttributes = [NSAttributedStringKey.foregroundColor: originalTextColor ?? .black]
let originalTextRange = NSRange(location: 0, length: originalText.count)
let postfixAttributes = [NSAttributedStringKey.foregroundColor: postfixColor]
let postfixRange = NSRange(location: originalText.count, length: postfixText.count)
let attributedString = NSMutableAttributedString(string: originalText + postfixText)
attributedString.setAttributes(originalTextAttributes, range: originalTextRange)
attributedString.setAttributes(postfixAttributes, range: postfixRange)
self.attributedText = attributedString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment