Comments (4)
@andreikeda the problem is that if I use {ddddd}-{d}
the -
will only be placed when the user inputs 6 characters. I needed it to be placed before the last character even if there is only 4 characters.
@BruhhGarcia @kvacquier I wind up implementing it by myself using the UITextFieldDelegate
, here goes the code that fit my needs:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//Getting the textfield text
var text: NSString = textField.text! as NSString
text = text.replacingCharacters(in: range, with: string) as NSString
//Adding the textfield text on a mutable string
var mutableString: NSMutableString = text.mutableCopy() as! NSMutableString
//Verifying if the user not tapped the delete key
if(!(range.length == 1 && string.lengthOfBytes(using: String.Encoding.utf8) == 0)) {
//Adding the formatter character at the correct position
if (mutableString.length > 1) {
mutableString = (mutableString.replacingOccurrences(of: "-", with: "") as NSString).mutableCopy() as! NSMutableString
mutableString.insert("-", at: mutableString.length - 1)
}
}
let char = string.cString(using: String.Encoding.utf8)!
let isBackSpace = strcmp(char, "\\b")
if (isBackSpace == -92 && mutableString.length > 1) {
mutableString = (mutableString.replacingCharacters(in: NSMakeRange(mutableString.length - 1, 1), with: "") as NSString).mutableCopy() as! NSMutableString
if (mutableString.length > 1) {
mutableString = (mutableString.replacingOccurrences(of: "-", with: "") as NSString).mutableCopy() as! NSMutableString
mutableString.insert("-", at: mutableString.length - 1)
}
}
//Putting the string with the formatter character on the textfield
textField.text = mutableString as String
return false
}
Maybe with some adjustments you will be able to adapt it to your needs.
from akmaskfield.
I would like to use it for an IP Address and i have the same exact question
from akmaskfield.
@filipealva I don't know if I understand your question, but you can make something like this:
`let EXPRESSION: String! = "{ddddd}-{d}"
let TEMPLATE: String! = "ddddd-d"
yourTextField.maskTemplate = TEMPLATE
yourTextField.maskExpression = EXPRESSION`
from akmaskfield.
Hi,
I have the same question. In my case I have a phone number that could be ({dd}){dddd}-{dddd} or ({dd}){ddddd}-{dddd}
Is there a way to use two different expression in the same text field?
from akmaskfield.
Related Issues (20)
- Get Original Text (Swift 3) HOT 1
- CocoaPods installs v1.0 only HOT 4
- Clear button not clearing through blocks
- Crashes on setting a placeholder without a mask
- Use of undeclared type 'AKMaskField' HOT 1
- Format a label
- Swift 3 xCode 8.3.2 Does not launch Simulator HOT 1
- Cant find the text in MaskBlock
- Manual install
- textFieldDidChange not working HOT 4
- Swift 4
- Question
- Swift 4.1 issue
- Swift 4.1 HOT 1
- Cursor position and selection HOT 1
- Swift 5 HOT 6
- MaskField in cell reloading
- How to reset all mask settings
- Deleting written character right away
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from akmaskfield.