简体   繁体   English

旋转后更改UITextView字体大小在iOS 5上很奇怪

[英]Changing UITextView font size after rotation works weird on iOS 5

I have a UITextView that can be rotated, resized dragged etc, and everything works fine on iOS 6 and iOS 5 when I resize or rotate or drag but when I rotate the UITextView and then resize the following result occurs: 我有一个可以旋转,调整大小并拖动的UITextView,当我调整大小或旋转或拖动但在旋转UITextView然后调整大小时,在iOS 6和iOS 5上一切正常,出现以下结果:

在此处输入图片说明

There are no newlines in that textView right now and it works fine on iOS 6 该textView中现在没有换行符,并且在iOS 6上运行良好 在此处输入图片说明

For rotation I'm using: 对于旋转,我正在使用:

- (void)rotation:(CGFloat)newAngle {
    self.transform = CGAffineTransformMakeRotation(newAngle *  M_PI / 180);
}

And for resize I calculate the new font size for the width user has set and set bounds with: 为了调整大小,我计算用户设置的宽度的新字体大小,并使用以下方法设置边界:

CGSize newFrameSize = [self sizeThatFits:CGSizeMake([[self getLongestStringInTextView] sizeWithFont:self.font].width + 30, CGFLOAT_MAX)];
self.bounds = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, newFrameSize.width, newFrameSize.height);

I know its a problem with bounds but I'm not sure what is wrong or if I'm doing anything wrong 我知道这是一个界限问题,但是我不确定是什么问题还是做错了什么

Ok, I fixed it. 好,我把它修好了。

We have a KVO observer for bounds 我们有一个KVO边界观察器

[self addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionOld context:NULL];

that tracks bounds changes and sets bounds to CGRectZero 跟踪边界变化并将边界设置为CGRectZero

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"bounds"]) {
        [self removeObserver:self forKeyPath:@"bounds"];

        CGRect bounds = self.bounds;
        self.bounds = CGRectZero;
        self.bounds = bounds;

        [self addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionOld context:NULL];
    }
}

which works fine on iOS 6 but does not on iOS 5, which got fixed by also setting frame to CGRectZero 在iOS 6上可以正常使用,但在iOS 5上却不能,通过将frame设置为CGRectZero可以解决此问题

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"bounds"]) {
        [self removeObserver:self forKeyPath:@"bounds"];

        CGRect bounds = self.bounds;
        CGRect frame = self.frame;
        self.frame = CGRectZero;
        self.bounds = CGRectZero;
        self.frame = frame;
        self.bounds = bounds;

        [self addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionOld context:NULL];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM