只允許輸入數(shù)字
TextField只允許輸入數(shù)字
只允許輸入數(shù)字和小數(shù)點(diǎn)
代碼實(shí)現(xiàn)
-(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;
}
限制位數(shù)
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag == 1000) {
//控制字?jǐn)?shù)限制在18位熬芜,多余位數(shù)無(wú)法輸入
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 輸入范圍縮進(jìn)
textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
參考文章
iOS開(kāi)發(fā)-UITextView高度自適應(yīng)
UITextView高度計(jì)算不正確
====================================================
UITextView行間距與字體設(shè)置
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, 100, 200)];
// ……此處可進(jìn)行其它設(shè)置 ……
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 5;// 字體的行間距
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:17], //字體設(shè)置
NSParagraphStyleAttributeName:paragraphStyle
};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];