这就是我的尝试,
UITextView *_textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 10)];
NSString *str = @"This is a test text view to check the auto increment of height of a text view. This is only a test. The real data is something different.";
_textView.text = str;
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;//Here i am adjusting the textview
[self.view addSubview:_textView];
Basically after fitting the text into textview,scrolling is enable,but i cannot view the content inside the textview without scrolling the textview.I do want to initialize the UITextView frame size based on the text size,font name etc.
感谢任何解决方案。谢谢。
答案 0 :(得分:2)
NSString *str = @"This is a test text view to check the auto increment of height of a text view. This is only a test. The real data is something different.";
UIFont * myFont = [UIFont fontWithName:@"your font Name"size:12];//specify your font details here
//然后计算上述文本所需的高度。
CGSize textviewSize = [str sizeWithFont:myFont constrainedToSize:CGSizeMake(300, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
//根据您从上面获得的高度
初始化您的textviewUITextView *_textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, textviewSize.width, textviewSize.height)];
_textView.text = str;
[self.view addSubview:_textView];
并且您还要在textview中禁用滚动,然后参考。
正如William Jockusch在his answer here中所述:
您可以通过以下方法禁用几乎所有滚动 进入你的UITextView子类:
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated { // do nothing }
我说“几乎”所有滚动的原因是,即使有上述情况, 它仍然接受用户滚动。虽然你可以禁用它们 将self.scrollEnabled设置为NO。
如果你只想禁用一些卷轴,那就做一个ivar,让我们打电话 它acceptScrolls,以确定是否要允许滚动或 不。然后你的scrollRectToVisible方法看起来像这样:
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated { if (self.acceptScrolls) [super scrollRectToVisible: rect animated: animated]; }