15

I have a UITextView that has some text in it. The problem is that the text scrolls outside of the boundaries of the UITextView box. (The UITextView is uneditable.)

here's the code and what I have tried to resolve this issue:

  - (void)viewDidLoad {

        textBG.contentInset = UIEdgeInsetsZero;

    //  textBG.layer.masksToBounds = NO;
        textBG.layer.cornerRadius = 10.0;
        textBG.layer.borderWidth = 0.0;
        [textBG setClipsToBounds:YES];

    [super viewDidLoad];
}

- (void)textViewDidBeginEditing:(UITextView*)textView
{

textBG.contentInset = UIEdgeInsetsZero;    
 [textBG setClipsToBounds:YES];   
}

- (void) shouldChangeTextInRange:(UITextView*)textView {

    textBG.contentInset = UIEdgeInsetsZero;    
    [textBG setClipsToBounds:YES];

}

thanks for any help

enter image description here

hanumanDev
  • 6,592
  • 11
  • 82
  • 146

4 Answers4

20

write this in textView delegate methods like textViewDidBeginEditing

textBG.contentInset = UIEdgeInsetsZero;

in viewDidload

[textBG setClipsToBounds:YES];
Narayana Rao Routhu
  • 6,303
  • 27
  • 42
  • 1
    same problem i faced before two days just i put textBG.contentInset = UIEdgeInsetsZero; in shouldChangeTextInRange and textViewDidBeginEditing methods.its working fine in my case .. – Narayana Rao Routhu Sep 30 '11 at 09:28
  • then put [textBG setClipsToBounds:YES]; i think it will work fine – Narayana Rao Routhu Sep 30 '11 at 09:31
  • 1
    I tried your answer and it worked for the text out of boundary issue. But my view had a shadow, when I used `clipsToBounds = YES`, the shadow wouldn't show up. Any solution to keep the shadow? @Narayana – Gon Jan 15 '14 at 13:32
  • @Gon: Did you ever find a solution for it? – Ing. Ron Feb 07 '17 at 14:05
  • 1
    worked for me. textBG.textView.contentInset = UIEdgeInsets.zero in swift 4 – Ravi Sep 15 '18 at 15:52
2

Try to change in XIB for your TextView like below:enter image description here

Loquatious
  • 1,791
  • 12
  • 21
1

Just put this one line code to solve problem.

yourTextView.clipsToBounds = true
Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19
0

I faced this same problem, so I track down the source ... this is because of while adding shadow somehow you disturbed it's masksToBounds property. Therefore, while adding more text it got overflowed. Simple solution which I found is to write this simple line after adding shadow.

[self.myTextView setClipsToBounds:YES];

usually should be in viewDidLoad method, but remember to write this line only after applying shadow.

Thanks for reading this.

Najam
  • 1,129
  • 11
  • 32