-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pod files. Changed swift version to '4.0'. Edited readme and pods…
…pec files.
- Loading branch information
BRANDERSTUDIO
committed
Oct 31, 2017
1 parent
4228d8d
commit 2f2fb03
Showing
25 changed files
with
1,625 additions
and
49 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4.0 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,18 @@ | ||
# | ||
# Be sure to run `pod lib lint AnyFormatKit.podspec' to ensure this is a | ||
# valid spec before submitting. | ||
# | ||
# Any lines starting with a # are optional, but their use is encouraged | ||
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html | ||
# | ||
|
||
Pod::Spec.new do |s| | ||
s.name = 'AnyFormatKit' | ||
s.version = '0.1.0' | ||
s.summary = 'A short description of AnyFormatKit.' | ||
|
||
# This description is used to generate tags and improve search results. | ||
# * Think: What does it do? Why did you write it? What is the focus? | ||
# * Try to keep it short, snappy and to the point. | ||
# * Write the description between the DESC delimiters below. | ||
# * Finally, don't worry about the indent, CocoaPods strips it! | ||
s.summary = 'Simple text formatting in Swift.' | ||
|
||
s.description = <<-DESC | ||
TODO: Add long description of the pod here. | ||
This library provide to format text with format like "## ##-###", where # - replaceble symbol. Support format all string or character by character input. | ||
DESC | ||
|
||
s.homepage = 'https://github.com/luximetr/AnyFormatKit' | ||
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' | ||
s.license = { :type => 'MIT', :file => 'LICENSE' } | ||
s.author = { 'luximetr' => 'alexandr.orlov@brander.ua' } | ||
s.source = { :git => 'https://github.com/luximetr/AnyFormatKit.git', :tag => s.version.to_s } | ||
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>' | ||
|
||
s.ios.deployment_target = '8.0' | ||
|
||
s.source_files = 'AnyFormatKit/Classes/**/*' | ||
|
||
# s.resource_bundles = { | ||
# 'AnyFormatKit' => ['AnyFormatKit/Assets/*.png'] | ||
# } | ||
|
||
# s.public_header_files = 'Pod/Classes/**/*.h' | ||
# s.frameworks = 'UIKit', 'MapKit' | ||
# s.dependency 'AFNetworking', '~> 2.3' | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// String+Extension.swift | ||
// TextInput | ||
// | ||
// Created by BRANDERSTUDIO on 23.10.2017. | ||
// Copyright © 2017 BRANDERSTUDIO. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - String extension | ||
extension String { | ||
/** | ||
Filter string with given regular expression | ||
- Parameters: | ||
- regex: optional regular expression, that use for filter all symbols, that don't satisfy the RegEx | ||
- Returns: optional filtered string with symbols, that satify to RegEx | ||
*/ | ||
func filter(regex: String?) -> String { | ||
guard let regex = regex else { return self } | ||
let regexPredicate = NSPredicate(format: "SELF MATCHES %@", regex) | ||
let filteredCharacters = characters.filter { (character) -> Bool in | ||
regexPredicate.evaluate(with: String(character)) | ||
} | ||
return String(filteredCharacters) | ||
} | ||
|
||
/** | ||
Find and return character in string by index | ||
- Parameters: | ||
- index: integer value, of character, that need to find | ||
- Returns: found character or nil | ||
*/ | ||
func characterAt(_ index: Int) -> Character? { | ||
guard index < characters.count else { return nil } | ||
return self[self.index(self.startIndex, offsetBy: index)] | ||
} | ||
|
||
/// Lenght of string | ||
var length: Int { | ||
return self.characters.count | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
AnyFormatKit/Classes/MulticastDelegate/MulticastDelegate.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// MulticastDelegate.swift | ||
// TextInput | ||
// | ||
// Created by BRANDERSTUDIO on 25.10.2017. | ||
// Copyright © 2017 BRANDERSTUDIO. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Multiple delegate implementation | ||
public class MulticastDelegate <T> { | ||
/// List of delegates | ||
private let delegates: NSHashTable<AnyObject> = NSHashTable.weakObjects() | ||
|
||
/** | ||
Add delegate in list of delegates. | ||
- Parameters | ||
- delegate: Object, that what to be delegate | ||
*/ | ||
public func add(delegate: T) { | ||
delegates.add(delegate as AnyObject) | ||
} | ||
|
||
/** | ||
Remove delegate from delegate list | ||
- Parameters: | ||
- delegate: Object, that must to be deleted from delegate list | ||
*/ | ||
public func remove(delegate: T) { | ||
for oneDelegate in delegates.allObjects.reversed() { | ||
if oneDelegate === delegate as AnyObject { | ||
delegates.remove(oneDelegate) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
Method, that used for calling delegate methods for all delegates | ||
- Parameters: | ||
- invocation: Closure, that will call for all delegates | ||
*/ | ||
public func invoke(invocation: (T) -> ()) { | ||
for delegate in delegates.allObjects.reversed() { | ||
if let castedDelegate = delegate as? T { | ||
invocation(castedDelegate) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
Overriden operator, that allow add delegate to delegate list | ||
- Parameters: | ||
- left: Multicast delegate, that will add new delegate for own delegate list | ||
- right: Object, that wont to be delegate | ||
*/ | ||
func += <T: AnyObject> (left: MulticastDelegate<T>, right: T) { | ||
left.add(delegate: right) | ||
} | ||
|
||
/** | ||
Overriden operator, that allow remove delegate from delegate list | ||
- Parameters: | ||
- left: Multicast delegate, that will remove delegate from own delegate list | ||
- right: Object, that wont to be remove from delegate list | ||
*/ | ||
func -= <T: AnyObject> (left: MulticastDelegate<T>, right: T) { | ||
left.remove(delegate: right) | ||
} |
Empty file.
32 changes: 32 additions & 0 deletions
32
AnyFormatKit/Classes/TextFormatter/TextFormatterProtocols/TextFormatterProtocol.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// TextFormatterProtocol.swift | ||
// TextInput | ||
// | ||
// Created by BRANDERSTUDIO on 18.10.2017. | ||
// Copyright © 2017 BRANDERSTUDIO. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Interface of text formatter | ||
public protocol TextFormatterProtocol { | ||
/** | ||
Formatting text with current textPattern | ||
- Parameters: | ||
- unformatted: String, that need to be convert with current textPattern | ||
- Returns: Formatted text with current textPattern | ||
*/ | ||
func formattedText(from unformatted: String?) -> String? | ||
|
||
/** | ||
Method for convert string, that sutisfy current textPattern, into unformatted string | ||
- Parameters: | ||
- formatted: String, that will convert | ||
- Returns: string converted into unformatted with current textPattern | ||
*/ | ||
func unformattedText(from formatted: String?) -> String? | ||
} |
35 changes: 35 additions & 0 deletions
35
AnyFormatKit/Classes/TextFormatter/TextFormatterProtocols/TextInputFormatterProtocol.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// TextInputFormatterProtocol.swift | ||
// TextInput | ||
// | ||
// Created by BRANDERSTUDIO on 18.10.2017. | ||
// Copyright © 2017 BRANDERSTUDIO. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Interface for formatter of TextInput, that allow change format of text during input | ||
public protocol TextInputFormatterProtocol: TextFormatterProtocol { | ||
/// String, that always will be at beggining of textPattern text during typing | ||
var prefix: String? { get } | ||
|
||
/// Current prefix with current format | ||
var formattedPrefix: String? { get } | ||
|
||
// Regular expression, that discript allowed characters for input | ||
var allowedSymbolsRegex: String? { set get } | ||
|
||
// MARK: - Public | ||
/** | ||
Method, that allow correct character by character input with specified format | ||
- Parameters: | ||
- textInput: Object, that conform to TextInput protocol and represent input field with correcting content | ||
- range: Range, that determine which symbols must to be replaced | ||
- replacementString: String, that will replace old content in determined range | ||
- Returns: Always return false (correct of textInput's content in method's body) | ||
*/ | ||
func shouldChangeTextIn( | ||
textInput: TextInput, range: NSRange, replacementString text: String) -> Bool | ||
} |
95 changes: 95 additions & 0 deletions
95
AnyFormatKit/Classes/TextFormatter/TextInputFormatters/TextFormatter.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// TextFormatter.swift | ||
// TextInput | ||
// | ||
// Created by BRANDERSTUDIO on 23.10.2017. | ||
// Copyright © 2017 BRANDERSTUDIO. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class TextFormatter: TextFormatterProtocol { | ||
// MARK: - Fields | ||
|
||
/// String, that will use for formatting of string replacing patter symbol, example: patternSymbol - "#", format - "### (###) ###-##-##" | ||
public let textPattern: String | ||
|
||
/// Symbol that will be replace by input symbols | ||
public let patternSymbol: Character | ||
|
||
// MARK: - Init | ||
/** | ||
Initializes formatter with pattern | ||
- Parameters: | ||
- textPatterm: String, that will use for formatting of string replacing patter symbol | ||
- patternSymbol: Character, that will be replaced by input characters in textPattern | ||
*/ | ||
public init(textPattern: String, | ||
patternSymbol: Character = TextFormatterConstants.defaultPatternSymbol) { | ||
self.textPattern = textPattern | ||
self.patternSymbol = patternSymbol | ||
} | ||
|
||
// MARK: - TextFormatterProtocol | ||
/** | ||
Formatting text with current textPattern | ||
- Parameters: | ||
- unformatted: String, that need to be convert with current textPattern | ||
- Returns: Formatted text with current textPattern | ||
*/ | ||
public func formattedText(from unformatted: String?) -> String? { | ||
guard let unformatted = unformatted else { return nil } | ||
var formatted = String.init() | ||
var unformattedIndex = 0 | ||
var patternIndex = 0 | ||
|
||
while patternIndex < textPattern.length && unformattedIndex < unformatted.length { | ||
guard let patternCharacter = textPattern.characterAt(patternIndex) else { break } | ||
if patternCharacter == patternSymbol { | ||
if let unformattedCharacter = unformatted.characterAt(unformattedIndex) { | ||
formatted.append(unformattedCharacter) | ||
} | ||
unformattedIndex += 1 | ||
} else { | ||
formatted.append(patternCharacter) | ||
} | ||
patternIndex += 1 | ||
} | ||
return formatted | ||
} | ||
|
||
/** | ||
Method for convert string, that sutisfy current textPattern, into unformatted string | ||
- Parameters: | ||
- formatted: String, that will convert | ||
- Returns: string converted into unformatted with current textPattern | ||
*/ | ||
public func unformattedText(from formatted: String?) -> String? { | ||
guard let formatted = formatted else { return nil } | ||
var unformatted = String() | ||
var formattedIndex = 0 | ||
|
||
while formattedIndex < formatted.length { | ||
if let formattedCharacter = formatted.characterAt(formattedIndex) { | ||
if formattedIndex >= textPattern.length { | ||
unformatted.append(formattedCharacter) | ||
} else if formattedCharacter != textPattern.characterAt(formattedIndex) { | ||
unformatted.append(formattedCharacter) | ||
} | ||
formattedIndex += 1 | ||
} | ||
} | ||
return unformatted | ||
} | ||
} | ||
|
||
// MARK: - Constants | ||
public struct TextFormatterConstants { | ||
/// default pattern symbol in textPattern | ||
public static let defaultPatternSymbol: Character = "#" | ||
} |
Oops, something went wrong.