将转换应用于UITextView - 防止内容调整大小 [英] Applying transform to UITextView - prevent content resizing

查看:170
本文介绍了将转换应用于UITextView - 防止内容调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将旋转变换应用于 UITextView 然后单击里面开始编辑时,似乎内容大小会自动变宽。内容视图的新宽度是旋转视图的边界框的宽度。例如,给定宽度为500且高度为400且旋转30度的文本框,新内容宽度为:

When I apply a rotation transform to a UITextView and then click inside to begin editing, it appears that the content size is automatically being made wider. The new width of the content view is the width of the rotated view's bounding box. For example, given a text box of width 500 and height 400, and rotated by 30 degrees, the new content width would be:

(500 * cos(30)) + (400 * sin(30)) = 633

或以图形方式:

有趣的是,如果您已经在编辑文本视图并且然后应用了转换,那么看起来似乎没有对内容大小进行修改。所以看起来在文本编辑开始的某个时候,文本视图会查看其 frame 属性,并根据帧宽调整内容大小。我想这个解决方案是告诉它使用 bounds 属性,但是我不知道在哪里做这个,因为我不确定到底在哪里文本视图决定修改内容大小。

Interestingly, if you are already editing the text view and THEN apply the transform, then it appears that no modification is made to the content size. So it appears that sometime around the start of text editing, the text view looks at its frame property and adjusts the content size based on the frame width. I imagine the solution to this is to tell it to use the bounds property instead, however I don't know where to do this, as I'm not sure exactly where the text view is deciding to modify the content size.

我已经google了,但似乎找不到任何使用转换后的UITextViews的引用。有没有人对此有任何想法?

I have googled but can't seem to find any references to using transformed UITextViews. Does anybody have any ideas about this?

编辑(测试项目中的按钮操作):

- (IBAction)rotateButtonTapped:(id)sender {
    if (CGAffineTransformIsIdentity(self.textView.transform)) {
        self.textView.transform = CGAffineTransformMakeRotation(30.0 * M_PI / 180.0);
    }
    else {
        self.textView.transform = CGAffineTransformIdentity;
    }

    NSLog(@"contentsize: %.0f, %.0f", textView.contentSize.width, textView.contentSize.height);
}


推荐答案

我也被困在这问题。

我找到的唯一解决方案是创建UIView实例并将UITextView添加为子视图。然后你可以旋转UIView的实例,UITextView也可以正常工作。

The only solution which I found was to create an instance of UIView and add the UITextView as a subview. Then you can rotate the instance of UIView and UITextView will work just fine.

UITextView *myTextView = [[UITextView alloc] init];
[myTextView setFrame:CGRectMake(0, 0, 100, 100)];

UIView *myRotateView = [[UIView alloc] init];
[myRotateView setFrame:CGRectMake(20, 20, 100, 100)];
[myRotateView setBackgroundColor:[UIColor clearColor]];
[myRotateView addSubview:myTextView];

myRotateView.transform = CGAffineTransformMakeRotation(0.8);
[[self view] addSubview:myRotateView];

这篇关于将转换应用于UITextView - 防止内容调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆