iOS 开发中实用小技巧(2)

NSCharacterSet的使用详解

    [NSCharacterSet alphanumericCharacterSet];          //所有数字和字母(大小写)
    [NSCharacterSet decimalDigitCharacterSet];          //0-9的数字
    [NSCharacterSet letterCharacterSet];                //所有字母
    [NSCharacterSet lowercaseLetterCharacterSet];       //小写字母
    [NSCharacterSet uppercaseLetterCharacterSet];       //大写字母
    [NSCharacterSet punctuationCharacterSet];           //标点符号
    [NSCharacterSet whitespaceAndNewlineCharacterSet];  //空格和换行符
    [NSCharacterSet whitespaceCharacterSet];            //空格

NSCharacterSet中的常用方法或属性


//指定字符集是包含于在于当前字符集
- (BOOL)characterIsMember:(unichar)aCharacter;
//以二进制格式编码接收器的NSData对象,此格式适用于保存到文件或以其他方式传输或归档
@property (readonly, copy) NSData *bitmapRepresentation;
//反转字符集,仅包含当前字符集中不存在的字符
@property (readonly, copy) NSCharacterSet *invertedSet;

延伸

//返回一个指定字符集分隔开的子字符串数组
- (NSArray<NSString *> *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator NS_AVAILABLE(10_5, 2_0);
//返回一个去除两端指定字符集的字符串
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;
//返回指定字符集在当前字符串中的第一个符合条件的范围
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet;
//在数组中子串之间插入指定字符
- (NSString *)componentsJoinedByString:(NSString *)separator;

举例使用

1. NSString *testString = @"This is the test string for %a*b*c&";
 NSArray *divArr = [testString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"abc"]];
  NSLog(@"%@",divArr);

打印结果:
(
    "This is the test string for %",
    "*",
    "*",
    "&"
)

2.去掉首尾空格
NSString *testString = @"      This is the string contains whitespace in beginning and ending     ";
 NSString *whitesspaceStr = [testString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
   NSLog(@"%@",whitesspaceStr);

3.用指定字符串替代当前字符中的指定字符集中的字符串

   NSMutableCharacterSet *letter = [NSMutableCharacterSet lowercaseLetterCharacterSet];
    NSCharacterSet *decimalDigit = [NSCharacterSet decimalDigitCharacterSet];
    [letter formUnionWithCharacterSet:decimalDigit];
    NSString *string = @"g8!hgr3@09#23uiq%^78sjn453t78&13gesg*wt53(545y45)q3at";
    NSLog(@"%@",[[string componentsSeparatedByCharactersInSet:letter] componentsJoinedByString:@"_"]);
    [letter invert];  //字母数字反转
    NSLog(@"%@",[[string componentsSeparatedByCharactersInSet:letter] componentsJoinedByString:@"_"]);


UITextField的字数限制


-(void)textFieldEditChanged:(NSNotification *)obj{
    
    UITextField *textField = (UITextField *)obj.object;
    NSString *toBeString = textField.text;
    
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    
    // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
    if (!position || !selectedRange)
    {
        if (toBeString.length > maxLength)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:maxLength];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}


推荐阅读更多精彩内容