I'm using Swift 2.0/Xcode 7.0 for an iOS app. I've built a Japanese IME to convert Roman characters to their Japanese equivalent. For example:
Typing "a" will convert the "a" to "あ"
Typing a "k" will do nothing (no match in Japanese) until the next character is typed, for example typing a "k" then an "a" will result in "か"
My problem is when I try to delete a Japanese character. If there is only one character in the text field, the delete key/function works as expected. However, if there is more than one, when I try to delete the character, I receive a can not increment endIndex error in the following code.
var imeInputLength: Int = 0
let currentInputValue: String = txtfldYourResponse.text!.lowercaseString
if(currentInputValue.characters.count==0) {
imeInputLength = 0
}
let inputStringToKeep: String = currentInputValue.substringWithRange(
Range<String.Index>(start: currentInputValue.startIndex.advancedBy(imeInputLength),
end: currentInputValue.endIndex))
let imeStringToKeep: String = currentInputValue.substringWithRange(
Range<String.Index>(start: currentInputValue.startIndex,
end: currentInputValue.startIndex.advancedBy(imeInputLength)))
if let imeValueDC = JIMEDC[inputStringToKeep] {
txtfldYourResponse.text = imeStringToKeep + imeValueDC
imeInputLength = (txtfldYourResponse.text?.characters.count)!-1
}
if let imeValue = JIME[inputStringToKeep] {
txtfldYourResponse.text = imeStringToKeep + imeValue
imeInputLength = txtfldYourResponse.text!.characters.count
}
currentInputValue is the text from the text field.
imeInputLength is an int (initial value = 0) that increments by the total character count in the text field AFTER a match is found.
JIMEDC and JIME are key/value pairs that handle the conversion from Roman to Japanese characters.
I'm printing the endIndex to the console before the code runs. It appears to increment/decrement as expected, but the code block above fails with the increment error.
I've been banging my head against this for a couple weeks now with no progress.
Edit: Clarified handling of imeInputLength and added additional code for clarification.
A side conversation with l'L'l got me on the right track. I was confused by the error message can not increment endIndex. In reality, I was creating an out-of-bounds error on my range.
When the backspace key was pressed, I wasn't modifying imeInputLength to reflect the new length of the input string. As a result, my advanceByamount was larger than the updated endIndex value. Swift threw this as can not increment endIndex but the reason for it was the startIndex was now greater than the endIndex.
I resolved this by first checking for the following:
if(imeInputLength > currentInputValue.characters.count) {
print("You must have pressed backspace")
imeInputLength = txtfldYourResponse.text!.characters.count
} else {
let inputStringToKeep:
...
}
If the backspace key IS pressed, I needed to see if the imeInputLength value was greater than the current character count of the input field. The only use case where imeInputLength ends up greater is when a Japanese character is deleted using the backspace key. When this condition is caught, I reset imeInputLength to the current character count and exit the routine.
I'm happy to say that all of my use cases now test out positive!
Related
I am trying to retrieve the deleted character from UITextView when user presses delete button on keypad.
When I enter some text in textview for eg:(abc), I am able to retrieve the deleted character by checking the range and location. So no problem for english text
The challenge starts when I have a non english text, for eg "Hindi" language.
Lets say i entered "परक" in textview, and then if I delete one character at a time from right to left, I am able to identify the deleted character as क, र, प respectively.
The real problem start when I have Hindi vowels in the string
For eg: If i have a text as "परी". Then according to Hindi text it contains three characters as प, र, ी. Here ी is a long vowel in Hindi language
When i enter ी in textview, the delegate method is able to identify this text.
However when i press delete button on keyboard to delete ी from word "परी", the range calculation to determine which word is deleted seems to go wrong.
I get range value to delete ी as
Range(3..<3)
- startIndex : 3
- endIndex : 3
Since startIndex and endIndex are same, I get an empty string.
Following is the code I am using to enter or delete a character in textview
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if ( range.location == 0 && text == " " )
{
return false
}
// Its backspace
if text == ""
{
// Set startIndex for Range
let startIndex = textView.text.startIndex.advancedBy(range.location, limit: textView.text.endIndex)
// Set new Range
let newRange = startIndex..<textView.text.startIndex.advancedBy(range.location + range.length, limit: textView.text.endIndex)
// Get substring
// Here I get the character that is about to be deleted
let substring = textView.text[newRange]
print(substring) //Gives empty string when i delete ी
return true
}
print(text) // This prints ी when i enter it in textview after पर
return true
}
It would be great if anyone can help me with this. I have checked with Android and it is able to identify ी, when deleted.
Awaiting for replies curiously....
Note: Code is written in Swift 2.3
When I removed the last character of "परी" in a UITextView, I got NSRange(location: 2, length: 1) in the delegate method. It's not empty, as you see, its length is not 0.
The problem is that your code of retrieving the substring is completely wrong.
When an iOS API returns an NSRange for string related operations, its location and length are based on UTF-16, which is the basis of NSString. You should not use those values for characters (String.CharacterView) based index calculation.
Replace these lines of your code:
// Set startIndex for Range
let startIndex = textView.text.startIndex.advancedBy(range.location, limit: textView.text.endIndex)
// Set new Range
let newRange = startIndex..<textView.text.startIndex.advancedBy(range.location + range.length, limit: textView.text.endIndex)
// Get substring
// Here I get the character that is about to be deleted
let substring = textView.text[newRange]
To:
//### Get substring
let substring = (textView.text as NSString).substringWithRange(range)
Your code seems to be written in Swift 2, but in Swift 3, it becomes:
//### Get substring
let substring = (textView.text as NSString).substring(with: range)
I'm replacing the selected text in a textView with the new one. To accomplish this, I'm using this code based on this answer of beyowulf. All works well, the replaced text becomes selected, the problem arises when in the text there is one ore more special characters (like emoji etc). In this case the selected text misses one ore more characters at the end of the selection.
mainTextField.replaceRange((theRange), withText: newStr) // replace old text with the new one
selectNewText(theRange, newStr: newStr) // select the new text
func selectNewText(theRange: UITextRange, newStr: String) {
let newStrLength = newStr.characters.count // let's see how long is the string
mainTextField.selectedTextRange = mainTextField.textRangeFromPosition(theRange.start, toPosition: mainTextField.positionFromPosition(theRange.start, offset: newStrLength)!)
mainTextField.becomeFirstResponder()
}
OK, after I read the answers and comments to this question, I fixed this problem by replacing this statement (which returns the "human-perceptible" number of characters):
let newStrLength = newStr.characters.count
With this one:
let newStrLength = newStr.utf16.count
PS
By the way, here is some test I done with different implementations:
let str = "Abc😬"
let count = str.characters.count
print(count) // 4
let count2 = str.utf16.count
print(count2) // 5
let count3 = str.utf8.count
print(count3) // 7
I entered a two emojis in textfield 👨👨👧👧😍, here I'm getting total number of 5 characters length whereas 4 characters for first emoji and 1 character for second. Looks like apple has combined 4 emojis to form a one.
I'm looking for the swift code where I can separate each of emojis separately, suppose by taking the above example I should be getting 2 strings/character separately for each emoji.
Can any one help me to solve this, I've tried many things like regex separation or componentsSeparatedByString or characterSet. but unfortunately ended up with negative.
Thanks in advance.
Update for Swift 4 (Xcode 9)
As of Swift 4 (tested with Xcode 9 beta) a "Emoji ZWJ Sequence" is
treated as a single Character as mandated by the Unicode 9 standard:
let str = "👨👨👧👧😍"
print(str.count) // 2
print(Array(str)) // ["👨👨👧👧", "😍"]
Also String is a collection of its characters (again), so we can
call str.count to get the length, and Array(str) to get all
characters as an array.
(Old answer for Swift 3 and earlier)
This is only a partial answer which may help in this particular case.
"👨👨👧👧" is indeed a combination of four separate characters:
let str = "👨👨👧👧😍" //
print(Array(str.characters))
// Output: ["👨", "👨", "👧", "👧", "😍"]
which are glued together with U+200D (ZERO WIDTH JOINER):
for c in str.unicodeScalars {
print(String(c.value, radix: 16))
}
/* Output:
1f468
200d
1f468
200d
1f467
200d
1f467
1f60d
*/
Enumerating the string with the .ByComposedCharacterSequences
options combines these characters correctly:
var chars : [String] = []
str.enumerateSubstringsInRange(str.characters.indices, options: .ByComposedCharacterSequences) {
(substring, _, _, _) -> () in
chars.append(substring!)
}
print(chars)
// Output: ["👨👨👧👧", "😍"]
But there are other cases where this does not work,
e.g. the "flags" which are a sequence of "Regional Indicator
characters" (compare Swift countElements() return incorrect value when count flag emoji). With
let str = "🇩🇪"
the result of the above loop is
["🇩", "🇪"]
which is not the desired result.
The full rules are defined in "3 Grapheme Cluster Boundaries"
in the "Standard Annex #29 UNICODE TEXT SEGMENTATION" in the
Unicode standard.
You can use this code example or this pod.
To use it in Swift, import the category into the YourProject_Bridging_Header
#import "NSString+EMOEmoji.h"
Then you can check the range for every emoji in your String:
let example: NSString = "👨👨👧👧😍" // your string
let ranges: NSArray = example.emo_emojiRanges() // ranges of the emojis
for value in ranges {
let range:NSRange = (value as! NSValue).rangeValue
print(example.substringWithRange(range))
}
// Output: ["👨👨👧👧", "😍"]
I created an small example project with the code above.
For further reading, this interesting article from Instagram.
In a UITableView, I'm listing a bunch of languages to be selected. And to put a section index view to the right like in Contacts app, I'm getting all first letters of languages in the list and then use it to generate the section index view.
It works almost perfect, Just I encountered with a problem in getting first letter of some strings in Hebrew. Here a screenshot from playground, one of the language name that I couldn't get the first letter:
Problem is, the first letter of the name of the language that has "ina" language code, isn't "א", it's an empty character; it's not a space, it's just an empty character. As you can see, it's actually 12 characters in total, but when I get count of it, it says 13 characters because there is an non-space empty character in index 0.
It works perfectly if I use "eng" or "ara" languages with putting these values in value: parameter. So maybe the problem is cause of system that returns a language name with an empty character in some cases, I don't know.
I tried some different methods of getting first letter, but any of it didn't work.
Here "א" isn't the first letter, it's the second letter. So I thought maybe I can find a simple hack with that, but I want to try solving it before trying workarounds.
Here is the code:
let locale = NSLocale(localeIdentifier: "he")
let languageName = locale.displayNameForKey(NSLocaleIdentifier, value: "ina")!
let firstLetter = first(languageName)!
println(countElements(languageName))
for character in languageName {
println(character)
}
You could use an NSCharacterSet.controlCharacterSet() to test each character. I can't figure out how to stay in Swift-native strings, but here's a function that uses NSString to return the first non-control character:
func firstNonControlCharacter(str: NSString) -> String? {
let controlChars = NSCharacterSet.controlCharacterSet()
for i in 0..<str.length {
if !controlChars.characterIsMember(str.characterAtIndex(i)) {
return str.substringWithRange(NSRange(location: i, length: 1))
}
}
return nil
}
let locale = NSLocale(localeIdentifier: "he")
let languageName = locale.displayNameForKey(NSLocaleIdentifier, value: "ina")!
let firstChar = firstNonControlCharacter(languageName) // Optional("א")
I have phone number in below format
1-1xx-2xx-9565
Currently VO read it as "One (pause) One x x (pause) two x x (pause) minus nine thousand five hundred sixty five".
VO should read it as "One (pause) One x x (pause) two x x (pause) nine five six five".
What could be the problem? Is this wrong phone format?
Let's break down what is happening. VoiceOver doesn't know that the text you are presenting is a phone number and treats it like a sentence of text. In that text it tries to find distinct components and read them appropriately. For example, the text "buy 60 cantaloupes" has 3 components, "buy", "60", and "cantaloupes". The first is text and is read as text, the second is purely numerical and is best read out as "sixty", and the third is read as text.
Applying the same logic to your phone number.
(I'm not talking about actual implementation, just reasoning.)
If you read 1-1xx-2xx-9565 from the left to the right then the first distinct component is "1" which in it self is numerical and is read as "1". If the phone number would have started with "12-1xx" then the first component would have been read as "twelve" because its purely numerical.
The next component is "1xx" or "-1xx" depending on how you look at it. In either case it is a combination of numbers and letters, e.g. it is not purely numerical and is thus read out as text. If you include the "-" in that component is interpreted as a hyphen which isn't read out. That is why the the "-" is never read out for that component. The next component ("-2xx") is treated in the same way.
The final component is "-9565" which turns out to be a valid number. As seen in the cantaloupe sentence, VoiceOver reads this as a number in which case the "-" is no longer interpreted as a hyphen but as a "minus sign".
Getting VoiceOver to read your own text
On any label, view or other element in your application that is used with Voice Over, you can supply your own "accessibility label" when you know more about how you want the text to be read. This is done by simply assigning your own string to the accessibilityLabel property.
Now, you can create a suitable string in many different ways, a very simple one in your case would be to just add spaces everywhere so that each number is read individually. However, it seems a bit fragile to me, so I went ahead and used a number formatter to translate the individual numbers to their textual representations.
NSString *phoneNumber = #"1-1xx-2xx-9565";
// we want to know if a character is a number or not
NSCharacterSet *numberCharacters = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
// we use this formatter to spell out individual numbers
NSNumberFormatter *spellOutSingleNumber = [NSNumberFormatter new];
spellOutSingleNumber.numberStyle = NSNumberFormatterSpellOutStyle;
NSMutableArray *spelledOutComonents = [NSMutableArray array];
// loop over the phone number add add the accessible variants to the array
[phoneNumber enumerateSubstringsInRange:NSMakeRange(0, phoneNumber.length)
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
// check if it's a number
if ([substring rangeOfCharacterFromSet:numberCharacters].location != NSNotFound) {
// is a number
NSNumber *number = #([substring integerValue]);
[spelledOutComonents addObject:[spellOutSingleNumber stringFromNumber:number]];
} else {
// is not a number
[spelledOutComonents addObject:substring];
}
}];
// finally separate the components with spaces (so that the string doesn't become "ninefivesixfive".
NSString *yourAccessiblePhoneNumber = [spelledOutComonents componentsJoinedByString:#" "];
The result when I ran this was
one - one x x - two x x - nine five six five
If you need to do other modifications to your phone numbers to get them to read appropriately then you can do that. I suspect that you will use this is more than one place in your app so creating a custom NSFormatter might be a good idea.
Edit
On iOS 7 you can also use the UIAccessibilitySpeechAttributePunctuation attribute on an attributes string to change how it is pronounced.
Speech Attributes for Attributed Strings
Attributes that you can apply to text in an attributed string to modify how that text is pronounced.
UIAccessibilitySpeechAttributePunctuation
The value of this key is an NSNumber object that you should interpret as a Boolean value. When the value is YES, all punctuation in the text is spoken. You might use this for code or other text where the punctuation is relevant.
Available in iOS 7.0 and later.
Declared in UIAccessibilityConstants.h.
As of iOS 13 you can use a - NSAttributedString.Key.accessibilitySpeechSpellOut as a accessibilityAttributedLabel to make VoiceOver read each letter of the provided string (or a range of string).
So for example:
yourView.accessibilityAttributedLabel = NSAttributedString(string: yourText, attributes: [.accessibilitySpeechSpellOut: true])
If you want to spell all characters individually, a simple solution is to separate the characters by a comma ",".
You can use a String extension to convert the string:
extension String
{
/// Returns string suitable for accessibility (voice over). All characters will be spelled individually.
func stringForSpelling() -> String
{
return stringBySeparatingCharactersWithString(",")
}
/// Inserts a separator between all characters
func stringBySeparatingCharactersWithString(separator: String) -> String
{
var s = ""
// Separate all characters
let chars = self.characters.map({ String($0) })
// Append all characters one by one
for char in chars {
// If there is already a character, append separator before appending next character
if s.characters.count > 0 {
s += separator
}
// Append next character
s += char
}
return s
}
}
And then use it in the code like this:
myLabel.accessibilityLabel = myString.stringForSpelling()
Just Add a comma to each digit of the last number and after the last digit as well,. this will make sure voice over reads the last number as same as previous numbers.
example your number :- 1-1xx-2xx-9565
accessibility label :- 1-1xx-2xx-9,5,6,5,
Here is the code in Swift
public func retrieveAccessiblePhoneNumber(phoneNumber: String) -> String {
// We want to know if a character is a number or not
let characterSet = NSCharacterSet(charactersInString: "0123456789")
// We use this formatter to spell out individual numbers
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .SpellOutStyle
var spelledOutComponents = [String]()
let range = Range<String.Index>(start: phoneNumber.startIndex, end: phoneNumber.endIndex)
// Loop over the phone number add add the accessible variants to the array
phoneNumber.enumerateSubstringsInRange(range,
options: NSStringEnumerationOptions.ByComposedCharacterSequences) { (substring, substringRange, enclosingRange, stop) -> () in
// Check if it's a number
if let substr = substring where substr.rangeOfCharacterFromSet(characterSet) != nil {
if let number = Int(substr) {
// Is a number
let nsNumber = NSNumber(integer: number)
spelledOutComponents.append(numberFormatter.stringFromNumber(nsNumber)!)
}
} else {
// Is not a number
spelledOutComponents.append(substring!)
}
}
// Finally separate the components with spaces (so that the string doesn't become "ninefivesixfive".
return spelledOutComponents.joinWithSeparator(" ")
}