在iOS 6下,当使用外部蓝牙键盘和在模拟器中工作时,UITextView会遵守向上和向下箭头键。在iOS 7下,向上和向下箭头键不再执行任何操作,但左右箭头键仍然移动光标。
如何在iOS 7下的UITextView中支持来自外部键盘的向上和向下箭头键?
答案 0 :(得分:11)
我不知道在iOS 7中是否故意省略向上和向下箭头键支持。这是一种恢复大约95%丢失功能的方法。请注意,此UITextView
的子类还纠正了iOS 7中的一些问题,其中滚动文本视图会导致其疯狂跳跃。我看到它从中间跳过一个长文件到文件的末尾。
我在这个实现中发现的唯一问题是没有处理重复键os,所以如果按住向上或向下箭头键,移动光标的代码仍然只调用一次。
#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
答案 1 :(得分:4)
这对我有用,除了这部分在downArrow:
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;
}
我必须删除if
行:
CGRect caret = [self firstRectForRange: range];
// 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;
它就像一个魅力!
答案 2 :(得分:0)
似乎问题已在iOS 7.0.3中修复,箭头键按预期移动光标。