Force Lowercase - iOS Swift

Force lowercase - ios swift

First you should set following property on your textfield to restrict auto capitalisation:

textfield.autocapitalizationType = UITextAutocapitalizationType.None

And this is how you can restrict it further:

func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {

if let _ = string.rangeOfCharacterFromSet(NSCharacterSet.uppercaseLetterCharacterSet()) {
// Do not allow upper case letters
return false
}

return true
}

UPDATED FOR SWIFT 4

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

if let _ = string.rangeOfCharacter(from: .uppercaseLetters) {
// Do not allow upper case letters
return false
}

return true
}

Swift -Force UITextview Lowercase Only

Instead of shouldChangeTextIn, use textViewDidChange.

extension ViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
textView.text = textView.text.lowercased()
}
}

Result:

Typing while keyboard caps lock is on, but the text entered turns out all lowercased

Typing always (force) lowercase or uppercase - iOS swift

You should return false in if block because you already updated the textfield.

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if textField.isEqual(textFieldBodrum) {
textFieldBodrum.text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string.lowercaseString)
return false
} else if textField.isEqual(textFieldYalikavak) {
textFieldYalikavak.text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string.uppercaseString)
return false
}
return true
}

If you don't want to affect other textfields you should return true end of the shouldChangeCharactersInRange function.

https://gist.github.com/fatihyildizhan/ac5f476aebd306b0580a1fa069f153a3

Force a UITextField to lowercase while typing and retaining cursor position

I have taken your code and tried this.

I can replace the cursor position wherever the text is changed.

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

let start = textField.positionFromPosition(textField.beginningOfDocument, offset:range.location)

let cursorOffset = textField.offsetFromPosition(textField.beginningOfDocument, toPosition:start!) + string.characters.count

textField.text = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string).lowercaseString

let newCursorPosition = textField.positionFromPosition(textField.beginningOfDocument, offset:cursorOffset)

let newSelectedRange = textField.textRangeFromPosition(newCursorPosition!, toPosition:newCursorPosition!)

textField.selectedTextRange = newSelectedRange

return false
}

SwiftUI TextField force lowercase

You can create a custom binding and set your state URL variable to the lowercased version of the input through it:

struct ContentView: View {
@State var url: String = ""

var body: some View {
let binding = Binding<String>(get: {
self.url
}, set: {
self.url = $0.lowercased()
})

return VStack {
TextField("Enter URL", text: binding)
}

}
}

Force all attribute keys and values of a swift class in lowercase

Like this:

struct Person : Codable {
var firstName: String
var lastName: String
var city: String
enum CodingKeys : String, CodingKey {
case firstName = "firstname"
case lastName = "lastname"
case city
}
func encode(to encoder: Encoder) throws {
var con = encoder.container(keyedBy: CodingKeys.self)
try! con.encode(self.firstName.lowercased(), forKey: .firstName)
try! con.encode(self.lastName.lowercased(), forKey: .lastName)
try! con.encode(self.city.lowercased(), forKey: .city)
}
}

Testing it:

let person = Person(firstName: "David", lastName: "Gill", city:
"Toronto")
let encoder = JSONEncoder()
let encoded = try encoder.encode(person)
print(String(decoding: encoded, as: UTF8.self))
// {"firstname":"david","lastname":"gill","city":"toronto"}

You can probably incorporate your .convertToLowerCase to save some work.



Related Topics



Leave a reply



Submit