Mik*_*ike 13 uitextview ios ios7
在iOS 6下,当使用外部蓝牙键盘和在模拟器中工作时,UITextView遵守向上和向下箭头键.在iOS 7下,向上和向下箭头键不再执行任何操作,但左右箭头键仍然移动光标.
在iOS 7下,如何在UITextView中支持来自外部键盘的向上和向下箭头键?
Mik*_*ike 11
我不知道在iOS 7中是否故意省略向上和向下箭头键支持.这是一种恢复大约95%丢失功能的方法.请注意,此子类UITextView还纠正了iOS 7中的一些问题,其中滚动文本视图会导致其疯狂跳跃.我已经看到它从一个长文件中途跳到文件的末尾.
我在这个实现中遇到的唯一问题是没有处理重复键,所以如果按住向上或向下箭头键,移动光标的代码仍然只调用一次.
#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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4031 次 |
| 最近记录: |