I have added a UITextView inside a UIView. UIView has some height depending upon screen sizes. UITextView can have more or less text. So I want to make the height of UITextView dynamic so if text is more then it should have more height but it should be less than the height of main view . If text is less then it should have less height.
            Asked
            
        
        
            Active
            
        
            Viewed 1,332 times
        
    0
            
            
        - 
                    dont you want the textview to scroll ? – Teja Nandamuri Oct 19 '15 at 12:52
- 
                    No i don't want to scroll – TechChain Oct 19 '15 at 12:52
- 
                    r u using storyboard ? – Teja Nandamuri Oct 19 '15 at 12:53
- 
                    r u used autolayout or autoresizing in your project – Anbu.Karthik Oct 19 '15 at 12:53
- 
                    I am using story board & autolayout – TechChain Oct 19 '15 at 12:54
- 
                    @deepakkumar hope this will work... http://stackoverflow.com/questions/50467/how-do-i-size-a-uitextview-to-its-content – Bhavin Ramani Oct 19 '15 at 13:20
4 Answers
5
            Size a UITextView to its content programmatically:
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [textView setDelegate:self];
    [textView setText:@"your long text"];
    [textView setScrollEnabled:NO];
    [self.view addSubview:textView];
    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    textView.frame = newFrame;
 
    
    
        Ty Lertwichaiworawit
        
- 2,950
- 2
- 23
- 42
0
            
            
        If you are using textview to display multilines of text with scrolling disabled, It is nothing more than a UILabel. I suggest you to use UILabel for the purpose.
In the storyboard, set the constraints as follows:
 
    
    
        Teja Nandamuri
        
- 11,045
- 6
- 57
- 109
- 
                    please note that constraint values are given as per my need. You can modify them as per your need – Teja Nandamuri Oct 19 '15 at 13:01
- 
                    
- 
                    you can even wrap the text in uilabel, the only difference is labels are read only and text views are editable. – Teja Nandamuri Oct 19 '15 at 13:02
- 
                    I have used beizer path to set the text wrap.which is not possible using label – TechChain Oct 19 '15 at 13:03
- 
                    try this with text view giving the same constraints, see if it works or not. I think this set of constraints shold work on text view too. Make sure your textview is not scrollable . – Teja Nandamuri Oct 19 '15 at 13:05
0
            
            
        Same as the first answer, but in Swift 4:
let screenSize = UIScreen.main.bounds
let textView = UITextView.init(frame: CGRect(x: 1, y: 40, width: screenSize.width, height: screenSize.height))
textView.delegate = self as? UITextViewDelegate
textView.isScrollEnabled = false
view.addSubview(textView)
let fixedWidth = textView.frame.size.width
let newSize: CGSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT)))
var newFrame = textView.frame
newFrame.size = CGSize(width: CGFloat(fmaxf(Float(newSize.width), Float(fixedWidth))), height: newSize.height)
 
    
    
        S. Rivers
        
- 1
- 1
-1
            
            
        Try with this
-(void)dynamicTextSize{
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(200, 300, 200, 30)];
textView.center = self.view.center;
[self.view addSubview:textView];
NSString *string  = @"This is pour text.a hjajksdkja kajhdsjk akjdhs jakhd skjahs ajkdhsjkad hskja akjdhs ajkhdjskar";
textView.text = string;
//UIFont *font = [UIFont fontWithName:@"Arial" size:16.0f];
NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:textView.font, NSFontAttributeName, nil];
textView.backgroundColor = [UIColor lightGrayColor];
CGRect frame  = [textView.text boundingRectWithSize:CGSizeMake(textView.frame.size.width, 10000) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
CGRect mFrame = textView.frame;
mFrame.size.width = frame.size.width;
mFrame.size.height = frame.size.height;
textView.frame = mFrame;
//NSLog(@"frame2:%@",NSStringFromCGRect(textView.frame));
}
 
    
    
        Jamil
        
- 2,977
- 1
- 13
- 23
 
     
    
