Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

oneeyeman

macrumors newbie
Original poster
Nov 14, 2012
16
0
Hi, ALL,

I'd like to limit number of characters in the NSTextView.

On the SO I got a sduggestion to use the following code:

- (BOOL)shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString { return [super shouldChangeTextInRange:affectedCharRange replacementString:replacementString] && (self.string.length + (replacementString.length - affectedCharRange.length) <= 140); }

It works as expected when I type the text or when I try to paste the text which is below the limit.

But when the paste occur that bring the text size to a bigger number of characters I need the paste to occur so that the number of characters become the maximum.

To give an example:

Lets sy the maximum number of characters is 20.
The view contains 10 characters - "abcdefghij".

Now I want to paste the text which has 20 characters: "abcdefghijklmnopqrst"

I want only first 10 characters be pasted and the last 10 be discarded..

Google search is unfortunately not helpful as everything there talks about NSTextField and not NSTextView.

Could someone please help?

Thank you.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.