4

I have created Custom UITextView class ,where textView grows dynamically as user types characters and textview frame resized dynamically.I want to align the text vertically center in the frame,it always coming to bottom by default.I added UIEdgeInsets to make or look a like it into center but it creates more issues for me.If i try to set content offset then the textView center also shifts.Is the content offset and textView center effect each other ?.I want textview center where ever it is but the text inside the textView stays vertically center. Initially i am setting UITextView font 23.0 with a rectangular border(textview frame layer border).When i start typing in that box the text is coming on bottom of the border.Then i am using

[textView setContentInset:UIEdgeInsetsMake(5,0,10,0)]; 

to make text in center. Then i have to send this text for printing which is placed on an UIImageView.Here i am using the mentioned code by Paras to match printer dpi and resizing it.But the text as looks on UIImageview doesn't match the exact placement of print because i have added UIEdgeInset to adjust vertical alignment

user1226038
  • 77
  • 1
  • 9
  • provide some example how exactly you are setting UIEdgeInsets and what issues it creates for you exactly? – art-divin May 02 '13 at 09:51
  • added after edit: then it's another type of question, because the right way is to use UIEdgeInsets as you specified (at least the answer on your initial question lays behind this property). – art-divin May 02 '13 at 10:30
  • See the answer to the same question posted here: http://stackoverflow.com/a/24871575/3499969 – Jeff Holliday Aug 14 '14 at 22:03

1 Answers1

-1

I have two types to set dynamic height to the UITextView see both are bellow...

UPDATE:

First Create UITextView programmatically like bellow...

-(IBAction)btnAddTextView:(id)sender
{
    UIView *viewTxt = [[UIView alloc]initWithFrame:CGRectMake(imgBackBoard.center.x - 100,20, 220, 84)];
    [viewTxt setBackgroundColor:[UIColor clearColor]];
    viewTxt.userInteractionEnabled = YES;
    
    UITextView *txtAddNote=[[UITextView alloc]initWithFrame:CGRectMake(20,20, 180, 44)];
    [txtAddNote setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
    [txtAddNote setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];
    txtAddNote.layer.borderWidth = 2.0;
    txtAddNote.layer.borderColor= [UIColor redColor].CGColor;
    viewTxt.tag = 111;
    txtAddNote.tag = 111;
    txtAddNote.userInteractionEnabled= YES;
    txtAddNote.delegate = self;
    txtAddNote.textColor = [UIColor whiteColor];
    [viewTxt addSubview:txtAddNote];
    [viewBoard addSubview:viewTxt];
    [txtAddNote release];

}

First

1. This bellow Method is Delegate Method of UITextViewDelegate.

in .h class add this UITextViewDelegate and then give your textView.delegate to self like bellow..

yourTextView.delegate = self;

and use paste this bellow method...

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, textView.contentSize.height);
    return YES;
}

here when your UITextView editing at a time with its text, the content size of textView is change.

2. Use my bellow custom Method for set Dynamic Height of UILable,UITextField and also UITextView

-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];

[text release];
[withFont release];

return suggestedSize.height;
}

use this method like bellow...

CGSize sizeToMakeLabel = [yourTextView.text sizeWithFont:yourTextView.font]; 
yourTextView.frame = CGRectMake(yourTextView.frame.origin.x, yourTextView.frame.origin.y, 
sizeToMakeLabel.width, sizeToMakeLabel.height); 
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • I think that changing the frame of UITextView each time symbol is inserted is not a good idea. User should work with NSString sizeWithFont: or similar approach to calculate height of the text and if the number of lines is changed only then one should change the frame – art-divin May 02 '13 at 09:52
  • @art-divin ya right i have this type of code also but this is very easy and simple so first i post this.. – Paras Joshi May 02 '13 at 09:54
  • @user1226038 see this answer also.. http://stackoverflow.com/questions/12591192/center-text-vertically-in-a-uitextview hope this helpful to you... :) +1 – Paras Joshi May 02 '13 at 10:20
  • see i create UITextView with my uPdated code wait i will post and its working great every text you seen in border of uitextview... i will updated that code... – Paras Joshi May 02 '13 at 10:23
  • @ParasJoshi author is asking about center position of the text inside the UITextView, but not about resizing whole UITextView – art-divin May 02 '13 at 10:26
  • yes dude.. i do something like that i just add and create some code which already do these things see my above method .. :) – Paras Joshi May 02 '13 at 10:27
  • @user1226038 here you can set UIView frame which you want and set textview frame to center so already its set the text in vertically .. :) – Paras Joshi May 02 '13 at 10:30