只允许输入数字
代码实现
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return [self validateNumberByRegExp:string andTextFieldType:textField.tag] ;
}
- (BOOL)validateNumberByRegExp:(NSString *)string andTextFieldType:(NSInteger)type {
BOOL isValid = YES;
NSUInteger len = string.length;
if (len > 0) {
NSString *numberRegex = (type == textFieldMoneyType) ? @"^[0-9]*((\\.|,)[0-9]{0,2})?$" :@"^[0-9]*$";
NSPredicate *numberPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numberRegex];
isValid = [numberPredicate evaluateWithObject:string];
}
return isValid;
}
限制位数
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag == 1000) {
//控制字数限制在18位,多余位数无法输入
if (range.location >= 18)
{
if ([string isEqualToString:@"\n"])
{
[textField resignFirstResponder];
return NO;
}
return NO;
}
}
if ([string isEqualToString:@"\n"])
{
[textField resignFirstResponder];
return NO;
}
return YES;
}
===================================================
UITextView 输入范围缩进
textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
参考文章
iOS开发-UITextView高度自适应
UITextView高度计算不正确
====================================================
UITextView行间距与字体设置
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, 100, 200)];
// ……此处可进行其它设置 ……
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 5;// 字体的行间距
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:17], //字体设置
NSParagraphStyleAttributeName:paragraphStyle
};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
参考文章
iOS之UITextView设置行间距