1.判断全汉字(这个问题苦恼了我很久,现在很方便,几行代码就搞定):
if ([self deptNameInputShouldChinese]) {
[DemonAlertHelper showToastWithMessage:@"只能是中文"];
return;
}
调用这个方法就可
#pragma mark--
#pragma mark 输入中文
- (BOOL) deptNameInputShouldChinese
{
NSString *regex = @"[\u4e00-\u9fa5]+";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
if (![pred evaluateWithObject:inputTextField.text]) {
return YES;
}
return NO;
}
2.判断全数字:
- (BOOL) deptNumInputShouldNumber
{
NSString *regex =@"[0-9]*";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
if (![pred evaluateWithObject:inputTextField.text]) {
return YES;
}
return NO;
}
判断全字母:
- (BOOL) deptPassInputShouldAlpha
{
NSString *regex =@"[a-zA-Z]*";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
if (![pred evaluateWithObject:inputTextField.text]) {
return YES;
}
return NO;
}
3.判断仅输入字母或数字:
- (BOOL) deptIdInputShouldAlphaNum
{
NSString *regex =@"[a-zA-Z0-9]*";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
if (![pred evaluateWithObject:inputTextField.text]) {
return YES;
}
return NO;
}
4.//*改变字符串中所有数字的颜色
- (void)setRichNumberWithLabel:(UILabel*)label Color:(UIColor *)color FontSize:(CGFloat)size {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
NSString *temp = nil;
for(int i =0; i < [attributedString length]; i++) {
temp = [label.text substringWithRange:NSMakeRange(i, 1)];
if ([self isPureInt:temp]) {
[attributedString setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
color, NSForegroundColorAttributeName,
[UIFont systemFontOfSize:size],NSFontAttributeName, nil ,nil]
range:NSMakeRange(i, 1)];
}
}
label.attributedText = attributedString;
}
5.一段字符串的处理
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:kInstructions];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//行间距
[paragraphStyle setLineSpacing:5.0];
//段落间距
[paragraphStyle setParagraphSpacing:5.0];
//第一行头缩进
[paragraphStyle setFirstLineHeadIndent:5.0];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [kInstructions length])];
[conentLbl setAttributedText:attributedString];
CGRect rect = [conentLbl.text boundingRectWithSize:CGSizeMake(ScreenWidth, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
6.改变字符窜中某些字符的颜色大小
#define kString @"1. 收到仿佛看见伤口方式 是是一致\n2. 是否是否守空房\n3. 严禁福建师范就死哦噢诶死附近反复\n4. 佛首付款司法考试浪费;了\n5. 佛寺福建师范色IE二恶烷款
NSArray *ary=@[@"一致",@"严禁"];
NSMutableAttributedString *mAttStri = [[NSMutableAttributedString alloc] initWithString:kString];
for (NSInteger i=0; i<6; i++) {
NSString *stringForColor = ary[i];
NSRange range = [kString rangeOfString:stringForColor];
[mAttStri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
[mAttStri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:range];
}
lblMsg.attributedText =mAttStri;
}
7.获取字符串的首字符和最后一个字符
NSString *str = @"ifosfdfkosdfisfjksflsfksksf";
NSString *first = [str substringToIndex:1];//第一个字符
NSString *last = [str substringFromIndex:str.length-1];//最后一个字符
网友评论