简体   繁体   中英

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:

在此处输入图片说明

There are no newlines in that textView right now and it works fine on 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

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

that tracks bounds changes and sets bounds to 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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