Pages

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];
    }];

No comments:

Post a Comment