7

我搜索了高低,似乎找不到答案。我见过的最接近的是: UITextView cursor below frame when changed frame

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

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

我的布局由一个标签(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,所以没有 AutoLayout。

有可能我对自己做事的方式非常天真。

提前致谢!

4

2 回答 2

7

这似乎是 iOS 7 中的一个错误。我发现解决此问题的唯一方法是为 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]];
    }
}
于 2013-10-08T17:25:12.990 回答
6

我找到了一种更有效但更有效的方法来解决这个问题:

- (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;
        }
    }
}

基本上,我确保文本字段中始终有一个尾随空格。然后,如果用户尝试更改选择以显示空间,请更改选择。通过始终在光标前面保留一个空格,该字段可以按预期滚动。

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

于 2013-11-13T00:07:53.890 回答