Ios UITextView外部的光标

Ios UITextView外部的光标,ios,uitextview,ios7,uiedgeinsets,Ios,Uitextview,Ios7,Uiedgeinsets,我到处找,似乎找不到答案。我见过的最接近的东西是: 对不起,没有截图,因为我(几乎)没有声誉,但它看起来类似于链接的SO帖子 当我在iOS6上运行应用程序时,一切正常(内容会随着光标滚动以保持在屏幕上),但在iOS7上,光标会超出UITextView的末尾一行。我尝试添加uiedgeinset来移动内容,但当用户主动输入文本时,它只会不断添加新行,直到光标位于文本视图的末尾下方 我的布局由一个标签(headerText)和一个UITextView(textView)组成。此视图通过选项卡栏显示

我到处找,似乎找不到答案。我见过的最接近的东西是:

对不起,没有截图,因为我(几乎)没有声誉,但它看起来类似于链接的SO帖子

当我在iOS6上运行应用程序时,一切正常(内容会随着光标滚动以保持在屏幕上),但在iOS7上,光标会超出UITextView的末尾一行。我尝试添加uiedgeinset来移动内容,但当用户主动输入文本时,它只会不断添加新行,直到光标位于文本视图的末尾下方

我的布局由一个标签(headerText)和一个UITextView(textView)组成。此视图通过选项卡栏显示。添加了一个键盘输入附件,但在调用该函数之前,它的高度会自动计算到键盘高度中

以下是我用来调整视图大小的函数,从键盘调用“显示/隐藏代理”、“旋转”、“初始布局”等:

-(void)resizeViewsWithKbdHeight:(float)kbHeight
{
    //set the header label frame

    //set constraints
    //total view width - 30 (15 each for left and right padding)
    CGFloat labelWidth = ([[[self navigationController] view] bounds].size.width - 30);
    CGSize constraintSize = {labelWidth, CGFLOAT_MAX};
    //calculate needed height for header label
    CGSize textSize = [[self headerText] sizeWithFont:[UIFont systemFontOfSize:17.0f]
                                    constrainedToSize:constraintSize
                                        lineBreakMode:NSLineBreakByWordWrapping];
    //build and set frame for header label:
    //make it the same as the old
    CGRect headerTempSize = [[self headerLabel] frame];
    //except for the height
    headerTempSize.size.height = textSize.height;
    //and set it
    [[self headerLabel] setFrame:headerTempSize];

    //correct the placement of the UITextView, so it's under the header label

    //build a new frame based on current textview frame
    CGRect newFrame = [[self textView] frame];

    //get the y position of the uitextview, the +8 is the padding between header and uitextview
    CGFloat vertPadding = [[self headerLabel] frame].origin.y + [[self headerLabel] frame].size.height + 8;

    //bump it down vertically
    newFrame.origin.y = vertPadding;

    //bump things down by the amount of the navigation bar and status bar
    float offscreenBump = [[[self navigationController] navigationBar] frame].origin.y + [[[self navigationController] navigationBar] frame].size.height;

    //if we aren't showing the keyboard, add the height of the tab bar
    if(kbHeight == 0) {
        offscreenBump += [[[self tabBarController] tabBar] frame].size.height;
    }

    //calculate the new height of the textview, the +9 is for padding below the text view
    CGFloat newHeight = [[[self navigationController] view] bounds].size.height - ([[self textView] frame].origin.y + 9 + kbHeight + offscreenBump);

    //resize the height as calculated
    newFrame.size.height = newHeight;

    //set textview frame to this new frame
    [[self textView] setFrame:newFrame];
}
我试图支持iOS5,所以没有自动布局

有可能我对自己做事的方式太天真了


提前谢谢

这似乎是iOS 7中的一个bug。我发现纠正此问题的唯一方法是为UITextView添加一个委托,并实现
textViewDidChangeSelection
,重置视图以如下方式显示选择:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

- (void) textViewDidChangeSelection: (UITextView *) tView {
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        [tView scrollRangeToVisible:[tView selectedRange]];
    }
}

我找到了一种更为骇客但更为有效的方法来处理这个问题:

- (void)textViewDidChangeSelection:(UITextView *)textView
{
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
        if ([textView.text characterAtIndex:textView.text.length-1] != ' ') {
            textView.text = [textView.text stringByAppendingString:@" "];
        }

        NSRange range0 = textView.selectedRange;
        NSRange range = range0;
        if (range0.location == textView.text.length) {
            range = NSMakeRange(range0.location - 1, range0.length);
        } else if (range0.length > 0 &&
                   range0.location + range0.length == textView.text.length) {
            range = NSMakeRange(range0.location, range0.length - 1);
        }
        if (!NSEqualRanges(range, range0)) {
            textView.selectedRange = range;
        }
    }
}
基本上,我确保文本字段中始终有一个尾随空格。然后,如果用户试图更改选择以显示空间,请更改选择。通过始终在光标前保留一个空格,字段按预期滚动


最后,如果需要,请在将文本复制到模型(未显示)时删除尾随空格。

Ya当键盘调用时,调整视图大小是一件痛苦的事情。。但是我在github中找到了一个名为
EKKeyboardAvoiding
的开源软件。如果你想试试,那就好了。当键盘调用时,它将根据需要调整scrollview的大小。你可以将其用于任何scrollview子类。谢谢Mike,这似乎很有效,尽管这并不理想,因为他们必须在不可见的行上键入以使其滚动(除非我遗漏了什么)。他们确实必须以某种方式将光标移到那里,因为这是根据光标位置进行滚动的。如果在文本末尾添加换行符,则此操作无效。这就是你所遇到的吗?是的,现在的行为是(根据你的修正),当光标到达终点时,它会在文本视图下方一行,然后一旦他们在看不见的行上输入文本,它就会向上滚动一行。