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 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
Can't Add a New iOS Version for My App
How to Write a Better Data Access Layer with Realm
Uialertview's Textfield Does Not Show Keyboard in iOS8
Testflight Sdk and iOS Simulator - How to Use
How to Use Sketch UI Elements Directly into Xcode
Sprite Kit Collision Detection
Translate Just 4 Lines of Code from Objective C to Swift (Pointers)
Error | [Ios] File Patterns: the 'Source_Files' Pattern Did Not Match Any File
Swift 4.2 Make Bottom of Tableview Move Up When Keyboard Shows
Remove Duplicate Structs in Array Based on Struct Property in Swift
Using String.Characterview.Index.Successor() in for Statements
Phonegap Notification.Alert Not Working
Invalid Update: Invalid Number of Sections in Uitableview
Force UIsplitviewcontroller to Always Show Master (Only) in Landscape (On iPhone 6 Plus)
iOS 13.2 Message: Nehelper Sent Invalid Result Code [1] for Wi-Fi Information Request
Uitableview Scroll Erases Data in Text Field Inside UItableviewcell
Restkit 0.20 JSON Mapping Along with Additional Offline Data