UITextView's text going beyond bounds

后端 未结 7 1467
深忆病人
深忆病人 2020-12-30 01:24

I have a non-scrollable UITextView with it\'s layoutManager maximumNumberOfLines set to 9, which works fine, but, I cannot seem to find a method in NSLayoutManager that rest

7条回答
  •  独厮守ぢ
    2020-12-30 02:18

    I created a test VC. It increases a line counter every time a new line is reached in the UITextView. As I understand you want to limit your text input to no more than 9 lines. I hope this answers your question.

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property IBOutlet UITextView *myTextView;
    
    @property CGRect previousRect;
    @property int lineCounter;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.myTextView setDelegate:self];
    
    self.previousRect = CGRectZero;
    self.lineCounter = 0;
    }
    
    - (void)textViewDidChange:(UITextView *)textView {
    UITextPosition* position = textView.endOfDocument;
    
    CGRect currentRect = [textView caretRectForPosition:position];
    
    if (currentRect.origin.y > self.previousRect.origin.y){
        self.lineCounter++;
        if(self.lineCounter > 9) {
            NSLog(@"Reached line 10");
            // do whatever you need to here...
        }
    }
    self.previousRect = currentRect;
    
    }
    
    @end
    

提交回复
热议问题