Pages

Sunday 27 July 2014

ios: Rating Star


Introduction

This post contains a coding that describes the displaying of the static rating star. It is very simple but very effective.

Required Concept(s)
  1. CALayer
  2. UIView
Required Source(s)
  1. An Image with five stars like this




Source Code


 #define POINT             1.0
 #define WIDTH            82.5
 #define HEIGHT          15.0 
 #define DIVISOR         5.0 
 
 //Creation of Rating Star.

 CALayer *mainStar=[CALayer layer]; 
 mainStar.contents=(__bridge id)([UIImage imageNamed:@"rating_star"].CGImage);
 mainStar.frame=CGRectMake(POINT
,POINT,WIDTH,HEIGHT);
 CALayer *starColor=[CALayer layer];
 starColor.backgroundColor=[UIColor blueColor].CGColor;
 starColor.frame=
CGRectMake(0,0,
(3.5/DIVISOR)*WIDTH,HEIGHT);   
 //Change value of 3.5 from 0.0 to 5.0 to see the different rating.
[mainStar addSublayer:starColor];
CALayer *maskStar=[CALayer layer];
maskStar.contents=(__bridge id)([UIImage imageNamed:@"rating_star"].CGImage);
maskStar.frame=CGRectMake(0,0,WIDTH,HEIGHT);  
[mainStar addSublayer:maskStar];
 starColor.mask=maskStar; 
  
// Add Main Star to View
UIView *ratingView=[[UIView alloc] init];
ratingView.frame=CGRectMake(100,100,85,18);
[ratingView.layer addSubLayer:mainStar];
[self.view addSubView:ratingView];   // self may be any view controller.
  

Note
   Mask property of CALayer plays vital role.

Request
    Please post your comments and questions.


Wednesday 12 March 2014

Toast in IOS7 as in Android

Just You can use the following code with uilabel and uianimation to get toast like in android.
It does two works one is toast task and it increases the height of the label according to the text length with wordwrap IOS 7 later

    CGRect initialFrame = CGRectMake(20, self.view.frame.size.height/2,300, 40);
  
   
    NSString *message=@"Toast in Iphone as in Android";
    UILabel *flashLabel=[[UILabel alloc] initWithFrame:initialFrame];
    flashLabel.font=[UIFont fontWithName:@"Optima-Italic" size:12.0];
    flashLabel.backgroundColor=[UIColor whiteColor];
    flashLabel.layer.cornerRadius=3.0f;
    flashLabel.numberOfLines=0;
    flashLabel.textAlignment=NSTextAlignmentCenter;
   
    CGSize maxSize = CGSizeMake(flashLabel.frame.size.width, MAXFLOAT);
   
    CGRect labelRect = [message boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:flashLabel.font} context:nil];
   
    //adjust the label the the new height.
    CGRect newFrame = flashLabel.frame;
    newFrame.size.height = labelRect.size.height;
    flashLabel.frame = newFrame;
    flashLabel.text=message;
    [self.view addSubview:flashLabel];
   
    flashLabel.alpha=1.0;
    self.view.userInteractionEnabled=FALSE;
   
    [UIView animateWithDuration:13.0 animations:^
    {
        flashLabel.alpha=0.0f;
    }
    completion:^(BOOL finished)
    {
        self.view.userInteractionEnabled=TRUE;
       
        [flashLabel removeFromSuperview];
    }];

Sunday 12 January 2014

iPhone: Dynamic Resizable UILabel and UITableViewCell


Dear friends, I am very glad to post this simple concept of uilabel resizing depending on text size with uitableview. This is my first post on blog. I believe, uilabel resizing with uitableview will be helpful to many beginners.

Kindly follow steps involved for uilabel resizing with uitableview.

Step 1: Create project by selecting Single View Application with story board from Xcode.

Step 2: Place UITableViewController on story board from object library.

Step 3:Select UITableViewCell on UITableViewController.

Step 4:Change UITableViewCell Style->Custom, set UITableViewCell Identifier->Cell

Step 5:Place UILableView On UITableViewCell.

Step 6: Create New File Name the Class as "SampleCell" and choose SubClass "UITableViewCell" in the Attributes Inspector.

Step 7: Copy and Paste bellow codes to SampleCell.h and SampleCell.m files.

SampleCell.h

#import <UIKit/UIKit.h>

@interface SampleCell : UITableViewCell
{
    IBOutlet UILabel *testLabel;
}

@property(retain,nonatomic)UILabel *testLabel;

@end

SampleCell.m


#import "SampleCell.h"

@implementation SampleCell

@synthesize testLabel;


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

Step 8: Now change the UITableViewCell class to "SampleCell" in the Identity Inspector.
Step 9: Link the UILabel with "testLabel" by referring outlet in the Connection Inspector.

Step 10: Create New File Name the Class as "SampleTable" and choose SubClass "UITableViewController" in the Attributes Inspector.
Step 11: Copy and Paste bellow codes to SampleTable.h and SampleTable.m files.

SampleTable.h


#import <UIKit/UIKit.h>

@interface SampleTable : UITableViewController

@end


SampleTable.m

#import "SampleTable.h"
#import "SampleCell.h"

@interface SampleTable ()

@end

@implementation SampleTable

int a=0;
NSString *test=@"My question essentially boils down to the best way to support dynamic heights of UILabel's (and I suppose other elements) in a UITableCell, and also correctly resize the label width/height and cell heights when rotating. ";

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

   
    // This is used to notify the rotation of the device.

  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
   
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didRotate:)
                                                 name:@"UIDeviceOrientationDidChangeNotification" object:nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    static NSString *CellIdentifier = @"Cell";
    SampleCell *cell =(SampleCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.testLabel.text=test;
    if ([self isPortrait]) {
       
        cell.testLabel.frame=CGRectMake(10,0,cell.frame.size.width,cell.frame.size.height);
    }
    else
   
   cell.testLabel.frame=CGRectMake(10,0,[[UIScreen mainScreen] bounds].size.height
10,cell.frame.size.height);
 
   cell.testLabel.numberOfLines=0;
   [cell.testLabel sizeToFit];

   return cell;
}
- (void) didRotate:(NSNotification *)notification
{
    [self.tableView reloadData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
   
    SampleCell *cell = (SampleCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    //Set the maximum size
    CGSize maximumLabelSize = cell.testLabel.frame.size;
    //Calculate the new size based on the text
    CGSize expectedLabelSize = [test sizeWithFont:cell.testLabel.font constrainedToSize:maximumLabelSize lineBreakMode:cell.testLabel.lineBreakMode];

    return expectedLabelSize.height;
}

-(BOOL)isPortrait
{
    UIInterfaceOrientation orientation=[[UIApplication sharedApplication] statusBarOrientation];
   
    if(orientation==UIInterfaceOrientationPortrait ||
orientation==UIInterfaceOrientationPortraitUpsideDown)return YES;
    return NO;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end

Step 12: Now change the UITableViewController class to "SampleTable" in the Identity Inspector.

Step 13: Run and Enjoy.