UITextView contentSize changes and NSLayoutManager in iOS7

后端 未结 3 1032
不思量自难忘°
不思量自难忘° 2021-02-06 04:20

The problem: UITextView silently changes it\'s contentSize in some situations.

The simplest case textView with large text and keyboard. Just ad

3条回答
  •  后悔当初
    2021-02-06 04:50

    I met a similar situation as urs. Mine shows with a different bug but due to the same reason: the contentSize property is silently changed by iOS7 incorrectly. Here is how I work around it. Its kinda a ugly fix. Whenever I need to use textView.contentSize, I calculate it by myself.

    -(CGSize)sizeOfText:(NSString *)textToMesure widthOfTextView:(CGFloat)width withFont:(UIFont*)font
    {
        CGSize size = [textToMesure sizeWithFont:font constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
        return size;
    }
    

    then you can just call this function to get the size:

    CGSize cont_size =   [self sizeOfText:self.text widthOfTextView:self.frame.size.width withFont:[UIFont systemFontOfSize:15]];
    

    then, don't do the following:

    self.contentSize = cont_size;// it causes iOS halt occasionally.
    

    so, just use cont_size directly. I believe it's bug in iOS7 for now. Hopefully apple will fix it soon. Hope this is helpful.

提交回复
热议问题