Wednesday, 18 September 2013

clear uitextfield

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if ([textField.text isEqualToString:@""] || [[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
    {
        [textField setText:@""];
        
    }    

}

Detect backspace in UITextField

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (range.length > 0 && [text isEqualToString:@""]) {
        NSLog(@"press Backspace.");
    }
    return YES;
}
(OR)
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([string isEqualToString:@""]) {
        return YES;
    }
}

Wednesday, 11 September 2013

UIDatePicker

.h file

@interface ViewController : UIViewController
{
    IBOutlet UIDatePicker *datePicker;
    IBOutlet UILabel *lblResult;

}

.m file

- (void)viewDidLoad
{
    [super viewDidLoad];
      datePicker.date = [NSDate date];    
    [datePicker addTarget:self
              action:@selector(datechange:)
    forControlEvents:UIControlEventValueChanged];
}

- (void)datechange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];

//[df setDateFormat:@"yyyy-MM-dd"];
   df.dateStyle = NSDateFormatterMediumStyle;
lblResult.text = [NSString stringWithFormat:@"%@",
                      [df stringFromDate:datePicker.date]];
}

Monday, 2 September 2013

Removing the image on the left of an UISearchbar

.h file
@interface Search : UIViewController<UISearchBarDelegate>
{
IBOutlet UISearchBar *searchBar;
}
.m file
@interface Search (Private)
- (void)setSearchIconToFavicon;
@end
@implementation Search
- (void)viewDidLoad {
[self setSearchIconToFavicon];
[super viewDidLoad];
}
#pragma mark Private
- (void)setSearchIconToFavicon {
// Really a UISearchBarTextField, but the header is private.
UITextField *searchField = nil;
for (UIView *subview in searchBar.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
if (searchField) {
UIImage *image = [UIImage imageNamed: @"favicon.png"];
UIImageView *iView = [[UIImageView alloc] initWithImage:image];
searchField.leftView = iView;
[iView release];
}
} 
@end

change UISearchBar from round to rectangle

  for (UIView *searchBarSubview in [MySearchbarName subviews]) {
        if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
            @try {
                
                [(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleRoundedRect];
                [(UITextField *)searchBarSubview setBackgroundColor:[UIColor blackColor]];

                [(UITextField *)searchBarSubview setTextColor:[UIColor whiteColor]];
            }
            @catch (NSException * e) {
                // ignore exception
            }
        }
    }