如何在iOS 7下使用蓝牙键盘支持向上和向下箭头键 [英] How can I support the up and down arrow keys with a Bluetooth keyboard under iOS 7

查看:209
本文介绍了如何在iOS 7下使用蓝牙键盘支持向上和向下箭头键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 6下,当使用外部蓝牙键盘和在模拟器中工作时,UITextView会遵守向上和向下箭头键。在iOS 7下,向上和向下箭头键不再执行任何操作,但左右箭头键仍然移动光标。

Under iOS 6, up and down arrow keys were respected by UITextView when using an external Bluetooth keyboard and when working in the simulator. Under iOS 7 the up and down arrow keys no longer do anything, although the left and right arrow keys still move the cursor.

如何支持向上和向下箭头键从iOS 7下的UITextView中的外部键盘?

How do you support up and down arrow keys from an external keyboard in UITextView under iOS 7?

推荐答案

我不知道是否省略了向上和向下箭头键支持在iOS 7中有意为之。这是一种恢复约95%丢失能力的方法。请注意, UITextView 的这个子类还纠正了iOS 7中的一些问题,其中滚动文本视图会导致它疯狂地跳跃。我已经看到它从一个长文件中间跳到文件末尾。

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

这篇关于如何在iOS 7下使用蓝牙键盘支持向上和向下箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆