How to set height of UITextView according to its content in objective c
Asked Answered
P

5

6

I have a UITextView and want to set its height as per its content changes.

How can I get height of the content.

Please guide me.

Thanks in advance.

Particia answered 13/9, 2011 at 11:56 Comment(2)
What kind of content do you have?Stinking
I have string in two languages English and JapaneesParticia
F
17

If you want to calculate height of your UITextView for some text then you can use such method:

CGSize textViewSize = [text sizeWithFont:[UIFont fontWithName:@"Marker Felt" size:20] 
                       constrainedToSize:CGSizeMake(WIDHT_OF_VIEW, FLT_MAX) 
                       lineBreakMode:UILineBreakModeTailTruncation];

In fontWithName define font that you are using in UITextView, in size parameter - size of your text in UITextView. Replace WIDHT_OF_VIEW width width of your UITextView.

textViewSize.height will contain height that UITextField to display text without scrolling

Fifi answered 13/9, 2011 at 12:2 Comment(10)
Okay let me do according to thisParticia
Why is this so complicated? It works, but this seems such an intricate solution that I almost cant believe that there is not a simpler solution ... however I have not found it even after searching for hours ... in every other platform I know (Java, NET, html) this is so much easier ...Nonagon
@Nonagon I can't agree that this solution is complicated. But if you need to know size for one line then you can use [text sizeWithFont:font] method. What could be simplier?Fifi
@Fifi Well, the driving goal of the question is to get a "automatically growing text box". With this solution first you have to get the width of the UITextView (for that you typically have to find out the orientation of the device). Then you have to calculate the height of the content of the UITextView (for that you have to ask the UITextView for details like current font). Then you have to propagate the calculated height back to the UITextView, with autolayout not that easy ... This is much too complicated for me and breaks all kind of encapsulations ... but I am a simple minded guy :-)Nonagon
@Fifi By the way: I am glad you posted that solution and I upvoted. It helped me to solve my problem. But I think there should be a simpler solution provided by the iOS platform to realize an "automatically growing text box".Nonagon
@Nonagon I see. May be. But if everything is simple then how I will earn money? =)Fifi
The functions used in the example have been depreciated in iOS 6.Acred
@Acred in iOS7, I think.Fifi
@Fifi I followed your code. But, the textView Frame height hide(ie. not fit upto last line) the last line, why?.Ferrel
depricated in ios 7 and aboveVarico
E
10
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CGRect frame = _textView.frame;
    frame.size.height = _textView.contentSize.height;
    _textView.frame = frame;
}
Euroclydon answered 13/9, 2011 at 12:7 Comment(2)
This seems not to be working when put in viewWillAppear. It works when put in viewDidAppear but then it is causing a "visual flicker", meaning that the resizing is visible on screen. _textView.contentSize.height returns a wrong value when called in viewWillAppear...Nonagon
I am using it in viewDidLoad but not working...your comment proves very helpful.It works in viewDidAppear.Plexiglas
S
1

You can calculate the size of the text with the method from UIStringDrawing category:

CGSize textSize = [textView.text sizeWithFont:font 
                                 forWidth:textWidth 
                                 lineBreakMode: UILineBreakModeWordWrap];
Stinking answered 13/9, 2011 at 12:7 Comment(0)
H
0
CGSize countentSize = textView.contentSize;
int numberOfLinesNeeded = countentSize.height / textView.font.lineHeight;
int numberOfLinesInTextView = textView.frame.size.height / textView.font.lineHeight;
CGRect textViewFrame= textView.frame;
textViewFrame.size.height = numberOfLinesNeeded * textView.font.lineHeight;
[textView setFrame:textViewFrame];
Hightest answered 3/3, 2014 at 16:51 Comment(0)
W
0

sizeWithFont is deprecated .

CGRect rect = [textView.text boundingRectWithSize:CGSizeMake(textView.frame.size.width, CGFLOAT_MAX)
                                                                    options:NSStringDrawingUsesLineFragmentOrigin
                                                                    attributes:@{NSFontAttributeName: textView.font}
                                                                    context:nil];
        rect.size.width = ceil(rect.size.width);
        rect.size.height = ceil(rect.size.height);

I hope it will help someone. Happy coding.

Weasner answered 12/9, 2022 at 1:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.