Friday, June 14, 2013

NSCharacterSet symbolCharacterSet example in Objective C (iOS).


NSCharacterSet symbolCharacterSet

Returns a character set containing the characters in the category of Symbols.

+ (id)symbolCharacterSet

Return Value
A character set containing the characters in the category of Symbols.

Discussion of [NSCharacterSet symbolCharacterSet]
These characters include, for example, the dollar sign ($) and the plus (+) sign.

NSCharacterSet symbolCharacterSet example.
NSCharacterSet* symbols = [NSCharacterSet symbolCharacterSet];
   if([symbols characterIsMember: yourCharacter]) {
        //Check Your condition here
    }
If you want to include many characters Use a combined NSCharacterset with NSMutableCharacterSet..

    NSMutableCharacterSet *space = [NSMutableCharacterSet characterSetWithCharactersInString:@" "];
    [space formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];
Use the space characterset to check the character

Example of [NSCharacterSet symbolCharacterSet].
test if for invalid chars:

[NSCharacterSet symbolCharacterSet]
rangeOfCharacterFromSet == YES if the array contains a char from the set
to rm them all:

id s = @"$bla++$bleb+$blub-$b";
NSCharacterSet *ch = [NSCharacterSet symbolCharacterSet]
s = [[s componentsSeparatedByCharactersInSet:ch] componentsJoinedByString:@""];
NSLog(@"%@ - %@",ch, s);

NSCharacterSet symbolCharacterSet example.
 while ([s scanCharactersFromSet:[NSCharacterSet symbolCharacterSet] intoString:&read]) { //symbolCharacterSet
        for(int idx=0;idx<read.length;idx=idx+6) //For Some Reason "6" works with + sign and Emojis...

        {

            NSString *word=[read substringFromIndex:0];  //substringWithRange:NSMakeRange(idx, 2)]; <-- I switched out the range and decided to get the WHOLE string.
            CGSize s = [word sizeWithFont:self.font];
            if(drawPoint.x + s.width > rect.size.width) {
                drawPoint = CGPointMake(0, drawPoint.y + s.height);

            }

End of NSCharacterSet symbolCharacterSet example article.