Skip to content

Commit 2f2fb03

Browse files
author
BRANDERSTUDIO
committed
Add pod files. Changed swift version to '4.0'. Edited readme and podspec files.
1 parent 4228d8d commit 2f2fb03

25 files changed

+1625
-49
lines changed

.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.0

AnyFormatKit.podspec

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,18 @@
1-
#
2-
# Be sure to run `pod lib lint AnyFormatKit.podspec' to ensure this is a
3-
# valid spec before submitting.
4-
#
5-
# Any lines starting with a # are optional, but their use is encouraged
6-
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
7-
#
8-
91
Pod::Spec.new do |s|
102
s.name = 'AnyFormatKit'
113
s.version = '0.1.0'
12-
s.summary = 'A short description of AnyFormatKit.'
13-
14-
# This description is used to generate tags and improve search results.
15-
# * Think: What does it do? Why did you write it? What is the focus?
16-
# * Try to keep it short, snappy and to the point.
17-
# * Write the description between the DESC delimiters below.
18-
# * Finally, don't worry about the indent, CocoaPods strips it!
4+
s.summary = 'Simple text formatting in Swift.'
195

206
s.description = <<-DESC
21-
TODO: Add long description of the pod here.
7+
This library provide to format text with format like "## ##-###", where # - replaceble symbol. Support format all string or character by character input.
228
DESC
239

2410
s.homepage = 'https://github.com/luximetr/AnyFormatKit'
25-
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
2611
s.license = { :type => 'MIT', :file => 'LICENSE' }
2712
s.author = { 'luximetr' => '[email protected]' }
2813
s.source = { :git => 'https://github.com/luximetr/AnyFormatKit.git', :tag => s.version.to_s }
29-
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
3014

3115
s.ios.deployment_target = '8.0'
3216

3317
s.source_files = 'AnyFormatKit/Classes/**/*'
34-
35-
# s.resource_bundles = {
36-
# 'AnyFormatKit' => ['AnyFormatKit/Assets/*.png']
37-
# }
38-
39-
# s.public_header_files = 'Pod/Classes/**/*.h'
40-
# s.frameworks = 'UIKit', 'MapKit'
41-
# s.dependency 'AFNetworking', '~> 2.3'
4218
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// String+Extension.swift
3+
// TextInput
4+
//
5+
// Created by BRANDERSTUDIO on 23.10.2017.
6+
// Copyright © 2017 BRANDERSTUDIO. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
// MARK: - String extension
12+
extension String {
13+
/**
14+
Filter string with given regular expression
15+
16+
- Parameters:
17+
- regex: optional regular expression, that use for filter all symbols, that don't satisfy the RegEx
18+
19+
- Returns: optional filtered string with symbols, that satify to RegEx
20+
*/
21+
func filter(regex: String?) -> String {
22+
guard let regex = regex else { return self }
23+
let regexPredicate = NSPredicate(format: "SELF MATCHES %@", regex)
24+
let filteredCharacters = characters.filter { (character) -> Bool in
25+
regexPredicate.evaluate(with: String(character))
26+
}
27+
return String(filteredCharacters)
28+
}
29+
30+
/**
31+
Find and return character in string by index
32+
33+
- Parameters:
34+
- index: integer value, of character, that need to find
35+
36+
- Returns: found character or nil
37+
*/
38+
func characterAt(_ index: Int) -> Character? {
39+
guard index < characters.count else { return nil }
40+
return self[self.index(self.startIndex, offsetBy: index)]
41+
}
42+
43+
/// Lenght of string
44+
var length: Int {
45+
return self.characters.count
46+
}
47+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// MulticastDelegate.swift
3+
// TextInput
4+
//
5+
// Created by BRANDERSTUDIO on 25.10.2017.
6+
// Copyright © 2017 BRANDERSTUDIO. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/// Multiple delegate implementation
12+
public class MulticastDelegate <T> {
13+
/// List of delegates
14+
private let delegates: NSHashTable<AnyObject> = NSHashTable.weakObjects()
15+
16+
/**
17+
Add delegate in list of delegates.
18+
19+
- Parameters
20+
- delegate: Object, that what to be delegate
21+
*/
22+
public func add(delegate: T) {
23+
delegates.add(delegate as AnyObject)
24+
}
25+
26+
/**
27+
Remove delegate from delegate list
28+
29+
- Parameters:
30+
- delegate: Object, that must to be deleted from delegate list
31+
*/
32+
public func remove(delegate: T) {
33+
for oneDelegate in delegates.allObjects.reversed() {
34+
if oneDelegate === delegate as AnyObject {
35+
delegates.remove(oneDelegate)
36+
}
37+
}
38+
}
39+
40+
/**
41+
Method, that used for calling delegate methods for all delegates
42+
43+
- Parameters:
44+
- invocation: Closure, that will call for all delegates
45+
*/
46+
public func invoke(invocation: (T) -> ()) {
47+
for delegate in delegates.allObjects.reversed() {
48+
if let castedDelegate = delegate as? T {
49+
invocation(castedDelegate)
50+
}
51+
}
52+
}
53+
}
54+
55+
/**
56+
Overriden operator, that allow add delegate to delegate list
57+
58+
- Parameters:
59+
- left: Multicast delegate, that will add new delegate for own delegate list
60+
- right: Object, that wont to be delegate
61+
*/
62+
func += <T: AnyObject> (left: MulticastDelegate<T>, right: T) {
63+
left.add(delegate: right)
64+
}
65+
66+
/**
67+
Overriden operator, that allow remove delegate from delegate list
68+
69+
- Parameters:
70+
- left: Multicast delegate, that will remove delegate from own delegate list
71+
- right: Object, that wont to be remove from delegate list
72+
*/
73+
func -= <T: AnyObject> (left: MulticastDelegate<T>, right: T) {
74+
left.remove(delegate: right)
75+
}

AnyFormatKit/Classes/ReplaceMe.swift

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// TextFormatterProtocol.swift
3+
// TextInput
4+
//
5+
// Created by BRANDERSTUDIO on 18.10.2017.
6+
// Copyright © 2017 BRANDERSTUDIO. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/// Interface of text formatter
12+
public protocol TextFormatterProtocol {
13+
/**
14+
Formatting text with current textPattern
15+
16+
- Parameters:
17+
- unformatted: String, that need to be convert with current textPattern
18+
19+
- Returns: Formatted text with current textPattern
20+
*/
21+
func formattedText(from unformatted: String?) -> String?
22+
23+
/**
24+
Method for convert string, that sutisfy current textPattern, into unformatted string
25+
26+
- Parameters:
27+
- formatted: String, that will convert
28+
29+
- Returns: string converted into unformatted with current textPattern
30+
*/
31+
func unformattedText(from formatted: String?) -> String?
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// TextInputFormatterProtocol.swift
3+
// TextInput
4+
//
5+
// Created by BRANDERSTUDIO on 18.10.2017.
6+
// Copyright © 2017 BRANDERSTUDIO. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/// Interface for formatter of TextInput, that allow change format of text during input
12+
public protocol TextInputFormatterProtocol: TextFormatterProtocol {
13+
/// String, that always will be at beggining of textPattern text during typing
14+
var prefix: String? { get }
15+
16+
/// Current prefix with current format
17+
var formattedPrefix: String? { get }
18+
19+
// Regular expression, that discript allowed characters for input
20+
var allowedSymbolsRegex: String? { set get }
21+
22+
// MARK: - Public
23+
/**
24+
Method, that allow correct character by character input with specified format
25+
26+
- Parameters:
27+
- textInput: Object, that conform to TextInput protocol and represent input field with correcting content
28+
- range: Range, that determine which symbols must to be replaced
29+
- replacementString: String, that will replace old content in determined range
30+
31+
- Returns: Always return false (correct of textInput's content in method's body)
32+
*/
33+
func shouldChangeTextIn(
34+
textInput: TextInput, range: NSRange, replacementString text: String) -> Bool
35+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
// TextFormatter.swift
3+
// TextInput
4+
//
5+
// Created by BRANDERSTUDIO on 23.10.2017.
6+
// Copyright © 2017 BRANDERSTUDIO. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public class TextFormatter: TextFormatterProtocol {
12+
// MARK: - Fields
13+
14+
/// String, that will use for formatting of string replacing patter symbol, example: patternSymbol - "#", format - "### (###) ###-##-##"
15+
public let textPattern: String
16+
17+
/// Symbol that will be replace by input symbols
18+
public let patternSymbol: Character
19+
20+
// MARK: - Init
21+
/**
22+
Initializes formatter with pattern
23+
24+
- Parameters:
25+
- textPatterm: String, that will use for formatting of string replacing patter symbol
26+
- patternSymbol: Character, that will be replaced by input characters in textPattern
27+
*/
28+
public init(textPattern: String,
29+
patternSymbol: Character = TextFormatterConstants.defaultPatternSymbol) {
30+
self.textPattern = textPattern
31+
self.patternSymbol = patternSymbol
32+
}
33+
34+
// MARK: - TextFormatterProtocol
35+
/**
36+
Formatting text with current textPattern
37+
38+
- Parameters:
39+
- unformatted: String, that need to be convert with current textPattern
40+
41+
- Returns: Formatted text with current textPattern
42+
*/
43+
public func formattedText(from unformatted: String?) -> String? {
44+
guard let unformatted = unformatted else { return nil }
45+
var formatted = String.init()
46+
var unformattedIndex = 0
47+
var patternIndex = 0
48+
49+
while patternIndex < textPattern.length && unformattedIndex < unformatted.length {
50+
guard let patternCharacter = textPattern.characterAt(patternIndex) else { break }
51+
if patternCharacter == patternSymbol {
52+
if let unformattedCharacter = unformatted.characterAt(unformattedIndex) {
53+
formatted.append(unformattedCharacter)
54+
}
55+
unformattedIndex += 1
56+
} else {
57+
formatted.append(patternCharacter)
58+
}
59+
patternIndex += 1
60+
}
61+
return formatted
62+
}
63+
64+
/**
65+
Method for convert string, that sutisfy current textPattern, into unformatted string
66+
67+
- Parameters:
68+
- formatted: String, that will convert
69+
70+
- Returns: string converted into unformatted with current textPattern
71+
*/
72+
public func unformattedText(from formatted: String?) -> String? {
73+
guard let formatted = formatted else { return nil }
74+
var unformatted = String()
75+
var formattedIndex = 0
76+
77+
while formattedIndex < formatted.length {
78+
if let formattedCharacter = formatted.characterAt(formattedIndex) {
79+
if formattedIndex >= textPattern.length {
80+
unformatted.append(formattedCharacter)
81+
} else if formattedCharacter != textPattern.characterAt(formattedIndex) {
82+
unformatted.append(formattedCharacter)
83+
}
84+
formattedIndex += 1
85+
}
86+
}
87+
return unformatted
88+
}
89+
}
90+
91+
// MARK: - Constants
92+
public struct TextFormatterConstants {
93+
/// default pattern symbol in textPattern
94+
public static let defaultPatternSymbol: Character = "#"
95+
}

0 commit comments

Comments
 (0)