How can I convert NSRange to Range<String.Index> in Swift?
I want to use the following UITextFieldDelegate method:
func textField(textField: UITextField!,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String!) -> Bool {
textField.text.stringByReplacingCharactersInRange(???, withString: string)
As of Swift 4 (Xcode 9), the Swift standard
library provides methods to convert between Swift string ranges
(Range<String.Index>) and NSString ranges (NSRange).
Example:
let str = "aπΏbπ©πͺc"
let r1 = str.range(of: "π©πͺ")!
// String range to NSRange:
let n1 = NSRange(r1, in: str)
print((str as NSString).substring(with: n1)) // π©πͺ
// NSRange back to String range:
let r2 = Range(n1, in: str)!
print(str[r2]) // π©πͺ
Therefore the text replacement in the text field delegate method
can now be done as
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
if let oldString = textField.text {
let newString = oldString.replacingCharacters(in: Range(range, in: oldString)!,
with: string)
// ...
}
// ...
}
(Older answers for Swift 3 and earlier:)
As of Swift 1.2, String.Index has an initializer
init?(_ utf16Index: UTF16Index, within characters: String)
which can be used to convert NSRange to Range<String.Index> correctly
(including all cases of Emojis, Regional Indicators or other extended
grapheme clusters) without intermediate conversion to an NSString:
extension String {
func rangeFromNSRange(nsRange : NSRange) -> Range<String.Index>? {
let from16 = advance(utf16.startIndex, nsRange.location, utf16.endIndex)
let to16 = advance(from16, nsRange.length, utf16.endIndex)
if let from = String.Index(from16, within: self),
let to = String.Index(to16, within: self) {
return from ..< to
}
return nil
}
}
This method returns an optional string range because not all NSRanges
are valid for a given Swift string.
The UITextFieldDelegate delegate method can then be written as
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if let swRange = textField.text.rangeFromNSRange(range) {
let newString = textField.text.stringByReplacingCharactersInRange(swRange, withString: string)
// ...
}
return true
}
The inverse conversion is
extension String {
func NSRangeFromRange(range : Range<String.Index>) -> NSRange {
let utf16view = self.utf16
let from = String.UTF16View.Index(range.startIndex, within: utf16view)
let to = String.UTF16View.Index(range.endIndex, within: utf16view)
return NSMakeRange(from - utf16view.startIndex, to - from)
}
}
A simple test:
let str = "aπΏbπ©πͺc"
let r1 = str.rangeOfString("π©πͺ")!
// String range to NSRange:
let n1 = str.NSRangeFromRange(r1)
println((str as NSString).substringWithRange(n1)) // π©πͺ
// NSRange back to String range:
let r2 = str.rangeFromNSRange(n1)!
println(str.substringWithRange(r2)) // π©πͺ
Update for Swift 2:
The Swift 2 version of rangeFromNSRange() was already given
by Serhii Yakovenko in this answer, I am including it
here for completeness:
extension String {
func rangeFromNSRange(nsRange : NSRange) -> Range<String.Index>? {
let from16 = utf16.startIndex.advancedBy(nsRange.location, limit: utf16.endIndex)
let to16 = from16.advancedBy(nsRange.length, limit: utf16.endIndex)
if let from = String.Index(from16, within: self),
let to = String.Index(to16, within: self) {
return from ..< to
}
return nil
}
}
The Swift 2 version of NSRangeFromRange() is
extension String {
func NSRangeFromRange(range : Range<String.Index>) -> NSRange {
let utf16view = self.utf16
let from = String.UTF16View.Index(range.startIndex, within: utf16view)
let to = String.UTF16View.Index(range.endIndex, within: utf16view)
return NSMakeRange(utf16view.startIndex.distanceTo(from), from.distanceTo(to))
}
}
Update for Swift 3 (Xcode 8):
extension String {
func nsRange(from range: Range<String.Index>) -> NSRange {
let from = range.lowerBound.samePosition(in: utf16)
let to = range.upperBound.samePosition(in: utf16)
return NSRange(location: utf16.distance(from: utf16.startIndex, to: from),
length: utf16.distance(from: from, to: to))
}
}
extension String {
func range(from nsRange: NSRange) -> Range<String.Index>? {
guard
let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),
let to16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location + nsRange.length, limitedBy: utf16.endIndex),
let from = from16.samePosition(in: self),
let to = to16.samePosition(in: self)
else { return nil }
return from ..< to
}
}
Example:
let str = "aπΏbπ©πͺc"
let r1 = str.range(of: "π©πͺ")!
// String range to NSRange:
let n1 = str.nsRange(from: r1)
print((str as NSString).substring(with: n1)) // π©πͺ
// NSRange back to String range:
let r2 = str.range(from: n1)!
print(str.substring(with: r2)) // π©πͺ
The NSString version (as opposed to Swift String) of replacingCharacters(in: NSRange, with: NSString) accepts an NSRange, so one simple solution is to convert String to NSString first. The delegate and replacement method names are slightly different in Swift 3 and 2, so depending on which Swift you're using:
Swift 3.0
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
let nsString = textField.text as NSString?
let newString = nsString?.replacingCharacters(in: range, with: string)
}
Swift 2.x
func textField(textField: UITextField,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String) -> Bool {
let nsString = textField.text as NSString?
let newString = nsString?.stringByReplacingCharactersInRange(range, withString: string)
}
This answer by Martin R seems to be correct because it accounts for Unicode.
However at the time of the post (Swift 1) his code doesn't compile in Swift 2.0 (Xcode 7), because they removed advance() function. Updated version is below:
Swift 2
extension String {
func rangeFromNSRange(nsRange : NSRange) -> Range<String.Index>? {
let from16 = utf16.startIndex.advancedBy(nsRange.location, limit: utf16.endIndex)
let to16 = from16.advancedBy(nsRange.length, limit: utf16.endIndex)
if let from = String.Index(from16, within: self),
let to = String.Index(to16, within: self) {
return from ..< to
}
return nil
}
}
Swift 3
extension String {
func rangeFromNSRange(nsRange : NSRange) -> Range<String.Index>? {
if let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),
let to16 = utf16.index(from16, offsetBy: nsRange.length, limitedBy: utf16.endIndex),
let from = String.Index(from16, within: self),
let to = String.Index(to16, within: self) {
return from ..< to
}
return nil
}
}
Swift 4
extension String {
func rangeFromNSRange(nsRange : NSRange) -> Range<String.Index>? {
return Range(nsRange, in: self)
}
}
You need to use Range<String.Index> instead of the classic NSRange. The way I do it (maybe there is a better way) is by taking the string's String.Index a moving it with advance.
I don't know what range you are trying to replace, but let's pretend you want to replace the first 2 characters.
var start = textField.text.startIndex // Start at the string's start index
var end = advance(textField.text.startIndex, 2) // Take start index and advance 2 characters forward
var range: Range<String.Index> = Range<String.Index>(start: start,end: end)
textField.text.stringByReplacingCharactersInRange(range, withString: string)
This is similar to Emilie's answer however since you asked specifically how to convert the NSRange to Range<String.Index> you would do something like this:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let start = advance(textField.text.startIndex, range.location)
let end = advance(start, range.length)
let swiftRange = Range<String.Index>(start: start, end: end)
...
}
A riff on the great answer by #Emilie, not a replacement/competing answer.
(Xcode6-Beta5)
var original = "πͺπΈπThis is a test"
var replacement = "!"
var startIndex = advance(original.startIndex, 1) // Start at the second character
var endIndex = advance(startIndex, 2) // point ahead two characters
var range = Range(start:startIndex, end:endIndex)
var final = original.stringByReplacingCharactersInRange(range, withString:replacement)
println("start index: \(startIndex)")
println("end index: \(endIndex)")
println("range: \(range)")
println("original: \(original)")
println("final: \(final)")
Output:
start index: 4
end index: 7
range: 4..<7
original: πͺπΈπThis is a test
final: πͺπΈ!his is a test
Notice the indexes account for multiple code units. The flag (REGIONAL INDICATOR SYMBOL LETTERS ES) is 8 bytes and the (FACE WITH TEARS OF JOY) is 4 bytes. (In this particular case it turns out that the number of bytes is the same for UTF-8, UTF-16 and UTF-32 representations.)
Wrapping it in a func:
func replaceString(#string:String, #with:String, #start:Int, #length:Int) ->String {
var startIndex = advance(original.startIndex, start) // Start at the second character
var endIndex = advance(startIndex, length) // point ahead two characters
var range = Range(start:startIndex, end:endIndex)
var final = original.stringByReplacingCharactersInRange(range, withString: replacement)
return final
}
var newString = replaceString(string:original, with:replacement, start:1, length:2)
println("newString:\(newString)")
Output:
newString: !his is a test
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let strString = ((textField.text)! as NSString).stringByReplacingCharactersInRange(range, withString: string)
}
In Swift 2.0 assuming func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {:
var oldString = textfield.text!
let newRange = oldString.startIndex.advancedBy(range.location)..<oldString.startIndex.advancedBy(range.location + range.length)
let newString = oldString.stringByReplacingCharactersInRange(newRange, withString: string)
Here's my best effort. But this cannot check or detect wrong input argument.
extension String {
/// :r: Must correctly select proper UTF-16 code-unit range. Wrong range will produce wrong result.
public func convertRangeFromNSRange(r:NSRange) -> Range<String.Index> {
let a = (self as NSString).substringToIndex(r.location)
let b = (self as NSString).substringWithRange(r)
let n1 = distance(a.startIndex, a.endIndex)
let n2 = distance(b.startIndex, b.endIndex)
let i1 = advance(startIndex, n1)
let i2 = advance(i1, n2)
return Range<String.Index>(start: i1, end: i2)
}
}
let s = "πͺπΈπ"
println(s[s.convertRangeFromNSRange(NSRange(location: 4, length: 2))]) // Proper range. Produces correct result.
println(s[s.convertRangeFromNSRange(NSRange(location: 0, length: 4))]) // Proper range. Produces correct result.
println(s[s.convertRangeFromNSRange(NSRange(location: 0, length: 2))]) // Improper range. Produces wrong result.
println(s[s.convertRangeFromNSRange(NSRange(location: 0, length: 1))]) // Improper range. Produces wrong result.
Result.
π
πͺπΈ
πͺπΈ
πͺπΈ
Details
NSRange from NSString counts UTF-16 code-units. And Range<String.Index> from Swift String is an opaque relative type which provides only equality and navigation operations. This is intentionally hidden design.
Though the Range<String.Index> seem to be mapped to UTF-16 code-unit offset, that is just an implementation detail, and I couldn't find any mention about any guarantee. That means the implementation details can be changed at any time. Internal representation of Swift String is not pretty defined, and I cannot rely on it.
NSRange values can be directly mapped to String.UTF16View indexes. But there's no method to convert it into String.Index.
Swift String.Index is index to iterate Swift Character which is an Unicode grapheme cluster. Then, you must provide proper NSRange which selects correct grapheme clusters. If you provide wrong range like the above example, it will produce wrong result because proper grapheme cluster range couldn't be figured out.
If there's a guarantee that the String.Index is UTF-16 code-unit offset, then problem becomes simple. But it is unlikely to happen.
Inverse conversion
Anyway the inverse conversion can be done precisely.
extension String {
/// O(1) if `self` is optimised to use UTF-16.
/// O(n) otherwise.
public func convertRangeToNSRange(r:Range<String.Index>) -> NSRange {
let a = substringToIndex(r.startIndex)
let b = substringWithRange(r)
return NSRange(location: a.utf16Count, length: b.utf16Count)
}
}
println(convertRangeToNSRange(s.startIndex..<s.endIndex))
println(convertRangeToNSRange(s.startIndex.successor()..<s.endIndex))
Result.
(0,6)
(4,2)
I've found the cleanest swift2 only solution is to create a category on NSRange:
extension NSRange {
func stringRangeForText(string: String) -> Range<String.Index> {
let start = string.startIndex.advancedBy(self.location)
let end = start.advancedBy(self.length)
return Range<String.Index>(start: start, end: end)
}
}
And then call it from for text field delegate function:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let range = range.stringRangeForText(textField.text)
let output = textField.text.stringByReplacingCharactersInRange(range, withString: string)
// your code goes here....
return true
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let current = textField.text, let r = Range(range, in: current) else {
return false
}
let text = current.replacingCharacters(in: r, with: string)
// ...
return true
}
In the accepted answer I find the optionals cumbersome. This works with Swift 3 and seems to have no problem with emojis.
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
guard let value = textField.text else {return false} // there may be a reason for returning true in this case but I can't think of it
// now value is a String, not an optional String
let valueAfterChange = (value as NSString).replacingCharacters(in: range, with: string)
// valueAfterChange is a String, not an optional String
// now do whatever processing is required
return true // or false, as required
}
extension StringProtocol where Index == String.Index {
func nsRange(of string: String) -> NSRange? {
guard let range = self.range(of: string) else { return nil }
return NSRange(range, in: self)
}
}
Because NSRange, when used in NSString operations, represents positions of the UTF-16 units. Then the shortest way to convert to String.Index is to initialise via String.Index(utf16Offset: Int, in: StringProtocol) initialiser.
let string = "...."
let nsRange = NSRange(....) // This NSRange belongs to `string` variable.
let range = String.Index(utf16Offset: nsRange.lowerBound, in: string)
..< String.Index(utf16Offset: nsRange.upperBound, in: string)
Example:
let string = "a-\u{1112}\u{1161}\u{11AB}-πΆ-\u{E9}\u{20DD}-βΌ-π-(Ψ§ΩΨΉΩΨ§Ψ¬ΩΨ©)-f"
let rangeOfLeftParenthesis = (string as NSString).range(of: "(")
let rangeOfRightParenthesis = (string as NSString).range(of: ")")
print("string: \(string)")
let lowerBound = String.Index.init(utf16Offset: rangeOfLeftParenthesis.upperBound, in: string)
let upperBound = String.Index.init(utf16Offset: rangeOfRightParenthesis.lowerBound, in: string)
let arabicSentenceRange = lowerBound ..< upperBound // Instance of `Range<String.Index>`
print("arabicSentenceRange: \(string[arabicSentenceRange])")
Output:
string: a-ν-πΆ-Γ©β-βΌ-π-(Ψ§ΩΨΉΩΨ§Ψ¬ΩΨ©)-f
arabicSentenceRange: Ψ§ΩΨΉΩΨ§Ψ¬ΩΨ©
The Swift 3.0 beta official documentation has provided its standard solution for this situation under the title String.UTF16View in section UTF16View Elements Match NSString Characters title
Swift 5 Solution
Short answer with main extension
extension NSRange {
public init(range: Range<String.Index>,
originalText: String) {
self.init(location: range.lowerBound.utf16Offset(in: originalText),
length: range.upperBound.utf16Offset(in: originalText) - range.lowerBound.utf16Offset(in: originalText))
}
}
For detailed answer check here
Related
Is there a way to limit a UITextField to only numeric value as well as limiting the length.
I have the below two functions but don't know how I can use the shouldChangeCharactersIn twice in the same delegate. Any ideas how to use both of these
// Allow Numeric Only in Quantity
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let allowCharacters = ".-+1234567890"
let allowedCharacterSet = CharacterSet(charactersIn: allowCharacters)
let typedCharacterSet = CharacterSet(charactersIn: string)
return allowedCharacterSet.isSuperset(of: typedCharacterSet)
}
// Limit the length of the input
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textFieldText = textField.text,
let rangeOfTextToReplace = Range(range, in: textFieldText) else {
return false
}
let substringToReplace = textFieldText[rangeOfTextToReplace]
let count = textFieldText.count - substringToReplace.count + string.count
return count <= 20
}
Many Thanks
You can do it like this
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textFieldText = textField.text,
let rangeOfTextToReplace = Range(range, in: textFieldText) else {
return false
}
let substringToReplace = textFieldText[rangeOfTextToReplace]
let count = textFieldText.count - substringToReplace.count + string.count
let allowedCharacters = ".-+1234567890"
let allowedCharcterSet = CharacterSet(charactersIn: allowedCharacters)
let typedCharcterSet = CharacterSet(charactersIn: string)
if allowedCharcterSet.isSuperset(of: typedCharcterSet)
, count <= 20 {
return true
} else {
return false
}
}
Or you can use this Subclass of UITextField
class JDTextField: UITextField {
#IBInspectable var maxLength: Int = 0 // Max character length
var valueType: ValueType = ValueType.none // Allowed characters
/************* Added new feature ***********************/
// Accept only given character in string, this is case sensitive
#IBInspectable var allowedCharInString: String = ""
func verifyFields(shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
switch valueType {
case .none:
break // Do nothing
case .onlyLetters:
let characterSet = CharacterSet.letters
if string.rangeOfCharacter(from: characterSet.inverted) != nil {
return false
}
case .onlyNumbers:
let numberSet = CharacterSet.decimalDigits
if string.rangeOfCharacter(from: numberSet.inverted) != nil {
return false
}
case .phoneNumber:
let phoneNumberSet = CharacterSet(charactersIn: "+0123456789")
if string.rangeOfCharacter(from: phoneNumberSet.inverted) != nil {
return false
}
case .alphaNumeric:
let alphaNumericSet = CharacterSet.alphanumerics
if string.rangeOfCharacter(from: alphaNumericSet.inverted) != nil {
return false
}
case .fullName:
var characterSet = CharacterSet.letters
print(characterSet)
characterSet = characterSet.union(CharacterSet(charactersIn: " "))
if string.rangeOfCharacter(from: characterSet.inverted) != nil {
return false
}
}
if let text = self.text, let textRange = Range(range, in: text) {
let finalText = text.replacingCharacters(in: textRange, with: string)
if maxLength > 0, maxLength < finalText.utf8.count {
return false
}
}
// Check supported custom characters
if !self.allowedCharInString.isEmpty {
let customSet = CharacterSet(charactersIn: self.allowedCharInString)
if string.rangeOfCharacter(from: customSet.inverted) != nil {
return false
}
}
return true
}
}
How to use it
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Verify all the conditions
if let sdcTextField = textField as? JDTextField {
return sdcTextField.verifyFields(shouldChangeCharactersIn: range, replacementString: string)
} else {
return true
}
}
And in viewDidLoad() you set characteristics in just one line
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.textField.delegate = self
self.textField.maxLength = 10
self.textField.allowedCharInString = ".-+1234567890"
}
I would like to put a limit for textfield. Example: Max character count should be 6 and characters can be digits only. But i could not put these two controls in one function.
First func for count of the text:
func textFieldCharacterCountLimit(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let maxLength = 6
let currentString: NSString = txt_phone_no_verification_code.text! as NSString
let newString: NSString =
currentString.replacingCharacters(in: range, with: string) as NSString
return newString.length <= maxLength
}
Second func for type of the text:
func textFieldCharacterTypeLimit(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let allowedCharacters = CharacterSet.decimalDigits
let characterSet = CharacterSet(charactersIn: string)
return allowedCharacters.isSuperset(of: characterSet)
}
Beside this, it gives an error also. And textFieldCharacterCountLimit function does not work. I think i get an error because two functions effect same textfield with return. Thanks
Allowing only a specified set of characters to be entered into a given text field with specific range
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.count == 0 {
return true
}
let currentText = textField.text ?? ""
let prospectiveText = (currentText as NSString).replacingCharacters(in: range, with: string)
return prospectiveText.containsOnlyCharactersIn(matchCharacters: "0123456789") &&
prospectiveText.count <= 6
}
String extension with condition
extension String {
// Returns true if the string contains only characters found in matchCharacters.
func containsOnlyCharactersIn(matchCharacters: String) -> Bool {
let disallowedCharacterSet = NSCharacterSet(charactersIn: matchCharacters).inverted
return self.rangeOfCharacter(from: disallowedCharacterSet as CharacterSet) == nil
}
}
How to program an iOS text field that takes only numeric input with a maximum length
http://www.globalnerdy.com/2015/04/27/how-to-program-an-ios-text-field-that-takes-only-numeric-input-or-specific-characters-with-a-maximum-length/
You can't just make up delegate method names. You need to implement the proper method and do all of your needed checking in the one method.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
if newString.count > 6 {
return false
}
return newString.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil
}
How to set currency format E.g. 2,000,000.00 in swift on a textfield using swift 3.2 ? I am getting this response when in text field when i am entering amount in textfield
CharacterView(_core: Swift._StringCore(_baseAddress:
Optional(0x00000001c464e38a), _countAndFlags: 13835058055282163718,
_owner: Optional(βΉΒ 75.33)), _coreOffset: 1)
My Code is:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Construct the text that will be in the field if this change is accepted
switch string {
case "0","1","2","3","4","5","6","7","8","9":
currentString += string
formatCurrency(currentString)
default:
if string.characters.count == 0 && currentString.characters.count != 0 {
currentString = String(currentString.characters.dropLast())
formatCurrency(currentString)
}
}
return false }
func formatCurrency(_ string: String) {
print("format \(string)")
print(txtfieldamount.text!)
let formatter = NumberFormatter()
formatter.numberStyle = .currency
//formatter.locale = findLocaleByCurrencyCode("NGN")
let numberFromField = (NSString(string: currentString).doubleValue)/100
let temp = formatter.string(from: NSNumber(value: numberFromField))
self.txtfieldamount.text! = String(describing: temp!.characters.dropFirst())
print(txtfieldamount.text!)
}
I want to get components separated by specific string from an NSAttributedString. Is it possible in swift?
I am able to do this for NSString but I am not sure how can I do same for NSAttributedString?
So to solve issue we need extension for the String that will convert Range to NSRange.
extension String {
func nsRange(fromRange range: Range<Index>) -> NSRange {
let from = range.lowerBound
let to = range.upperBound
let location = characters.distance(from: startIndex, to: from)
let length = characters.distance(from: from, to: to)
return NSRange(location: location, length: length)
}
}
So input data.
//Input array with \n
let attributedString = NSAttributedString(string: "test string1\ntest string2\ntest string3")
//Simle String
let notAttributedString = attributedString.string
//Array of String components separated by \n
let components = notAttributedString.components(separatedBy: "\n")
Than we going to use map and flatMap functions. The main point is usage of the attributedSubstring(from: nsRange) because it will return
NSAttributedString of the our parent attributedString with all effects.
flatMap is used because our map function return NSAttributedString? and we would like to get rid of optionals.
let attributedStringArray = components.map{ item -> NSAttributedString? in
guard let range = notAttributedString.lowercased().range(of:item) else {
return nil
}
let nsRange = notAttributedString.nsRange(fromRange: range)
return attributedString.attributedSubstring(from: nsRange)
}.flatMap{$0}
Output:
[test string1{ }, test string2{ }, test string3{ }]
I created a union between two character sets to be able to use a period with the decimalDigits character set.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let allowed = CharacterSet.decimalDigits
let period = CharacterSet.init(charactersIn: ".")
let both = CFCharacterSetUnion(allowed as! CFMutableCharacterSet, period as CFCharacterSet)
let characterSet = NSCharacterSet(charactersIn: string)
return both.isSuperset(of: characterSet as CharacterSet)
}
however, returning "both.isSuperset(of: characterSet as CharacterSet)". how to correct?
Update 1:
Errors displayed with the current suggested answer
Update 2:
latest update
Try doing this instead:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
var allowed = CharacterSet.decimalDigits
let period = CharacterSet.init(charactersIn: ".")
allowed.formUnion(period)
//UNCOMMENT when isSuperset is working
//let characterSet = CharacterSet(charactersIn: string)
//return allowed.isSuperset(of: characterSet)
// Swift 3 appropriate solution
let isSuperset = string.rangeOfCharacter(from: allowed.inverted) == nil
return isSuperset
}
the basis for which I found here.
Even better, make "allowed" (or "both", whatever you decide to name it) a property that gets created in viewDidLoad so you don't recreate and union the character sets each time a character is typed.