Skip to content

Commit

Permalink
Create UITextField extension for set a selected range via `Range<St…
Browse files Browse the repository at this point in the history
…ring.Index>`
  • Loading branch information
seaburg committed Oct 25, 2016
1 parent 9b1aab2 commit d6535a4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions FormattedTextField.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
objects = {

/* Begin PBXBuildFile section */
8FD40B471DBEB3840015DB1D /* UITextFieldExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FD40B461DBEB3840015DB1D /* UITextFieldExtension.swift */; };
8FFF615C1DBC074E0033E1FC /* FormattedTextField.h in Headers */ = {isa = PBXBuildFile; fileRef = 8FFF615A1DBC074E0033E1FC /* FormattedTextField.h */; settings = {ATTRIBUTES = (Public, ); }; };
8FFF61641DBC07C70033E1FC /* FormattedTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FFF61621DBC07C70033E1FC /* FormattedTextField.swift */; };
8FFF61651DBC07C70033E1FC /* StringExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FFF61631DBC07C70033E1FC /* StringExtensions.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
8FD40B461DBEB3840015DB1D /* UITextFieldExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITextFieldExtension.swift; sourceTree = "<group>"; };
8FFF61571DBC074D0033E1FC /* FormattedTextField.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FormattedTextField.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8FFF615A1DBC074E0033E1FC /* FormattedTextField.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FormattedTextField.h; sourceTree = "<group>"; };
8FFF615B1DBC074E0033E1FC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -52,6 +54,7 @@
children = (
8FFF61621DBC07C70033E1FC /* FormattedTextField.swift */,
8FFF61631DBC07C70033E1FC /* StringExtensions.swift */,
8FD40B461DBEB3840015DB1D /* UITextFieldExtension.swift */,
8FFF615A1DBC074E0033E1FC /* FormattedTextField.h */,
8FFF615B1DBC074E0033E1FC /* Info.plist */,
);
Expand Down Expand Up @@ -139,6 +142,7 @@
buildActionMask = 2147483647;
files = (
8FFF61651DBC07C70033E1FC /* StringExtensions.swift in Sources */,
8FD40B471DBEB3840015DB1D /* UITextFieldExtension.swift in Sources */,
8FFF61641DBC07C70033E1FC /* FormattedTextField.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
41 changes: 41 additions & 0 deletions FormattedTextField/UITextFieldExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// UITextFieldExtension.swift
// FormattedTextField
//
// Created by Evgeniy Yurtaev on 25/10/2016.
// Copyright © 2016 Evgeniy Yurtaev. All rights reserved.
//

import UIKit

extension UITextField {
var selectedCharachtersRange: Range<String.Index>? {
get {
guard let selectedTextRange = selectedTextRange else {
return nil
}
guard let text = text else {
return nil
}
let location = offset(from: beginningOfDocument, to: selectedTextRange.start)
let length = offset(from: selectedTextRange.start, to: selectedTextRange.end)
let range = text.range(fromUtf16NsRange: NSMakeRange(location, length))

return range
}
set(value) {
guard let value = value else {
selectedTextRange = nil
return
}
guard let text = text else {
return
}
let utf16Range = text.utf16Nsrange(fromRange: value)

let from = position(from: beginningOfDocument, offset: utf16Range.location)!
let to = position(from: from, offset: utf16Range.length)!
selectedTextRange = textRange(from: from, to: to)
}
}
}

0 comments on commit d6535a4

Please sign in to comment.