Powered By Blogger

Tuesday, November 27, 2012

how to change the selected segment button color ?

Hi all,
Please use the below code if you want to change the color of UISegmentedControl's component after the selection of that rather showing the default color.

For example : 

UISegmentedControl *segmentedControl; // You can use your own segmented control rather using this one...

UIColor *selectedColor = [UIColor colorWithRed: 0.0f/255.0 green:0.0f/255.0 blue:255.0f/255.0 alpha:1.0];
         UIColor *deselectedColor = [UIColor colorWithRed: 0.0f/255.0 green: 0.0f/255.0 blue: 255.0/255.0 alpha:0.2];

for (id subview in [segmentedControl subviews]) {
    if ([subview isSelected])
       [subview setTintColor:selectedColor];
    else
       [subview setTintColor:deselectedColor];
}


Thanks,
Nilesh Prajapati

how to add gradient effect to button in iphone?

Hi friends,
If anyone, of you, want to add gradient effect to custom button then here is the way to add that effect.

Example :

        // Add Border to button
        CALayer *layer = btnCalender.layer;
        layer.cornerRadius = 5.0f;
        layer.masksToBounds = YES;
        layer.borderWidth = 1.0f;
        layer.borderColor = [UIColor colorWithWhite:0.3f alpha:0.2f].CGColor;
       
        // Add Shine to button
        CAGradientLayer *Layer = [CAGradientLayer layer];
        Layer = layer.bounds;
        Layer.colors = [NSArray arrayWithObjects:
                             (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                             (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                             (id)[UIColor colorWithWhite:0.75f alpha:0.2f].CGColor,
                             (id)[UIColor colorWithWhite:0.4f alpha:0.2f].CGColor,
                             (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                             nil];
        Layer.locations = [NSArray arrayWithObjects:
                                [NSNumber numberWithFloat:0.0f],
                                [NSNumber numberWithFloat:0.2f],
                                [NSNumber numberWithFloat:0.3f],
                                [NSNumber numberWithFloat:0.0f],
                                [NSNumber numberWithFloat:1.0f],
                                nil];
        [layer addSublayer: Layer];

Regards, 
Nilesh Prajapati

Saturday, November 24, 2012

how to use NSDateFormatter for different format?

 Hi,
Using the below category for NSDate, you can use below functions to get various date format string or date.

 #import <Foundation/Foundation.h>

@interface NSDate(TCUtils)

- (NSDate *)TC_dateByAddingCalendarUnits:(NSCalendarUnit)calendarUnit amount:(NSInteger)amount;
- (NSString*)TC_dateindisplayformat:(NSString*) format;
- (NSString*)TC_datewithTformat:(NSString*)dt format:(NSString*) format;
- (NSString*)TC_dateformat:(NSString*)dt format:(NSString*) format;
- (NSDate*)TC_stringformat:(NSString*)dt format:(NSString*) format;
- (NSString*)TC_date24format:(NSString*)dt format:(NSString*) format;
- (NSDate*)TC_datewithTformat:(NSString*)dt;
- (BOOL)isSameDay:(NSDate*)anotherDate;
- (NSDate *) todayDateWithoutTime;
@end



#import "NSDate+TCUtils.h"

@implementation NSDate (TCUtils)


- (NSDate *)TC_dateByAddingCalendarUnits:(NSCalendarUnit)calendarUnit amount:(NSInteger)amount {
    NSDateComponents *components = [[NSDateComponents alloc] init];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *newDate;
   
    switch (calendarUnit) {
        case NSSecondCalendarUnit:
            [components setSecond:amount];
            break;
        case NSMinuteCalendarUnit:
            [components setMinute:amount];
            break;
        case NSHourCalendarUnit:
            [components setHour:amount];
            break;
        case NSDayCalendarUnit:
            [components setDay:amount];
            break;
        case NSWeekCalendarUnit:
            [components setWeek:amount];
            break;
        case NSMonthCalendarUnit:
            [components setMonth:amount];
            break;
        case NSYearCalendarUnit:
            [components setYear:amount];
            break;
        default:
            ////NSLog(@"addCalendar does not support that calendarUnit!");
            break;
    }
   
    newDate = [gregorian dateByAddingComponents:components toDate:self options:0];
    [components release];
    [gregorian release];
    return newDate;
}

- (NSString*)TC_dateindisplayformat:(NSString*) format
{
    NSDateFormatter* df=[[[NSDateFormatter alloc] init] autorelease];
    [df setTimeZone:[NSTimeZone defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; 
    [df setLocale:locale];
    [df setDateFormat:format];
    return [df stringFromDate:self];   
}

- (NSString*)TC_datewithTformat:(NSString*)dt format:(NSString*) format
{
    NSDateFormatter* df=[[[NSDateFormatter alloc] init] autorelease];
    [df setTimeZone:[NSTimeZone defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; 
    [df setLocale:locale];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSArray* a=[dt componentsSeparatedByString:@"T"];
    NSDate* tdt;
    if([a count]>0){
        tdt=[df dateFromString:[a objectAtIndex:0]];
    }
    else{
        tdt=[NSDate date];
    }
    [df setDateFormat:format];
   
    return [df stringFromDate:tdt];
}

- (NSString*)TC_dateformat:(NSString*)dt format:(NSString*) format
{
    NSDateFormatter* df=[[[NSDateFormatter alloc] init] autorelease];
    [df setTimeZone:[NSTimeZone defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; 
    [df setLocale:locale];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate* tdt=[df dateFromString:dt];
    [df setDateFormat:format];
   
    return [df stringFromDate:tdt];
}

- (NSDate*)TC_stringformat:(NSString*)dt format:(NSString*) format
{
    NSDateFormatter* df=[[[NSDateFormatter alloc] init] autorelease];
    [df setTimeZone:[NSTimeZone defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; 
    [df setLocale:locale];
    [df setDateFormat:format];

    return [df dateFromString:dt];
}

- (NSDate*)TC_datewithTformat:(NSString*)dt
{
    NSDateFormatter* df=[[[NSDateFormatter alloc] init] autorelease];
    [df setTimeZone:[NSTimeZone defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
    [df setLocale:locale];
    dt=[dt stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [df dateFromString:dt];
}

- (NSString*)TC_date24format:(NSString*)dt format:(NSString*) format
{
    NSDateFormatter* df=[[[NSDateFormatter alloc] init] autorelease];
    [df setTimeZone:[NSTimeZone defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; 
    [df setLocale:locale];
    [df setDateFormat:@"yyyy-MM-dd hh:mm a"];
    NSDate* tdt=[df dateFromString:dt];
    [df setDateFormat:format];
   
    return [df stringFromDate:tdt];
}

- (NSDate *) todayDateWithoutTime
{
    NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
      [df setTimeZone:[NSTimeZone  defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
    [df setLocale:locale];
    [df setDateFormat:@"yyyy-MM-dd"];

    NSString *todayDateS = [df stringFromDate:[NSDate date]];
    NSDate *todayDate = [df dateFromString:todayDateS];
    return todayDate;
}



- (BOOL)isSameDay:(NSDate*)anotherDate{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components1 = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:self];
    NSDateComponents* components2 = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:anotherDate];
    return ([components1 year] == [components2 year] && [components1 month] == [components2 month] && [components1 day] == [components2 day]);
}

@end

Friday, November 23, 2012

how to add video url in webview as thumbnail in iphone ?

Hi friends,
This is the example for showing Video thumbnail icon in webview just as it appears in normal/youtube video list.

-(void)viewDidLoad
{
    UIWebView *webViewVideo = [[UIWebView alloc] initWithFrame:CGRectMake(0, (1024-500)/2, 768, 500)];
    [webViewVideo setBackgroundColor:[UIColor redColor]];
   
    UIButton* b=(UIButton*)sender;
   
    NSString* urlString=[array_Youtube objectAtIndex:b.tag];
   
    NSString *embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";
    NSString *html;
    html = [NSString stringWithFormat:embedHTML, urlString, 768.0, 500.0];
       
    [webViewVideo loadHTMLString:html  baseURL:nil];
    //UIView* v=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
    //[v addSubview:webViewVideo];
    //v.backgroundColor=[UIColor blackColor];
   
    [self.view addSubview:webViewVideo];
    [webViewVideo release];
}

 I hope this will be the useful to everyone of you.

Thanks,
Nilesh Prajapati