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


130 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
  19. Thanks for blogging about Software Testing, I hope you will post more regarding software testing, keep blogging...
    Regards,
    be projects in chennai | msc projects in chennai

    ReplyDelete
  20. Thanks for sharing with us. Great Blog to read,Its gives more useful information.Thanks lot.

    Software Testing Training in Chennai | Selenium Training in Chennai

    ReplyDelete
  21. I have seen lot blogs and Information on othersites But in this Java Blog Information is very useful thanks for sharing it........
    advanced java training in chennai | struts training in chennai

    ReplyDelete
  22. Easily peruses text from messages, internet browsers, PDFs and regular projects, for example, Outlook, Pages, Preview, and Text Edit.read this to me

    ReplyDelete

  23. This was an nice and amazing and the given contents were very useful and the precision has given here is good.
    Java Training in Chennai

    ReplyDelete
  24. This was an nice and amazing and the given contents were very useful and the precision has given here is good.


    Bigdata AnalyticsTraining in Bangalore

    ReplyDelete
  25. Nice Blog, Thanks for sharing this valuable one.This is very useful for me and gain more information,

    Java Training in Chennai

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. Wow, Excellent post. This article is really very interesting and effective.you are posting a good information for people and keep maintain and give more update too.

    React Training
    React Training in Chennai

    ReplyDelete
  28. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great

    content of different kinds of the valuable information's.

    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs Training in online

    angularjs Training in marathahalli

    ReplyDelete
  29. You finished a couple fine focuses there. I did an inquiry on the subject and discovered about all persons will oblige with your online journal. Tax Enquiry Accounts

    ReplyDelete
  30. Much obliged to you again for all the information you distribute,Good post. I was exceptionally keen on the article, it's entirely rousing I ought to concede. I like going to you site since I generally run over fascinating articles like this one.Great Job, I incredibly acknowledge that.Do Keep sharing! Respects, HMRC Tax Investigation Accountants

    ReplyDelete
  31. You have a decent point here!I thoroughly concur with what you have said!!Thanks for sharing your views...hope more individuals will read this article!!! Tax Enquiry

    ReplyDelete
  32. On the off chance that more individuals that compose articles truly fretted about composing incredible substance like you, more perusers would be occupied with their works. Much obliged to you for thinking about your substance. Let Campaign Tax Enquiry

    ReplyDelete
  33. When visiting blogs, i always look for a very nice content like yours .   Tax Investigation Self Employed

    ReplyDelete
  34. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us. Do check Six Sigma Training in Bangalore | Six Sigma Training in Dubai & Get trained by an expert who will enrich you with the latest trends.

    ReplyDelete
  35. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    python training in tambaram | python training in annanagar | python training in jayanagar

    ReplyDelete
  36. Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in
    advanced excel training in bangalore

    ReplyDelete
  37. I truly like you're composing style, awesome data, thankyou for posting. Let Property Campaign

    ReplyDelete

  38. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.

    rpa training in electronic-city | rpa training in btm | rpa training in marathahalli | rpa training in pune

    ReplyDelete
  39. I am trusting the same best exertion from you later on too. Truth be told your experimental writing abilities has roused me. Let Property Campaign Accountants

    ReplyDelete
  40. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
    Java training in Chennai | Java training in Bangalore

    Java interview questions and answers | Core Java interview questions and answers

    ReplyDelete
  41. Great Article...Thanks for sharing the best information.It was so good to read and useful to improve my knowledge as updated one.

    iOS Training in Chennai
    Android Training in Chennai
    Mobile Apps Training in Chennai

    ReplyDelete
  42. Thanks for your sharing such a useful information. this was really helpful to me

    zhosters
    Guest posting sites

    ReplyDelete

  43. Thank you for taking the time to write about this much needed subject. I felt that your remarks on this technology is helpful and were especially timely.


    devops course fees in chennai | devops training in chennai with placement | devops training in chennai omr | best devops training in chennai quora | devops foundation certification chennai

    ReplyDelete
  44. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.

    Best AWS Training in Chennai | Amazon Web Services Training in Chennai
    AWS Training in Bangalore | Amazon Web Services Training in Bangalore
    Amazon Web Services Training in OMR , Chennai | Best AWS Training in OMR,Chennai

    ReplyDelete
  45. Nice Information http://www.stylobe.com

    ReplyDelete
  46. This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
    Android Development Course in Chennai
    Android app Development Course in Chennai
    Android Training Institute in Chennai
    AWS Certification Training in Chennai
    AWS Training near me
    AWS Training in Chennai

    ReplyDelete
  47. I can’t imagine focusing long enough to research; much less write this kind of article. You’ve outdone yourself with this material. This is great content. Buy to Let Property Tax Advisors

    ReplyDelete
  48. This is my first time i visit here. I discovered such a large number of fascinating stuff in your web journal particularly its discourse. From the huge amounts of remarks on your articles, I figure I am not by any means the only one having all the satisfaction here! keep doing awesome Buy to Let Property Tax Advice

    ReplyDelete
  49. Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. seo services

    ReplyDelete
  50. This is extremely instructive substance and composed well for a change. It"s pleasant to see that a few individuals still see how to compose a quality post! domain authority blog comments

    ReplyDelete
  51. I as of late discovered numerous helpful data in your site particularly this online journal page. Among the loads of remarks on your articles. A debt of gratitude is in order for sharing. Buy to Let Tax Advisor

    ReplyDelete
  52. Pleasant web journal and totally exceptional. You can improve yet despite everything I say this perfect.Keep striving generally advantageous. Buy to Let Property Tax Advice London

    ReplyDelete
  53. It turned out to be Very useful to me and I am certain to every one of the observers here! Property Tax Accountant

    ReplyDelete
  54. That is the excellent mindset, nonetheless is just not help to make every sence whatsoever preaching about that mather. Virtually any method many thanks in addition to i had endeavor to promote your own article in to delicius nevertheless it is apparently a dilemma using your information sites can you please recheck the idea. thanks once more. Property Tax Advice

    ReplyDelete
  55. decent post, stay aware of this fascinating work. It truly regards realize that this subject is being secured likewise on this site so cheers for setting aside time to talk about this! blog comments backlinks

    ReplyDelete
  56. A debt of gratitude is in order for sharing the data, keep doing awesome... I truly delighted in investigating your site. great asset... Property Tax Advisors

    ReplyDelete
  57. Every one of the substance you said in post is too great and can be extremely helpful. I will remember it, much obliged for sharing the data continue upgrading, looking forward for more posts.Thanks high DA blog comments

    ReplyDelete
  58. Very nice post here thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    machine learning training center in Chennai
    machine learning training in velachery
    machine learning certification course in Chennai
    machine learning certification in Chennai

    ReplyDelete
  59. I was taking a gander at some of your posts on this site and I imagine this site is truly informative! Keep setting up.. Tax on Buy to Let Property

    ReplyDelete
  60. This was truly a fascinating point and I kinda concur with what you have said here! Tax Investigation Accountants

    ReplyDelete
  61. This is a really decent site post. Not very numerous individuals would really, the way you simply did. I am truly inspired that there is such a great amount of data about this subject have been revealed and you"ve put forth a valiant effort, with so much class. On the off chance that needed to know more about green smoke surveys, than by all methods come in and check our stuff. Tax Investigation Advisors

    ReplyDelete
  62. Much thanks for composing such an intriguing article on this theme. This has truly made me think and I want to peruse more. Tax Investigation Self Assessment

    ReplyDelete
  63. thanks for your Draw beautiful diagram effortlessly on ipad.
    android training in chennai

    ReplyDelete
  64. I adore this site!! The blaze up the top is great!! Tax Investigation Specialists

    ReplyDelete
  65. Extraordinary data! I as of late went over your online journal and have been perusing along. I thought I would leave my first remark. I don't recognize what to say aside from that I have. Tax Investigation Self Employed

    ReplyDelete
  66. Companion, this site may be fabolous, i simply like it. Tax Investigation Accountants

    ReplyDelete
  67. Really useful information. Thank you so much for sharing.It will help everyone.Keep Post. RPA training in chennai | RPA training in Chennai with placement

    ReplyDelete
  68. You may tune in to any of the stored text while driving, practicing or voyaging. In this way, the utilization of a MP3 player isn't restricted to simply tuning in to music, and you stay gainful even while driving. notevibes.com

    ReplyDelete
  69. This is truly a decent and enlightening, containing all data furthermore greatly affects the new innovation. A debt of gratitude is in order for sharing it, Tax Investigation Advisors

    ReplyDelete
  70. Incredible Information sharing .. I am exceptionally glad to peruse this article .. a debt of gratitude is in order for giving us experience info.Fantastic pleasant. I welcome this post. Tax Investigation Specialists

    ReplyDelete
  71. Your site furnished us with profitable data to work with. Each and every tips of your post are magnificent. You rock for sharing. Continue blogging.. Tax Investigation Self Assessment

    ReplyDelete
  72. This is extremely fascinating substance! I have completely appreciated perusing your focuses and have arrive at the conclusion that you are right about large portions of them. You are extraordinary. Tax Investigation Self Employed

    ReplyDelete
  73. Incredible data! I as of late went over your site and have been perusing along. I thought I would leave my first remark. I don"t comprehend what to say aside from that I have. Revenue Tax Investigation

    ReplyDelete
  74. its is very very helpful for all of us and I never get bored while reading your article because

    AWS Training in Bangalore

    Best AWS Training Institute in Bangalore

    ReplyDelete
  75. I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  76. Thanks for sharing this information admin, it helps me to learn new things. Continue sharing more like this.
    Regards,
    Tableau training in Chennai | Tableau Courses Training in Chennai | Tableau training Institute in Chennai

    ReplyDelete
  77. I am all that much satisfied with the substance you have specified. I needed to thank you for this extraordinary article. Tax Investigation Self Employed

    ReplyDelete
  78. You know your activities emerge of the group. There is something extraordinary about them. It appears to me every one of them are truly splendid! Tax Investigation Accountants

    ReplyDelete