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

标签 ios objective-c uitextview frame

我有一个可以旋转,调整大小并拖动的UITextView,当我调整大小,旋转或拖动但旋转UITextView然后调整大小时,在iOS 6和iOS 5上一切正常,出现以下结果:

该textView中目前没有换行符,并且在iOS 6上运行良好

对于旋转,我正在使用:

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

为了调整大小,我计算用户设置的宽度的新字体大小,并使用以下方法设置边界:
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);

我知道这是一个范围问题,但是我不确定是什么问题还是做错了什么

最佳答案

好的,我修复了。

我们有一个KVO边界观察器

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

跟踪边界变化并将边界设置为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];
    }
}

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

关于ios - 旋转后更改UITextView字体大小在iOS 5上很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16261842/

相关文章:

objective-c - ABRecordCopyValue() EXC_BAD_ACCESS 错误

ios - 是否可以缓存视频? IOS- swift

objective-c - 在全屏应用程序前添加 NSWindow

objective-c - 使窗口弹出进出屏幕边缘

ios7 - 如何更改 UITextView 的光标样式?

ios - 仅当 UITextView 的内容 View 大于其名望时才显示图像

ios - 为什么我得到 : Object file was built for newer iOS version than being linked?

ios - DownPicker选中时如何获取Array Index Value

iphone - 如何将应用程序的铃声添加到 iPhone 的铃声中?

iOS >> UITextView >> 自动 "Jump"到最后一行