iOS7中的UITextView内容大小不同

iOS7中的UITextView内容大小不同,ios,objective-c,cocoa-touch,ios7,uitextview,Ios,Objective C,Cocoa Touch,Ios7,Uitextview,我使用的是一个UITextView,可以通过点击“更多”按钮进行扩展。问题如下: 在iOS6上,我使用这个 self.DescriptionTextView.text = @"loong string"; if(self.DescriptionTextView.contentSize.height>self.DescriptionTextView.frame.size.height) { //set up the more button } 问题在于,在iOS7上,cont

我使用的是一个
UITextView
,可以通过点击“更多”按钮进行扩展。问题如下:

在iOS6上,我使用这个

self.DescriptionTextView.text =  @"loong string";

if(self.DescriptionTextView.contentSize.height>self.DescriptionTextView.frame.size.height) { 
    //set up the more button
}
问题在于,在iOS7上,
contentSize.height
返回的值与在iOS6上返回的值不同(小得多)。 为什么会这样?如何修复它?

这似乎有答案


在使用
contentSize
之前,必须先使用
sizeToFit
,内容大小属性不再像在iOS 6上那样工作。按照其他人的建议使用sizeToFit可能有效,也可能无效,这取决于许多因素

它对我不起作用,所以我用它来代替:

- (CGFloat)measureHeightOfUITextView:(UITextView *)textView
{
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
    {
        // This is the code for iOS 7. contentSize no longer returns the correct value, so
        // we have to calculate it.
        //
        // This is partly borrowed from HPGrowingTextView, but I've replaced the
        // magic fudge factors with the calculated values (having worked out where
        // they came from)

        CGRect frame = textView.bounds;

        // Take account of the padding added around the text.

        UIEdgeInsets textContainerInsets = textView.textContainerInset;
        UIEdgeInsets contentInsets = textView.contentInset;

        CGFloat leftRightPadding = textContainerInsets.left + textContainerInsets.right + textView.textContainer.lineFragmentPadding * 2 + contentInsets.left + contentInsets.right;
        CGFloat topBottomPadding = textContainerInsets.top + textContainerInsets.bottom + contentInsets.top + contentInsets.bottom;

        frame.size.width -= leftRightPadding;
        frame.size.height -= topBottomPadding;

        NSString *textToMeasure = textView.text;
        if ([textToMeasure hasSuffix:@"\n"])
        {
            textToMeasure = [NSString stringWithFormat:@"%@-", textView.text];
        }

        // NSString class method: boundingRectWithSize:options:attributes:context is
        // available only on ios7.0 sdk.

        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];

        NSDictionary *attributes = @{ NSFontAttributeName: textView.font, NSParagraphStyleAttributeName : paragraphStyle };

        CGRect size = [textToMeasure boundingRectWithSize:CGSizeMake(CGRectGetWidth(frame), MAXFLOAT)
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:attributes
                                                  context:nil];

        CGFloat measuredHeight = ceilf(CGRectGetHeight(size) + topBottomPadding);
        return measuredHeight;
    }
    else
    {
        return textView.contentSize.height;
    }
}

请尝试以下链接中的答案,
contentSize
之前应调用
layoutifneedd

答案是:

在iOS7中,
UITextView
使用
NSLayoutManager
来布局文本:

// If YES, then the layout manager may perform glyph generation and layout for a given portion of the text, without having glyphs or layout for preceding portions.  The default is NO.  Turning this setting on will significantly alter which portions of the text will have glyph generation or layout performed when a given generation-causing method is invoked.  It also gives significant performance benefits, especially for large documents.
@property(NS_NONATOMIC_IOSONLY) BOOL allowsNonContiguousLayout;
禁用
AllowsUncontiguousLayout
以修复
contentSize

textView.layoutManager.allowsNonContiguousLayout = NO;

尝试以下代码,它将在iOS6和ios7中工作,请尝试

CGSize myTextViewSize = [self.myTextView sizeThatFits:CGSizeMake(self.myTextView.frame.size.width, FLT_MAX)];
self.myTextView.height = myTextViewSize.height;
NSLog(@"%f", self.myTextView.height);

谢谢,这个看起来很好吃。你能帮我回答另一个类似的问题吗?下一步检查iOS版本很容易:if(floor(NSFoundationVersionNumber)>NSFoundationVersionNumber\u iOS\u 6\u 1)是迄今为止我看到的解决iOS 7导致的contentSize.height问题的最佳修复方法。我唯一想提及的是,最好检查系统版本,以防SnapshotViewAfterScreenuUpdate发生变化。我已更新代码,对照系统版本检查iOS 7。好的,下面是我的建议:iOS 7.1中已修复(好消息!)。对于iOS 7.0.x,按照此处所述计算contentSize(或对于非属性字符串,如以下所示:CGFloat textViewheight=[self.textView SizeHattfits:CGSizeMake(self.textView.frame.size.width,FLT_MAX)]。高度;然后将textView的高度约束的常数设置为该高度。(在自动布局方案中,您很可能不应该与框架鬼混)根据文档,AllowsUncontiguousLayout默认设置为false。当它设置为true时,是否存在某些情况,我们必须手动重置它?@Lope我认为对于您自己创建的布局管理器,默认情况下可能为false,但对于textview创建的布局管理器,默认情况下为true。将其设置回false可以解决我的问题布局问题。谢谢,@nova!链接上说sizeToFit以前在iOS 6中工作,但现在在iOS 7中有一些额外的解决方法。