Advertisement
Guest User

UITextView line limit

a guest
Aug 3rd, 2011
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. NSInteger startChars = 0;
  2.  
  3. - (BOOL)textView:aView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  4. {    
  5.     NSInteger numLines;
  6.     numLines = tView.contentSize.height/tView.font.leading;
  7.    
  8.     if (numLines == 9)
  9.     {
  10.         if (startChars == 0)
  11.         {
  12.             startChars = tView.text.length;
  13.         }
  14.        
  15.         NSLog(@"StartChars: %d", startChars);
  16.        
  17.         NSString *lastLine = [[NSString alloc]initWithString:[[tView text] substringFromIndex:startChars]];
  18.         CGSize lineSize = [lastLine sizeWithFont:tView.font forWidth:tView.contentSize.width lineBreakMode:UILineBreakModeWordWrap];
  19.         [lastLine release];
  20.        
  21.         if (range.length > text.length)
  22.         {
  23.             return YES;
  24.         }
  25.         else if (numLines == 9 && lineSize.width >= tView.contentSize.width - 45)
  26.         {
  27.             return NO;
  28.         }
  29.     }
  30.     else
  31.     {
  32.         startChars = 0;
  33.     }
  34.    
  35.     return YES;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement