226{
  227  _shown = YES;
  228 
  229  BOOL isSecondButton = NO;
  230  NSUInteger index = 0;
  231  for (NSUInteger i = 0; i < _blocks.count; i++)
  232  {
  233    NSArray *block = [_blocks objectAtIndex:i];
  234    NSString *title = [block objectAtIndex:1];
  235    NSString *color = [block objectAtIndex:2];
  236 
  237    UIImage *image =
  238        [UIImage imageNamed:[NSString stringWithFormat:@"alert-%@-button.png", color]];
  239    image = [image stretchableImageWithLeftCapWidth:(int)(image.size.width + 1) >> 1
  240                                       topCapHeight:0];
  241 
  242    CGFloat maxHalfWidth = floorf((_view.bounds.size.width - kAlertViewBorder * 3) * 0.5);
  243    CGFloat width = _view.bounds.size.width - kAlertViewBorder * 2;
  244    CGFloat xOffset = kAlertViewBorder;
  245    if (isSecondButton)
  246    {
  247      width = maxHalfWidth;
  248      xOffset = width + kAlertViewBorder * 2;
  249      isSecondButton = NO;
  250    }
  251    else if (i + 1 < _blocks.count)
  252    {
  253      
  254      
  255      CGSize size = [title sizeWithFont:buttonFont
  256                            minFontSize:10
  257                         actualFontSize:nil
  258                               forWidth:_view.bounds.size.width - kAlertViewBorder * 2
  259                          lineBreakMode:NSLineBreakByClipping];
  260 
  261      if (size.width < maxHalfWidth - kAlertViewBorder)
  262      {
  263        
  264        NSArray *block2 = [_blocks objectAtIndex:i + 1];
  265        NSString *title2 = [block2 objectAtIndex:1];
  266        size = [title2 sizeWithFont:buttonFont
  267                        minFontSize:10
  268                     actualFontSize:nil
  269                           forWidth:_view.bounds.size.width - kAlertViewBorder * 2
  270                      lineBreakMode:NSLineBreakByClipping];
  271 
  272        if (size.width < maxHalfWidth - kAlertViewBorder)
  273        {
  274          
  275          isSecondButton = YES; 
  276          width = maxHalfWidth;
  277        }
  278      }
  279    }
  280    else if (_blocks.count == 1)
  281    {
  282      
  283      CGSize size = [title sizeWithFont:buttonFont
  284                            minFontSize:10
  285                         actualFontSize:nil
  286                               forWidth:_view.bounds.size.width - kAlertViewBorder * 2
  287                          lineBreakMode:UILineBreakModeClip];
  288 
  289      size.width = MAX(size.width, 80);
  290      if (size.width + 2 * kAlertViewBorder < width)
  291      {
  292        width = size.width + 2 * kAlertViewBorder;
  293        xOffset = floorf((_view.bounds.size.width - width) * 0.5);
  294      }
  295    }
  296 
  297    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  298    button.frame = CGRectMake(xOffset, _height, width, kAlertButtonHeight);
  299    button.titleLabel.font = buttonFont;
  300    if (IOS_LESS_THAN_6)
  301    {
  302#pragma clang diagnostic push
  303#pragma clang diagnostic ignored "-Wdeprecated-declarations"
  304      button.titleLabel.minimumFontSize = 10;
  305#pragma clang diagnostic pop
  306    }
  307    else
  308    {
  309      button.titleLabel.adjustsFontSizeToFitWidth = YES;
  310      button.titleLabel.adjustsLetterSpacingToFitWidth = YES;
  311      button.titleLabel.minimumScaleFactor = 0.1;
  312    }
  313    button.titleLabel.textAlignment = NSTextAlignmentCenter;
  314    button.titleLabel.shadowOffset = kAlertViewButtonShadowOffset;
  315    button.backgroundColor = [UIColor clearColor];
  316    button.tag = i + 1;
  317 
  318    [button setBackgroundImage:image forState:UIControlStateNormal];
  319    [button setTitleColor:kAlertViewButtonTextColor forState:UIControlStateNormal];
  320    [button setTitleShadowColor:kAlertViewButtonShadowColor forState:UIControlStateNormal];
  321    [button setTitle:title forState:UIControlStateNormal];
  322    button.accessibilityLabel = title;
  323 
  324    [button addTarget:self
  325                  action:@selector(buttonClicked:)
  326        forControlEvents:UIControlEventTouchUpInside];
  327 
  328    [_view addSubview:button];
  329 
  330    if (!isSecondButton)
  331      _height += kAlertButtonHeight + kAlertViewBorder;
  332 
  333    index++;
  334  }
  335 
  336  _height += 10; 
  337 
  338  if (_height < background.size.height)
  339  {
  340    CGFloat offset = background.size.height - _height;
  341    _height = background.size.height;
  342    CGRect frame;
  343    for (NSUInteger i = 0; i < _blocks.count; i++)
  344    {
  345      UIButton *btn = (UIButton *)[_view viewWithTag:i + 1];
  346      frame = btn.frame;
  347      frame.origin.y += offset;
  348      btn.frame = frame;
  349    }
  350  }
  351 
  352  CGRect frame = _view.frame;
  353  frame.origin.y = -_height;
  354  frame.size.height = _height;
  355  _view.frame = frame;
  356 
  357  UIImageView *modalBackground = [[UIImageView alloc] initWithFrame:_view.bounds];
  358 
  359  if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
  360    modalBackground.image = backgroundlandscape;
  361  else
  362    modalBackground.image = background;
  363 
  364  modalBackground.contentMode = UIViewContentModeScaleToFill;
  365  modalBackground.autoresizingMask =
  366      UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  367  [_view insertSubview:modalBackground atIndex:0];
  368  [modalBackground release];
  369 
  370  if (_backgroundImage)
  371  {
  373    [_backgroundImage release];
  374    _backgroundImage = nil;
  375  }
  376 
  377  [
BlockBackground sharedInstance].vignetteBackground = _vignetteBackground;
 
  379 
  380  __block CGPoint center = _view.center;
  381  center.y = floorf([
BlockBackground sharedInstance].bounds.size.height * 0.5) + kAlertViewBounce;
 
  382 
  383  _cancelBounce = NO;
  384 
  385  [UIView animateWithDuration:0.4
  386      delay:0.0
  387      options:UIViewAnimationCurveEaseOut
  388      animations:^{
  390        _view.center = center;
  391      }
  392      completion:^(BOOL finished) {
  393        if (_cancelBounce)
  394          return;
  395 
  396        [UIView animateWithDuration:0.1
  397            delay:0.0
  398            options:0
  399            animations:^{
  400              center.y -= kAlertViewBounce;
  401              _view.center = center;
  402            }
  403            completion:^(BOOL finished) {
  404              [[NSNotificationCenter defaultCenter]
  405                  postNotificationName:@"AlertViewFinishedAnimations"
  406                                object:nil];
  407            }];
  408      }];
  409 
  410  [self retain];
  411}