I found a strange behavior for function String.characters.count for rows in which row are the Emoji flags:
import UIKit
var flag = ""
print(flag.characters.count)
print(flag.unicodeScalars.count)
print(flag.utf16.count)
print(flag.utf8.count)
flag = "000"
print(flag.characters.count)
print(flag.unicodeScalars.count)
print(flag.utf16.count)
print(flag.utf8.count)
I want to limit the string length of the text when writing and editing in the UITextView. Actually my code this:
var lastRange: NSRange? = nil
var lastText: String? = nil
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText string: String) -> Bool {
if string == "\n" {
// Execute same code
return false
}
var text = string.uppercaseString
if lastText != text || lastRange != nil && (lastRange!.location != range.location || lastRange!.length != range.length) {
lastRange = range
lastText = text
var text = (self.textView.text ?? "" as NSString).stringByReplacingCharactersInRange(range, withString: string)
// Delete chars if length more kMaxLengthText
while text.utf16.count >= kMaxLengthText {
text.removeAtIndex(text.endIndex.advancedBy(-1))
}
// Set position after insert text
self.textView.selectedRange = NSRange(location: range.location + lastText!.utf16.count, length: 0)
}
return false
}