何が起きたか
UITextField に AlignmentRight 属性を指定した場合、テキストの末尾にスペースを入力しても入力されていないように見える。
データ上はスペースが入力されているので、続けてなにか文字を入力すると、スペースと一緒に表示される。
stackoverflow の質問もあって、どうやら iOS 7 からの現象ぽい
https://stackoverflow.com/questions/19569688/right-aligned-uitextfield-spacebar-does-not-advance-cursor-in-ios-7
どうしたか
UITextField を編集したときに呼ばれる textField(_:shouldChangeCharactersIn:replacementString:)
に修正を加える
もしスペースが入力されたら、 non-breaking space に置き換えて表示してやる
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let text = textField.text!
if range.location == text.characters.count && string == " " {
textField.text = text + "\u{00a0}"
return false
}
return true
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (range.location == textField.text.length && [string isEqualToString:@" "]) {
textField.text = [textField.text stringByAppendingString:@"\u00a0"];
return NO;
}
return YES;
}
これでよさそう
サンプルコードはこちらhttps://gist.github.com/tdrk18/bdbaabd4ba898dcbdb0d8e5c62fbabad
🐢
NSTextAlignment.left
とかにすればいいんですけど、 いろいろと制約があって NSTextAlignment.right
のままで回避したい場合に。