UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(originX, originY, width, height)];
textView.backgroundColor = [UIColor whiteColor];
textView.font = [UIFont systemFontOfSize:12];
textView.placeholderColor = [UIColor redColor];
textView.placeholderLabel.font = [UIFont systemFontOfSize:12];
textView.textColor = [UIColor redColor];
textView.placeholder = @"請(qǐng)輸入...";
textView.delegate = self;
[self.view addSubview:textView];
//設(shè)置textview是否可編輯
@property (assign, nonatomic) BOOL isContentTextViewEnable;
#pragma mark textview delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@""]) {
return YES;
}else{
return self.isContentTextViewEnable;
}
}
- (void)textViewDidChange:(UITextView *)textView {
NSDictionary *attibute = @{
@"body":@[[UIFont systemFontOfSize:11],[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]],
@"num":[UIColor colorWithRed:255/255.0 green:80/255.0 blue:11/255.0 alpha:1.0f]
};
self.isContentTextViewEnable = true;
//獲取當(dāng)前鍵盤類型
UITextInputMode *mode = (UITextInputMode *)[UITextInputMode activeInputModes][0];
//獲取當(dāng)前鍵盤語言
NSString *lang = mode.primaryLanguage;
//如果語言是漢語(拼音)
if ([lang isEqualToString:@"zh-Hans"]) {
//取到高亮部分范圍
UITextRange *selectedRange = [self.clubIntroTextView markedTextRange];
//取到高亮部分
UITextPosition *position = [self.clubIntroTextView positionFromPosition:selectedRange.start offset:0];
//如果取不到高亮部分,代表沒有拼音
if (!position){
//當(dāng)超過最大字?jǐn)?shù)限制時(shí)
if (self.clubIntroTextView.text.length > 48) {
//對(duì)超出部分進(jìn)行裁剪
self.clubIntroTextView.text = [self.clubIntroTextView.text substringToIndex:48];
//同時(shí)將可繼續(xù)書寫屬性設(shè)為否,shouldChangeTextInRange方法會(huì)調(diào)用
self.isContentTextViewEnable = NO; ? ? ? ? ??
}
}else {
//表示還有高亮部分卡睦,暫不處理
}
}else {
//如果語言不是漢語,直接計(jì)算
if (self.clubIntroTextView.text.length > 48) {
self.clubIntroTextView.text = [self.clubIntroTextView.text substringToIndex:48];
self.isContentTextViewEnable = NO;
}
}
}
//如果需要配合鍵盤移動(dòng),不需要忽略
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//監(jiān)聽textfield輸入文字的個(gè)數(shù)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:)
name:@"UITextFieldTextDidChangeNotification" object:self.clubNameTextField];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark 鍵盤的通知
- (void)keyboardWillShow:(NSNotification *)noti {
NSValue *frameValue = [dict valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect frame = [frameValue CGRectValue];
//獲取彈鍵盤前的偏移量
self.offsetY = self.tableView.contentOffset.y;
CGFloat height = frame.size.height;
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, height, 0);
//將tableview滾動(dòng)到textfield對(duì)應(yīng)的行
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:7 inSection:0]? atScrollPosition:UITableViewScrollPositionBottom animated:NO];//這里一定要設(shè)置為NO均牢,動(dòng)畫可能會(huì)影響到scrollerView,導(dǎo)致增加數(shù)據(jù)源之后,tableView到處亂跳
}
- (void)keyboardWillHide:(NSNotification *)noti {
self.tableView.contentInset = UIEdgeInsetsZero;
}