Draw beautiful diagram effortlessly on ipad. Click here to know more..

Saturday, March 23, 2013

Variable size text view in iOS (iPad and iPhone)

Based on specific need, you may want a variable size text view on iOS as

  • Fixed width. Vary height based on length of text
  • Vary both width and height, based on length of text. You will increase height if new line is added otherwise increase width.
These two kinds of text views are used in "Lekh Diagram" app. This is a diagramming app. You can add text on a shape, or free floating text. If text is added on a shape, then width of text is constrained to the width of shape. The free floating text has variable width and height.

Lets see how to do in code:
Add to text boxes, one for fixed width and other for variable width and height.

In ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView1;
@property (weak, nonatomic) IBOutlet UITextView *textView2;
@end

In ViewController.m

#import "ViewController.h"

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.textView1.delegate = self;
    self.textView2.delegate = self;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
-(BOOL)shouldAutorotate
{
    return NO;
}
- (void)textViewDidChange:(UITextView *)textView
{
    // textView2 has variable width and variable height
    if (textView == self.textView2) {
        // max widht and height: some random big value
        float maxWidth = 40000;
        float maxHeight = 40000;
        
        CGSize sz = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(maxWidth, maxHeight)];
        
        // There is some margin around text view. we need to consider margin,
        // otherwise there will text wrapping without new line.
        sz.width += 32;
        sz.height += 16;
        
        CGRect frame = textView.frame;
        frame.size = sz;
        textView.frame = frame;
    }
    else {
        // textView1 has fixed width and variable height
        CGRect frame = textView.frame;
        frame.size.width = textView.contentSize.width;
        frame.size.height = textView.contentSize.height;
        textView.frame = frame;
    }
}
@end


In code above, textView2 has variable width and height and textView1 has fixed width.
Download code from here

The code above is based on code used in "Lekh Diagram", a sketch recognition diagramming app for iPad and iPhone.
Get "Lekh Diagram" from app store: https://itunes.apple.com/us/app/lekh-diagram/id576124115?mt=8


25 comments:

  1. I agree with your post. Android software development kit makes the application development process lot simpler and effective. You can create best performing android application with ease. Android Training Institutes in Chennai

    ReplyDelete
  2. Nice Blog

    Telugu70mm.com Provides Latest Telugu Movie Reviews and other news like Telugu Movie News , Telugu Political News and Movie Released Dates

    ReplyDelete
  3. Excellent and very cool idea and the subject at the top of magnificence and I am happy to this post..Interesting post! Thanks for writing it.What's wrong with this kind of post exactly? It follows your previous guideline for post length as well as clarity.
    Java Training in Chennai

    ReplyDelete
  4. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
  5. Please count the heads to come disagreeing over a noble sense of dressing either to them or to their darling gizmos to wear. If yes, it shapes them unlikely to please all socially. teleprompter

    ReplyDelete
  6. This blog had a to a great degree solid effect on me.
    visit our website

    ReplyDelete
  7. The new hello tech Austin Convention Center proposed disposal of printed material requests bringing about 200 percent ROI for the tech monster.click for more info

    ReplyDelete
  8. Thanks for your marvelous posting! It is very useful and good. Come on. I want to introduce an get app installs, I try it and I feel it is so good to rank app to top in app store search results, have you ever heard it?

    ReplyDelete
  9. This information is impressive..I am inspired with your post writing style & how continuously you describe this topic. After reading your post,thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic Excellent article.
    AngularJS Training Institute in Chennai
    AngularJS Cetification Training in Chennai
    AWS Training in Chennai
    AWS Devops Training in Chennai
    DevOps Certification in Chennai
    DevOPs Certification Training Institute in Chennai
    Best AngularJS Training in Chennai
    AngularJS Training in Chennai

    ReplyDelete
  10. Pretty post, Thanks for sharing an innovative idea, It is useful information for beginners.
    Learn Android course.

    ReplyDelete
  11. Such a beautiful content, this writer is very smart selection of the page.
    i proud of that writer, thank you.
    2stage and single stage snow blower

    ReplyDelete
  12. I was barely amazed at how you had written this content. Please keep posting.
    free java training in chennai
    Core Java Training

    ReplyDelete
  13. Very true and inspiring article. I strongly believe all your points. I also learnt a lot from your post. Cheers and thank you for the clear path.
    hadoop big data training in chennai
    best institute for big data in chennai

    ReplyDelete
  14. Superb information, as always. After reading this one I really got refreshing and fantastic feeling! This is also a great and encouraging post.
    best dot net training
    dot net course

    ReplyDelete
  15. I am always searching online for articles that can help. There is obviously a lot to know about this. I think you made some good points.

    cloud computing training chennai
    cloud computing training centers in chennai

    ReplyDelete
  16. I was really excited about your daily updates. If you have new update me.
    Digital Marketing Training in Chennai
    Digital Marketing Chennai

    ReplyDelete
  17. This is the best explanation I have seen so far on the web. I was looking for a simple yet informative about this topic finally your site helped me a lot.

    Software testing training
    Software training

    ReplyDelete
  18. It’s very awesome blog. It will help to improve my basic to higher level. Thank you for sharing this wonderful site.
    Digital Marketing Courses in Chennai | Digital marketing training institute in Chennai | Digital marketing course Chennai | Digital Marketing Training

    ReplyDelete