textview 限制

来源:互联网 发布:淘宝手机写试用报告 编辑:程序博客网 时间:2024/06/04 19:27
 关闭联想功能 textField.autocorrectionType=UITextAutocorrectionTypeNo; -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{    NSInteger count = [self unicodeLengthOfString:textView.text];    NSInteger max = 31;    //frame change    NSDictionary *textDict = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};    CGRect textFrame = [textView.text textRectWithSize:CGSizeMake(([UIScreen mainScreen].bounds.size.width)-10, MAXFLOAT) attributes:textDict];    self.inputH.constant = textFrame.size.height+13.3;    if (self.toolbar.locationlbtn.selected)    {        max = 24;    }    self.Remains = max-count;       if (count +[self unicodeLengthOfString:text] >= max)    {        //1.让 max == count 的 count 推回去字符串的长度//        textView.text = [textView.text substringToIndex:textView.text.length-1];        return NO;    }//    char *p = [textView.text cStringUsingEncoding:NSASCIIStringEncoding];    NSUInteger bytes = [textView.text lengthOfBytesUsingEncoding:NSASCIIStringEncoding];    NSLog(@"%lu bytes", (unsigned long)bytes);    NSLog(@"字节----,count%ld,length%ld",(long)count,textView.text.length);    // string text    NSString *stt = [NSString stringWithFormat:@"%ld",self.Remains];    NSMutableAttributedString *s = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"你还可以输入%@字数",stt]];    [s addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,stt.length)];    self.toolbar.countl.attributedText = s;       return YES;}    /*由于联想输入的时候,函数textView:shouldChangeTextInRange:replacementText:无法判断字数,      因此使用textViewDidChange对TextView里面的字数进行判断      */      - (void)textViewDidChange:(UITextView *)textView      {          //该判断用于联想输入          if (textView.text.length > BOOKMARK_WORD_LIMIT)          {              textView.text = [textView.text substringToIndex:BOOKMARK_WORD_LIMIT];          }      } 吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊//记录等于24的字符串长度    NSUInteger len = 0;    if ([self unicodeLengthOfString:textView.text]+[self unicodeLengthOfString:text]== 24)    {        len =textView.text.length;    }    if (self.toolbar.locationlbtn.selected && [self unicodeLengthOfString:textView.text] > 24 )    {        for (int i=0; i<[textView.text length]; i++) {            NSString *str = textView.text;            [str  substringToIndex:textView.text.length-i];            if (24 == [self  unicodeLengthOfString:str])            {              len =  [str length];            }        }        [textView.text substringToIndex:len];//        NSLog(@"substringtoindex  %lu",(unsigned long)len);    }吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊吊----------//记录等于24的字符串长度-(void)IslocSelAndBeyondText{    if (self.toolbar.locationlbtn.selected && [self unicodeLengthOfString:self.toolbar.input.text] > 24 )    {        NSString *strB = @"";        for (int i=0; i<[self.toolbar.input.text length]; i++)        {            strB = self.toolbar.input.text;            strB = [strB  substringToIndex:strB.length-i];            if (24 == [self unicodeLengthOfString:strB])            {                self.toolbar.input.text = strB;                return;            }        }        NSLog(@"24 -- %@---- textview%@",self.toolbar.input.text,strB);    }}//限制字节-(NSUInteger) unicodeLengthOfString: (NSString *) text {    NSUInteger asciiLength = 0;    for (NSUInteger i = 0; i < text.length; i++)    {        unichar uc = [text characterAtIndex: i];        asciiLength += isascii(uc) ? 1 : 2;    }    NSUInteger unicodeLength = asciiLength / 2;    if(asciiLength % 2) {        unicodeLength++;    }    return unicodeLength;}
0 0
原创粉丝点击