Thursday 20 February 2014

Text field accept user required characters only

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {

    NSString *acceptedCharacters=@""; // only these characters  will be allowed to be displayed in text filed.
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:acceptedCharacters] invertedSet];

    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

    return [string isEqualToString:filtered];
}

Text field accept only numbers (Max. 10 numbers)

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    if (textField.keyboardType == UIKeyboardTypeNumberPad)
    {
        if (!(textField.text.length<10||[string isEqualToString:@""])||[string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound)
        {
            

            return NO;
        }
    }

    return YES;
}

Sunday 16 February 2014

Spinner (UIActivityIndicatorView) at the Center of UITableView

spinner.center = CGPointMake( [UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
yourAppdelegateClass *appDelegate = (yourAppdelegateClass*)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:spinner];

Thursday 13 February 2014

UITableViewCell with dynamic height

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        //This code for Tableview with basic type Cell
        CGFloat defaultRowHeight=44;
        NSString *cellText; //Give cell text here from the data source Array of TableView
        UIFont *cellFont ;  //Give cell font here 
        CGSize constraintSize = CGSizeMake(220, MAXFLOAT);
        CGSize labelSize = [cellText boundingRectWithSize:constraintSize
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:cellFont}context:nil].size;
        CGFloat rowHeight =  labelSize.height;
        return   MAX(rowHeight, defaultRowHeight);
    }

Wednesday 5 February 2014

Flipping Views inside a View controller

- (IBAction)flipViews:(UIButton *)sender {


    if ([self.view.subviews indexOfObject:view2]>[self.view.subviews indexOfObject:view1]) {

        [UIView transitionWithView:view1
                      duration:1
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                       [self.view exchangeSubviewAtIndex:[self.view.subviews indexOfObject:view1] withSubviewAtIndex:[self.view.subviews indexOfObject:view2]];
                    } completion:^(BOOL finished) {

                    }];
    }else{

        [UIView transitionWithView:view2
                          duration:1
                           options:UIViewAnimationOptionTransitionFlipFromRight
                        animations:^{
                            [self.view exchangeSubviewAtIndex:[self.view.subviews indexOfObject:view2] withSubviewAtIndex:[self.view.subviews indexOfObject:view1]];
                        } completion:^(BOOL finished) {

                        }];
    }


}
Sample Code