0

I want to change my UITextView's content size, and I tried as below:

[self.textView setContentSize:CGSizeMake(self.textView.contentSize.width, self.textView.contentSize.height+30)];

I found it doesn't work. What's more, I make it in a button's click event to see what will happen.

- (IBAction)click:(id)sender {
    static int y = 0;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, y, 30, 30)];
    label.text = @"hello";
    y = y+30;
    [self.textView setContentSize:CGSizeMake(self.textView.contentSize.width, self.textView.contentSize.height+30)];
    NSLog(@"%f",self.textView.contentSize.height);
    [self.textView addSubview:label];
}

The result is the setContentSize doesn't work? What's more, contentSize just change for text content changing.

I want to know why, and Is there any method to change the UITextView's contentSize manually?

ADDITION: output for click action

2014-11-17 17:05:56.306 viewControllerAdvance[2371:607] 82.000000 
2014-11-17 17:05:56.455 viewControllerAdvance[2371:607] 82.000000
2014-11-17 17:05:56.638 viewControllerAdvance[2371:607] 82.000000
2014-11-17 17:05:56.772 viewControllerAdvance[2371:607] 82.000000
2014-11-17 17:05:56.923 viewControllerAdvance[2371:607] 82.000000
2014-11-17 17:05:57.072 viewControllerAdvance[2371:607] 82.000000
Daizy
  • 331
  • 2
  • 5
  • 12
  • Can you please post your NSLog(@"%f",self.textView.contentSize.height); values after calling - (IBAction)click:(id)sender method few times? – thavasidurai Nov 17 '14 at 09:04
  • 1
    Are you sure you need to set contentsize or frame – Ramesh Muthe Nov 17 '14 at 09:05
  • When you write the text in the textview the contentsize automatically increases and why you need to set it manually ?? – Ramesh Muthe Nov 17 '14 at 09:09
  • @RameshMuthe, Actually, I want change some content word into tags (maybe using UILabel), just like StackOverFlow's adding tags. So maybe the textview's height is not content word's height. – Daizy Nov 17 '14 at 09:11
  • Just do a cross check once whether you are resetting content size or allocating textview again – thavasidurai Nov 17 '14 at 09:13
  • @thavasidurai, May you describe more details about it? I'm a freshbird.:p – Daizy Nov 17 '14 at 09:19
  • are you allocating or resetting(content size) textview anywhere after click acton? – thavasidurai Nov 17 '14 at 09:23
  • try by changing this line instead of yours `[self.textView setContentSize:CGSizeMake(self.textView.contentSize.width, self.textView.contentSize.height+y)];` – Shankar BS Nov 17 '14 at 09:23
  • tell me your exact requirement and I think in this way you can not handle the textview.If possible add the image what you need and think of different solution – Ramesh Muthe Nov 17 '14 at 09:28
  • Are you using autolayout? – Kiran Thapa Nov 17 '14 at 09:50
  • @RameshMuthe, I just want to make an input box, which can make word into tags. It's something just like Stack over flow's adding tags step when you want to ask a question. It can make some of your word to become a tag, and you can continue to input after your tags. I want to make tags `inline` style in my text box – Daizy Nov 17 '14 at 09:52
  • @KiranThapa, I dont know, how to check it? – Daizy Nov 17 '14 at 10:04
  • @Shan, I think it's not the answer. – Daizy Nov 17 '14 at 10:05
  • i know but your content view is increasing its height – Shankar BS Nov 17 '14 at 10:07
  • Click the storyboard or xib file. On the file inspector pane (View -> Utilities -> Show File Inspector), there is "Interface Builder Document" section. There you will find the checkbox for whether you are using auto layout or not. – Kiran Thapa Nov 17 '14 at 10:11

1 Answers1

0

Try to modify your method like below and check whether it meets your requirement or not.To use below way you need to add your text view on a scroll view and whenever you increase text view's height you suppose to increase scroll view's content size

- (IBAction)click:(id)sender { 

     static int y = 0;
     UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, y, 30, 30)];
     label.text = @"hello";
     y = y+30;
     [self.textView addSubview:label];

     CGFloat fixedWidth = textView.frame.size.width;
     CGFloat dynamicHeight = textView.frame.size.height+30;

     CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, dynamicHeight)];
     CGRect newFrame = textView.frame;
     newFrame.size = CGSizeMake(fixedWidth, fmaxf(newSize.height, dynamicHeight));
     textView.frame = newFrame;

 }

Also set textview.scrollEnabled = NO;

thavasidurai
  • 1,972
  • 1
  • 26
  • 51
  • Yeah, it's really meet my requirement! Thanks a lot!! But why `setContentSize` doesnt work? – Daizy Nov 17 '14 at 10:07
  • Look at this http://stackoverflow.com/questions/19837178/resize-uitextview-according-to-its-content-in-ios7 it seems setting content size may not work in iOS 7 – thavasidurai Nov 17 '14 at 10:18