Sunday 28 December 2014

Swift + Get Height of Label Dynamically

func heightForLabel(text:String, font:UIFont, width:CGFloat) -> CGFloat
    {
        let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.ByWordWrapping
        label.font = font
        label.text = text
        
        label.sizeToFit()
        return label.frame.height

    }




let font = UIFont(name: "Verdana", size: 12)
        var detailHeight = heightForLabel("Your String", font: font!, width: self.scrollView.bounds.size.width-10)

Thursday 18 December 2014

Objective C + Calculate Height of Table's Cell Dynamically or At Run Time



// TableView Delegate Method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    int width = 300; // width of your label
    
    return ([Self calculateHeightOfString:@"Your String" withFont:[UIFont systemFontOfSize:15] ofWidth:width withLineBreak:NSLineBreakByWordWrapping]+30);
    

}




// Method to calculate height
-(float)calculateHeightOfString:(NSString *)str withFont:(UIFont *)font ofWidth:(float)width withLineBreak:(NSLineBreakMode)line
{
    //NSLog(@"%s",__PRETTY_FUNCTION__);
  
CGSize suggestedSize = [str sizeWithFont:font constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:line];

return suggestedSize.height;

}

Objective C + Open UIDatePicker with ActionSheet



// In .h file
UIDatePicker *datePicker;



[self showDatePicker: UIDatePickerModeDate];
                              or
[self showDatePicker: UIDatePickerModeTime];






// Method to show DatePicker on ActionSheet
-(void) showDatePicker: (UIDatePickerMode) modeDatePicker
{
    
    UIView *viewDatePicker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    [viewDatePicker setBackgroundColor:[UIColor clearColor]];
    
    //Make a frame for the picker & then create the picker
    CGRect pickerFrame = CGRectMake(0, 0, self.view.frame.size.width, 200);
    datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
    
    datePicker.datePickerMode = modeDatePicker;
    datePicker.hidden = NO;
    [viewDatePicker addSubview:datePicker];
    
    
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                                 message:@"\n\n\n\n\n\n\n\n\n\n"
                                                                          preferredStyle:UIAlertControllerStyleActionSheet];
        
        [alertController.view addSubview:viewDatePicker];
        
        
        
        UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                                     {
                                         //Detect particular click by tag and do some thing here

                                         if(tagDateField == 0 || tagDateField == 2)
                                         {
                                             [self setSelectedDateInField];
                                         }
                                         else if (tagDateField == 1 || tagDateField == 3)
                                         {
                                             [self setSelecteedTimeInField];
                                         }
                                         
                                         
                                         NSLog(@"OK action");
                                         
                                     }];
        [alertController addAction:doneAction];
        
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
                                       {
                                           NSLog(@"Cancel action");
                                       }];
        
        
        
        [alertController addAction:cancelAction];
        
        /*
         UIAlertAction *resetAction = [UIAlertAction
         actionWithTitle:NSLocalizedString(@"Reset", @"Reset action")
         style:UIAlertActionStyleDestructive
         handler:^(UIAlertAction *action)
         {
         NSLog(@"Reset action");
         }];
         
         [alertController addAction:resetAction];
         */
        
        
        [self presentViewController:alertController animated:YES completion:nil];
        
        
        
    }
    else
    {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Done" otherButtonTitles:nil, nil];
        [actionSheet addSubview:viewDatePicker];
        [actionSheet showInView:self.view];
        
    }
    
}



// Methods to set selected date and time in related field

-(void)setSelectedDateInField
{
    NSLog(@"date :: %@",datePicker.date.description);
    
    
    //set Date formatter
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    [formatter1 setDateFormat:@"MM/dd/YY"];
    
    if(tagDateField == 0)
    {
        [txtExpirationDate setText:[formatter1 stringFromDate:datePicker.date]];
        
    }
    else if (tagDateField == 2)
    {
        [txtBeginDate setText:[formatter1 stringFromDate:datePicker.date]];
    }
    
}


-(void)setSelecteedTimeInField
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"HH:mm"];
    
    if (tagDateField == 3)
    {
        [txtBeginTime setText:[dateFormatter stringFromDate:datePicker.date]];
    }
    else if (tagDateField == 1)
    {
        [txtExpirationTime setText:[dateFormatter stringFromDate:datePicker.date]];
    }

}









// Action sheet Delegate In case of iOS 7
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        if(tagDateField == 0 || tagDateField == 2)
        {
            [self setSelectedDateInField];
        }
        else if (tagDateField == 1 || tagDateField == 3)
        {
            [self setSelecteedTimeInField];
        }

    }
}