Saturday, 14 July 2012

Check part of String is present in NSString

Below is code snippet to check whether part of string is present in a provided NSString



 NSString *myString = @"This is an APPLE";
 if ([ myString  rangeOfString:@"APPLE"].location != NSNotFound)
   {
        NSLog(@"Found");
   }
   else
   {
        NSLog(@"Not Found");
   }

Find URL from NSString

If you want to fetch a URL from a String:



NSString *StringContainingURL = @"You can click http://objectivecwithsuraj.blogspot.in/";
NSDataDetector *Detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [Detector matchesInString: StringContainingURL  options:0 range:NSMakeRange(0, [StringContainingURL  length])];
        for (NSTextCheckingResult *match in matches)
        {
            if ([match resultType] == NSTextCheckingTypeLink)
            {
                NSURL *url = [match URL];
                NSLog(@"URL : %@",url); 
            }
        }
   

Output of the log is:

Detect Touch on UILabel


In one situation i want to detect touch on my Label, so i want to share the code :

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITapGestureRecognizer *taplabelGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureTap)];
    UILabel *label = @"Suraj Mirajkar";
    [label setFrame: CGRectMake(5o, 50, 100, 80)];
    [self.view addSubview: label];
    label .userInteractionEnabled = YES;
    [label  addGestureRecognizer:taplabelGesture];
    [taplabelGesture release];
}
-(void) gestureTap
{
   NSLog("Hey, you detected touch on Label");
}

Remove part of string between two strings

In below sample i have removed string between two angular brackets i.e. between < & >

I want the strings between the angular brackets so,

-(void) FetchString
{
    NSString  *descriptionString = @"<hi this is> Suraj <and this is my> blog";
    NSString  *outputString        = [self removeNodesFromXMLString: descriptionString ];
    NSLog(@"outputString is :- %@", outputString);
}

-(NSString *) removeNodesFromXMLString: (NSString *) descriptionString
{
    NSMutableString *resultString = [descriptionString mutableCopy];
   
    NSRegularExpression *regex = [NSRegularExpression        
                                  regularExpressionWithPattern:@"\\<.+?\\>"
                                  options:NSRegularExpressionCaseInsensitive
                                  error:NULL];
   
    [regex replaceMatchesInString:resultString
                          options:0
                            range:NSMakeRange(0, [resultString length]) 
                     withTemplate:@" "];
   
    return resultString;
}


When my FetchString method is executed i will get log:
outputString is :-Suraj blog

and that's it i am done.

MultiLine UILabel


In many cases we need a label with multi line. 
For doing this you need to set 2 properties :
1. Number of Lines - 0, it means any number of lines.
2. Line break Mode - WordWrap


CGSize labelsize;
UILabel *TextLabel = [[UILabel alloc] init];
[TextLabel setNumberOfLines:0];
[TextLabel setBackgroundColor:[UIColor clearColor]];
NSString *text=@"yourtextString";
[TextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]];
labelsize=[text sizeWithFont:TextLabel.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
TextLabel.frame=CGRectMake(10, 24, 268, labelsize.height);
[cell.contentView addSubview:TextLabel];
[TextLabel release];