1

This is what i have tried,

 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.

Any solution is appreciated.Thanks.

Master Stroke
  • 5,108
  • 2
  • 26
  • 57

1 Answers1

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

//then calculate the required height for the above text.

CGSize textviewSize = [str sizeWithFont:myFont constrainedToSize:CGSizeMake(300, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

//initialize your textview based on the height you got from the above

UITextView *_textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, textviewSize.width, textviewSize.height)];
_textView.text = str;
[self.view addSubview:_textView];

And also you want to disable the scrolling in textview then refer this.

As William Jockusch states in his answer here:

You can disable almost all scrolling by putting the following method into your UITextView subclass:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
  // do nothing
}

The reason I say "almost" all scrolling is that even with the above, it still accepts user scrolls. Though you could disable those by setting self.scrollEnabled to NO.

If you want to only disable some scrolls, then make an ivar, lets call it acceptScrolls, to determine whether you want to allow scrolling or not. Then your scrollRectToVisible method can look like this:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
   if (self.acceptScrolls)
     [super scrollRectToVisible: rect animated: animated];
}
Community
  • 1
  • 1
Master Stroke
  • 5,108
  • 2
  • 26
  • 57
  • had missed something. .it worked like a charm now. .thanks a lot –  Jan 26 '13 at 05:22
  • Please provide proper citation when you copy the words of others. I've done this in your answer above, but make sure you do this in the future. – Brad Larson Feb 08 '13 at 23:21