Skip to content

Commit

Permalink
Merge pull request #5 from xavierdonnellon/master
Browse files Browse the repository at this point in the history
Character Limit
  • Loading branch information
AlexFine authored Feb 14, 2021
2 parents c90130a + ee276ec commit d272db0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Modifier | Description
`.keyboardType(_ type: UIKeyboardType)` | Modifies the text field’s **keyboard type**. 📩
`.autocapitalization(_ style: UITextAutocapitalizationType)` | Modifies the text field’s **autocapitalization** style. 🔡
`.returnKeyType(_ type: UIReturnKeyType)` | Modifies the text field’s **return key** type. ✅
`.characterLimit(_ limit: Int?)` | Sets the maximum amount of characters allowed in this text field.
`.isSecure(_ isSecure: Bool)` | Modifies the text field’s **secure entry** settings. 🔒
`.clearsOnBeginEditing(_ shouldClear: Bool)` | Modifies the **clear-on-begin-editing** setting of a text field. ❌
`clearsOnInsertion(_ shouldClear: Bool)` | Modifies the **clear-on-insertion** setting of a text field. 👆
Expand Down
10 changes: 10 additions & 0 deletions Sources/iTextField/iTextField+ViewModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import UIKit

@available(iOS 13.0, *)
extension iTextField {

/// Sets the maximum amount of characters allowed in this text field.
/// - Parameter limit: the maximum amount of characters allowed
/// - Returns: An updated text field limited to limit
public func characterLimit(_ limit: Int?) -> iTextField {
var view = self
view.characterLimit = limit
return view
}

/// Modifies the text field’s **font** from a `UIFont` object. 🔠🔡
/// - Parameter font: The desired font 🅰️🆗
/// - Returns: An updated text field using the desired font 💬
Expand Down
32 changes: 25 additions & 7 deletions Sources/iTextField/iTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public struct iTextField: UIViewRepresentable {
var autocapitalization: UITextAutocapitalizationType = .sentences
var keyboardType: UIKeyboardType = .default
var returnKeyType: UIReturnKeyType = .default
var characterLimit: Int? = nil

var isSecure = false
var isUserInteractionEnabled = true
Expand Down Expand Up @@ -169,18 +170,22 @@ public struct iTextField: UIViewRepresentable {
}

public func makeCoordinator() -> Coordinator {
return Coordinator(text: $text,
isEditing: isEditing,
didBeginEditing: didBeginEditing,
didChange: didChange,
didEndEditing: didEndEditing,
shouldReturn: shouldReturn,
shouldClear: shouldClear)
return Coordinator(
text: $text,
isEditing: isEditing,
characterLimit: characterLimit,
didBeginEditing: didBeginEditing,
didChange: didChange,
didEndEditing: didEndEditing,
shouldReturn: shouldReturn,
shouldClear: shouldClear
)
}

public final class Coordinator: NSObject, UITextFieldDelegate {
@Binding var text: String
@Binding var isEditing: Bool
var characterLimit: Int? = nil

var didBeginEditing: () -> Void
var didChange: () -> Void
Expand All @@ -190,6 +195,7 @@ public struct iTextField: UIViewRepresentable {

init(text: Binding<String>,
isEditing: Binding<Bool>,
characterLimit: Int?,
didBeginEditing: @escaping () -> Void,
didChange: @escaping () -> Void,
didEndEditing: @escaping () -> Void,
Expand All @@ -198,6 +204,7 @@ public struct iTextField: UIViewRepresentable {
{
self._text = text
self._isEditing = isEditing
self.characterLimit = characterLimit
self.didBeginEditing = didBeginEditing
self.didChange = didChange
self.didEndEditing = didEndEditing
Expand Down Expand Up @@ -242,5 +249,16 @@ public struct iTextField: UIViewRepresentable {
text = ""
return false
}

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//if there is a character limit set and new text will be greater than limt, then don't allow the newly proposed edit
if let limit = characterLimit, let text = textField.text {
if text.count + string.count > limit {
return false
}
}

return true
}
}
}

0 comments on commit d272db0

Please sign in to comment.