Tuesday, October 20, 2015

Misc. Objective-c code blocks

Adding arrow to the table view cell

cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;


change the back color of navigation controller:

self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
self.navigationController.navigationBar.tintColor = [UIColor blueColor];

HexColor to UIColor

Add the following macro to predefine.pch file
#define UIColorFromHexColor(rgbValue) [UIColor colorWith 
Red:((float)((rgbValue & 0xFF0000) >> 16))/255.0 
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

call it in your m file like this

label.textColor=UIColorFromHexColor(0xFBAE08);
label.text = @"Some Text";

That is it .
Advantage of adding in the pch file is that it is available to the whole project.


Hide Status bar xcode

Here is how to do it.
in plist File add the following.

Status bar is initially hidden -->  YES
View controller-based status bar appearance --> NO

If you want to remove the navigation controller as well like in my case please do the following.

In the first screen or the Load screen add the
self.navigationController.navigationBar.hidden=true;

NSDate to NSString and NSString to NSDate

 
-(NSDate*) getDateFromString: (NSString*)dtStr{
   NSDateFormatter* dateFrmt = [[NSDateFormatter alloc]init];
    [dateFrmt setDateFormat: @"mm/dd/yyyy HH:mm:ss"];
    return [dateFrmt dateFromString:dtStr];
}

-(NSString*)getStringFromNSDate: (NSDate*)dt{
    NSDateFormatter* dateFrmt=[[NSDateFormatter alloc]init];
    [dateFrmt setDateFormat:@"mm/dd/yyyy HH:mm:ss"];
    return [dateFrmt stringFromDate:dt];
}

Create a new GUID Xcode:-

- (NSString *)generateUID
{
   CFUUIDRef guidRef= CFUUIDCreate(NULL);
   CFStringRef guidRefinStringRef= CFUUIDCreateString(NULL, guidRef);
   CFRelease(guidRef);
   return [(NSString *)guidRefinStringRef autorelease];
}

code to check for email using regular expression 



- (BOOL) checkIfStringisEmailusingRegEx: (NSString *) emailString {

    NSString *regExForEmail =

    @"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
    @"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
    @"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
    @"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
    @"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
    @"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
    @"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
    NSPredicate *isEmail = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", 
regExForEmail];
    
    return [isEmail evaluateWithObject:emailString];
}



Thanks,
Bobby :)

No comments:

Post a Comment