Last active
July 12, 2019 11:34
-
-
Save dedeexe/d9a43894081317e7c418b96d1d081b25 to your computer and use it in GitHub Desktop.
Simples And Fast String Mask Formatter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// StringMaskFormatter.swift | |
// StringMaskFormatter | |
// | |
// Created by dede.exe on 10/07/16. | |
// It's totally base on article : "http://vojtastavik.com/2015/03/29/real-time-formatting-in-uitextfield-swift-basics/" | |
// And I don't mind if the original author should allow me(or not) to publish it :)... But I'll keep the reference | |
// | |
import UIKit | |
public struct StringMaskFormatter { | |
public var pattern : String = "" | |
public var replecementChar : Character = "*" | |
public var allowNumbers : Bool = true | |
public var allowText : Bool = false | |
public init(pattern:String, replecementChar:Character="*", allowNumbers:Bool=true, allowText:Bool=true) | |
{ | |
self.pattern = pattern | |
self.replecementChar = replecementChar | |
self.allowNumbers = allowNumbers | |
self.allowText = allowText | |
} | |
private func prepareString(string:String) -> String { | |
var charSet : NSCharacterSet! | |
if allowText && allowNumbers { | |
charSet = NSCharacterSet.alphanumericCharacterSet().invertedSet | |
} | |
else if allowText { | |
charSet = NSCharacterSet.letterCharacterSet().invertedSet | |
} | |
else if allowNumbers { | |
charSet = NSCharacterSet.decimalDigitCharacterSet().invertedSet | |
} | |
let result = string.componentsSeparatedByCharactersInSet(charSet) | |
return result.joinWithSeparator("") | |
} | |
public func createFormattedStringFrom(text:String) -> String | |
{ | |
var resultString = "" | |
if text.characters.count > 0 && pattern.characters.count > 0 | |
{ | |
var finalText = "" | |
var stop = false | |
let tempString = prepareString(text) | |
var formatIndex = pattern.startIndex | |
var tempIndex = tempString.startIndex | |
while !stop | |
{ | |
let formattingPatternRange = formatIndex ..< formatIndex.advancedBy(1) | |
if pattern.substringWithRange(formattingPatternRange) != String(replecementChar) { | |
finalText = finalText.stringByAppendingString(pattern.substringWithRange(formattingPatternRange)) | |
} | |
else if tempString.characters.count > 0 { | |
let pureStringRange = tempIndex ..< tempIndex.advancedBy(1) | |
finalText = finalText.stringByAppendingString(tempString.substringWithRange(pureStringRange)) | |
tempIndex = tempIndex.advancedBy(1) | |
} | |
formatIndex = formatIndex.advancedBy(1) | |
if formatIndex >= pattern.endIndex || tempIndex >= tempString.endIndex { | |
stop = true | |
} | |
resultString = finalText | |
} | |
} | |
return resultString | |
} | |
} | |
//MARK: - Pre defined formats | |
extension StringMaskFormatter { | |
static func CPFPatternFactory() -> StringMaskFormatter { | |
return StringMaskFormatter(pattern: "***.***.***-**", replecementChar: "*", allowNumbers: true, allowText: false) | |
} | |
static func CreditCardPatternFactory() -> StringMaskFormatter { | |
return StringMaskFormatter(pattern: "**** **** **** ****", replecementChar: "*", allowNumbers: true, allowText: false) | |
} | |
static func MMYYDatePatternFactory() -> StringMaskFormatter { | |
return StringMaskFormatter(pattern: "**/**", replecementChar: "*", allowNumbers: true, allowText: false) | |
} | |
static func CompleteDatePatternFactory() -> StringMaskFormatter { | |
return StringMaskFormatter(pattern: "**/**/****", replecementChar: "*", allowNumbers: true, allowText: false) | |
} | |
static func SimpleDatePatternFactory() -> StringMaskFormatter { | |
return StringMaskFormatter(pattern: "**/**/**", replecementChar: "*", allowNumbers: true, allowText: false) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment