I have no idea if the omission of up and down arrow key support is intentional in iOS 7. Here's a way to restore about 95% of the lost capability. Note that this subclass of UITextView
also corrects a few issues in iOS 7 where scrolling a text view causes it to jump wildly. I've seen it jump from midway through a long file to the end of the file.
The only problem I have found with this implementation is that the repeat key os not handled, so if you hold the up or down arrow keys down, the code to move the cursor is still only called one time.
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
@implementation ArrowKeyTextView
- (id) initWithFrame: (CGRect) frame {
self = [super initWithFrame:frame];
if (self) {
// This has nothing to do with support for arrow keys, but it does fix a number of issues with scrolling in iOS 7.
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
self.layoutManager.allowsNonContiguousLayout = NO;
}
return self;
}
- (NSArray *) keyCommands {
UIKeyCommand *upArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputUpArrow modifierFlags: 0 action: @selector(upArrow:)];
UIKeyCommand *downArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputDownArrow modifierFlags: 0 action: @selector(downArrow:)];
return [[NSArray alloc] initWithObjects: upArrow, downArrow, nil];
}
- (void) upArrow: (UIKeyCommand *) keyCommand {
UITextRange *range = self.selectedTextRange;
if (range != nil) {
float lineHeight = self.font.lineHeight;
CGRect caret = [self firstRectForRange: range];
if (isinf(caret.origin.y)) {
// Work-around for a bug in iOS 7 that returns bogus values when the caret is at the start of a line.
range = [self textRangeFromPosition: range.start toPosition: [self positionFromPosition: range.start offset: 1]];
caret = [self firstRectForRange: range];
caret.origin.y = caret.origin.y + lineHeight;
}
caret.origin.y = caret.origin.y - lineHeight < 0 ? 0 : caret.origin.y - lineHeight;
caret.size.width = 1;
UITextPosition *position = [self closestPositionToPoint: caret.origin];
self.selectedTextRange = [self textRangeFromPosition: position toPosition: position];
caret = [self firstRectForRange: self.selectedTextRange];
if (isinf(caret.origin.y)) {
// Work-around for a bug in iOS 7 that occurs when the range is set to a position past the end of the last character
// on a line.
NSRange range = {0, 0};
range.location = [self offsetFromPosition: self.beginningOfDocument toPosition: position];
self.selectedRange = range;
}
}
}
- (void) downArrow: (UIKeyCommand *) keyCommand {
UITextRange *range = self.selectedTextRange;
if (range != nil) {
float lineHeight = self.font.lineHeight;
CGRect caret = [self firstRectForRange: range];
if (isinf(caret.origin.y)) {
// Work-around for a bug in iOS 7 that returns bogus values when the caret is at the start of a line.
range = [self textRangeFromPosition: range.start toPosition: [self positionFromPosition: range.start offset: 1]];
caret = [self firstRectForRange: range];
caret.origin.y = caret.origin.y + lineHeight;
}
caret.origin.y = caret.origin.y + lineHeight < 0 ? 0 : caret.origin.y + lineHeight;
caret.size.width = 1;
UITextPosition *position = [self closestPositionToPoint: caret.origin];
self.selectedTextRange = [self textRangeFromPosition: position toPosition: position];
caret = [self firstRectForRange: self.selectedTextRange];
if (isinf(caret.origin.y)) {
// Work-around for a bug in iOS 7 that occurs when the range is set to a position past the end of the last character
// on a line.
NSRange range = {0, 0};
range.location = [self offsetFromPosition: self.beginningOfDocument toPosition: position];
self.selectedRange = range;
}
}
}
@end