Thursday, April 20, 2017

Utility class for Custom Items in ios

#import Foundation/Foundation.h
#import UIKit/UIKit.h

@interface Util : NSObject
{
    NSString *selectView;
    BOOL isPrepaidCardFlag;
}
-(UILabel *)createLabel:(NSString *)placeholder  x:(float)x y:(float)y l_Width:(float)l_Width l_Height:(float)l_Height l_Color:(UIColor *)l_Color l_Font:(UIFont *)l_Font l_NSTextAlignment:(int)l_NSTextAlignment;
-(UITextField *) createField:(NSString *)placeholder tag:(int)tag x:(float)x y:(float)y t_Width:(float)t_Width t_Height:(float)t_Height t_Color:(UIColor *)t_Color t_Font:(UIFont *)t_Font t_NSTextAlignment:(int)t_NSTextAlignment;
-(void)alertViewShow : (NSString *)_msg withTag:(int)_tag;
- (void)showPopupWithTitle:(NSString *)title
                    mesage:(NSString *)message
              dismissAfter:(NSTimeInterval)interval className:(NSString *)className;
-(NSString *) randomString;
-(CALayer *) borderLine:(float)width height:(float)height;
-(UIView *) buttonLine:(float)x y:(float)y width:(float)width height:(float)height;
- (void)showPopupWithTitleDismis:(NSString *)title
                          mesage:(NSString *)message
                    dismissAfter:(NSTimeInterval)interval className:(NSString *)className;
-(UITextField *) padding:(UITextField *)placeholder t_Height:(float)t_Height;

-(UITextField *) createFieldLightGray:(NSString *)placeholder tag:(int)tag x:(float)x y:(float)y t_Width:(float)t_Width t_Height:(float)t_Height t_Color:(UIColor *)t_Color t_Font:(UIFont *)t_Font t_NSTextAlignment:(int)t_NSTextAlignment;
-(UITextField *) createField2:(NSString *)placeholder tag:(int)tag x:(float)x y:(float)y t_Width:(float)t_Width t_Height:(float)t_Height t_Color:(UIColor *)t_Color t_Font:(UIFont *)t_Font t_NSTextAlignment:(int)t_NSTextAlignment;
@end











#import "Util.h"
#import "UIColor+customColor.h"
#import "Constants.h"
@implementation Util

-(UILabel *)createLabel:(NSString *)placeholder  x:(float)x y:(float)y l_Width:(float)l_Width l_Height:(float)l_Height l_Color:(UIColor *)l_Color l_Font:(UIFont *)l_Font l_NSTextAlignment:(int)l_NSTextAlignment{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, l_Width, l_Height)];
    [label setText:placeholder];
    [label setTextColor:l_Color];
    [label setTextAlignment:l_NSTextAlignment];
    [label setFont:l_Font];
    label.backgroundColor = [UIColor clearColor];
    return label;
}

-(UITextField *) createField:(NSString *)placeholder tag:(int)tag x:(float)x y:(float)y t_Width:(float)t_Width t_Height:(float)t_Height t_Color:(UIColor *)t_Color t_Font:(UIFont *)t_Font t_NSTextAlignment:(int)t_NSTextAlignment
{
    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(x, y, t_Width, t_Height)];
    field.textAlignment = t_NSTextAlignment;
    field.font = [UIFont systemFontOfSize:18];
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
    //field.backgroundColor = [UIColor whiteColor];
    field.tag = tag;
    UIColor *color = [UIColor colorWithHexString:STRING_GreenWholesaleColor];
    field.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: color}];
    [field setKeyboardType:UIKeyboardTypeDefault];
    field.textColor = [UIColor colorWithHexString:STRING_GreenDarkColor];

    CALayer *border = [CALayer layer];
    CGFloat borderWidth = 2;
    border.borderColor = [UIColor colorWithHexString:STRING_GreenDarkColor].CGColor;
    border.frame = CGRectMake(0, field.frame.size.height - borderWidth, field.frame.size.width, field.frame.size.height);
    border.borderWidth = borderWidth;
    [field.layer addSublayer:border];
    field.layer.masksToBounds = YES;

    return field;
}

-(UITextField *) createField2:(NSString *)placeholder tag:(int)tag x:(float)x y:(float)y t_Width:(float)t_Width t_Height:(float)t_Height t_Color:(UIColor *)t_Color t_Font:(UIFont *)t_Font t_NSTextAlignment:(int)t_NSTextAlignment
{
    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(x, y, t_Width, t_Height)];
    field.textAlignment = t_NSTextAlignment;
    field.font = [UIFont systemFontOfSize:18];
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
    //field.backgroundColor = [UIColor whiteColor];
    field.tag = tag;
    UIColor *color = [UIColor colorWithHexString:STRING_GreenWholesaleColor];
    field.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: color, NSFontAttributeName: [UIFont systemFontOfSize:13]}];
    [field setKeyboardType:UIKeyboardTypeDefault];
    field.textColor = [UIColor colorWithHexString:STRING_GreenDarkColor];
   
    CALayer *border = [CALayer layer];
    CGFloat borderWidth = 2;
    border.borderColor = [UIColor colorWithHexString:STRING_GreenDarkColor].CGColor;
    border.frame = CGRectMake(0, field.frame.size.height - borderWidth, field.frame.size.width, field.frame.size.height);
    border.borderWidth = borderWidth;
    [field.layer addSublayer:border];
    field.layer.masksToBounds = YES;
   
    return field;
}


-(UITextField *) createFieldLightGray:(NSString *)placeholder tag:(int)tag x:(float)x y:(float)y t_Width:(float)t_Width t_Height:(float)t_Height t_Color:(UIColor *)t_Color t_Font:(UIFont *)t_Font t_NSTextAlignment:(int)t_NSTextAlignment
{
    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(x, y, t_Width, t_Height)];
    field.textAlignment = t_NSTextAlignment;
    field.font = [UIFont systemFontOfSize:18];
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
    //field.backgroundColor = [UIColor whiteColor];
    field.tag = tag;
    UIColor *color = [UIColor lightGrayColor];
    field.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: color}];
    [field setKeyboardType:UIKeyboardTypeDefault];
    field.textColor = [UIColor blackColor];
   
    CALayer *border = [CALayer layer];
    CGFloat borderWidth = 2;
    border.borderColor = [UIColor lightGrayColor].CGColor;
    border.frame = CGRectMake(0, field.frame.size.height - borderWidth, field.frame.size.width, field.frame.size.height);
    border.borderWidth = borderWidth;
    [field.layer addSublayer:border];
    field.layer.masksToBounds = YES;
   
    return field;
}
-(UILabel *)createLabel1:(NSString *)placeholder  x:(float)x y:(float)y l_Width:(float)l_Width l_Height:(float)l_Height l_Color:(UIColor *)l_Color l_Font:(UIFont *)l_Font l_NSTextAlignment:(int)l_NSTextAlignment{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, l_Width, l_Height)];
    [label setText:placeholder];
    [label setTextColor:l_Color];
    [label setTextAlignment:l_NSTextAlignment];
    [label setFont:l_Font];
    label.backgroundColor = [UIColor clearColor];
    return label;
}
-(UITextField *) padding:(UITextField *)placeholder t_Height:(float)t_Height{
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, t_Height)];
    placeholder.leftView = paddingView;
    placeholder.leftViewMode = UITextFieldViewModeAlways;
    return placeholder;
}
-(void)alertViewShow : (NSString *)_msg withTag:(int)_tag {
//    UIAlertController * alert=   [UIAlertController
//                                  alertControllerWithTitle:STRING_AppName
//                                  message:_msg
//                                  preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//     }];
//    [alert addAction:okAction];
//    UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
//    [vc presentViewController:alert animated:YES completion:nil];
   
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:STRING_AppName
                                                                    message:_msg
                                                                   delegate:self
                                                          cancelButtonTitle:@"Ok"
                                                          otherButtonTitles:nil, nil];
   
                [alertView show];
   
   
}

-(NSString *) randomString {
    NSMutableString *randomString = [NSMutableString stringWithCapacity: 10];
    for (int i=0; i<10 br="" i="">        [randomString appendFormat: @"%C", [@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" characterAtIndex: arc4random_uniform(36)]];
    }
    return randomString;
}


@end






UIButton and UITextField in ios

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Button" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
 
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
textField.delegate = self;
[self.view addSubview:textField];
[textField release]; 

PickerView in ios

_pickerData = @[@"Item 1", @"Item 2", @"Item 3", @"Item 4", @"Item 5", @"Item 6"];

  picker=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 200)];
    picker.dataSource = self;
    picker.delegate = self;
    [self.view addSubview:picker];

// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return _pickerData[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
  //  [picker removeFromSuperview];


//array object at index row will get the selected value from array
}

CoreData in ios

#import "AppDelegate.h"

Allocation -
- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}


Adding Data -
 NSManagedObjectContext *context = [self managedObjectContext];
   
    // Create a new managed object
    NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];
    [newDevice setValue:@"1" forKey:@"id"];
    [newDevice setValue:@"Rince" forKey:@"name"];
   
   
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }

Display Data -
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@",[device valueForKey:@"id"], [device valueForKey:@"name"]]];