在 iOS 6 下,当使用外部蓝牙键盘和在模拟器中工作时,UITextView 尊重向上和向下箭头键。在 iOS 7 下,上下箭头键不再执行任何操作,但左右箭头键仍可移动光标。
如何在 iOS 7 下的 UITextView 中支持外接键盘的向上和向下箭头键?
最佳答案
我不知道在 iOS 7 中是否有意省略向上和向下箭头键支持。这里有一种方法可以恢复大约 95% 的丢失功能。请注意,UITextView
的这个子类还纠正了 iOS 7 中的一些问题,即滚动 TextView 会导致它疯狂跳动。我见过它从一个长文件的中途跳到文件末尾。
我发现这个实现的唯一问题是没有处理重复键,所以如果你按住向上或向下箭头键,移动光标的代码仍然只被调用一次。
#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
关于ios - iOS 7下蓝牙键盘如何支持上下方向键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19235762/