[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChangeValue:) name: UITextFieldTextDidChangeNotification object:self.nickNameField];
- (void)textFieldDidChangeValue:(NSNotification *)notification
{
UITextField *textField = (UITextField *)[notification object];
if (textField.text.length < 2) {
_sure_btn.backgroundColor = [UIColor grayColor];
_sure_btn.userInteractionEnabled = NO;
}else{
_sure_btn.backgroundColor = [UIColor MyLanse];
_sure_btn.userInteractionEnabled = YES;
};
NSString *toBeString = textField.text;
//获取高亮部分
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position)
{
if (toBeString.length > MAX_STARWORDS_LENGTH)
{
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];
if (rangeIndex.length == 1)
{
textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];
}
else
{
NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];
textField.text = [toBeString substringWithRange:rangeRange];
}
}
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
网友评论