diff --git a/TSAlertView/TSAlertView.h b/TSAlertView/TSAlertView.h index dfdc97c..0d885a0 100644 --- a/TSAlertView/TSAlertView.h +++ b/TSAlertView/TSAlertView.h @@ -67,6 +67,7 @@ typedef enum @property(nonatomic, retain) UIImage* backgroundImage; @property(nonatomic, assign) TSAlertViewStyle style; @property(nonatomic, readonly) UITextField* inputTextField; +@property(nonatomic, assign) BOOL isiOS7; - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...; - (NSInteger)addButtonWithTitle:(NSString *)title; diff --git a/TSAlertView/TSAlertView.m b/TSAlertView/TSAlertView.m old mode 100644 new mode 100755 index e83fe74..1d6710b --- a/TSAlertView/TSAlertView.m +++ b/TSAlertView/TSAlertView.m @@ -4,6 +4,8 @@ // Created by Nick Hodapp aka Tom Swift on 1/19/11. // +#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) + #import "TSAlertView.h" #import @@ -126,8 +128,10 @@ @implementation TSAlertView @synthesize style; const CGFloat kTSAlertView_LeftMargin = 10.0; +const CGFloat kTSAlertView_LeftMarginiOS7 = 4.75; const CGFloat kTSAlertView_TopMargin = 16.0; const CGFloat kTSAlertView_BottomMargin = 15.0; +const CGFloat kTSAlertView_BottomMarginiOS7 = 8.5; const CGFloat kTSAlertView_RowMargin = 5.0; const CGFloat kTSAlertView_ColumnMargin = 10.0; @@ -157,12 +161,18 @@ - (id) initWithFrame:(CGRect)frame - (id) initWithTitle: (NSString *) t message: (NSString *) m delegate: (id) d cancelButtonTitle: (NSString *) cancelButtonTitle otherButtonTitles: (NSString *) otherButtonTitles, ... { + if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { + _isiOS7 = YES; + } else { + _isiOS7 = NO; + } + if ( (self = [super init] ) ) // will call into initWithFrame, thus TSAlertView_commonInit is called { self.title = t; self.message = m; self.delegate = d; - + if ( nil != cancelButtonTitle ) { [self addButtonWithTitle: cancelButtonTitle ]; @@ -223,7 +233,6 @@ - (void)dealloc [super dealloc]; } - - (void) TSAlertView_commonInit { self.backgroundColor = [UIColor clearColor]; @@ -236,6 +245,12 @@ - (void) TSAlertView_commonInit buttonLayout = TSAlertViewButtonLayoutNormal; cancelButtonIndex = -1; firstOtherButtonIndex = -1; + + if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { + _isiOS7 = YES; + } else { + _isiOS7 = NO; + } } - (void) setWidth:(CGFloat) w @@ -340,7 +355,13 @@ - (UILabel*) titleLabel _titleLabel = [[UILabel alloc] init]; _titleLabel.font = [UIFont boldSystemFontOfSize: 18]; _titleLabel.backgroundColor = [UIColor clearColor]; - _titleLabel.textColor = [UIColor whiteColor]; + + if(_isiOS7) { + _titleLabel.textColor = [UIColor blackColor]; + } else { + _titleLabel.textColor = [UIColor whiteColor]; + } + _titleLabel.textAlignment = UITextAlignmentCenter; _titleLabel.lineBreakMode = UILineBreakModeWordWrap; _titleLabel.numberOfLines = 0; @@ -356,7 +377,13 @@ - (UILabel*) messageLabel _messageLabel = [[UILabel alloc] init]; _messageLabel.font = [UIFont systemFontOfSize: 16]; _messageLabel.backgroundColor = [UIColor clearColor]; - _messageLabel.textColor = [UIColor whiteColor]; + + if(_isiOS7) { + _messageLabel.textColor = [UIColor blackColor]; + } else { + _messageLabel.textColor = [UIColor whiteColor]; + } + _messageLabel.textAlignment = UITextAlignmentCenter; _messageLabel.lineBreakMode = UILineBreakModeWordWrap; _messageLabel.numberOfLines = 0; @@ -412,7 +439,11 @@ - (UIImage*) backgroundImage { if ( _backgroundImage == nil ) { - self.backgroundImage = [[UIImage imageNamed: @"TSAlertViewBackground.png"] stretchableImageWithLeftCapWidth: 15 topCapHeight: 30]; + if(_isiOS7) { + self.backgroundImage = [[UIImage imageNamed: @"TSAlertViewBackgroundiOS7.png"] stretchableImageWithLeftCapWidth: 15 topCapHeight: 30]; + } else { + self.backgroundImage = [[UIImage imageNamed: @"TSAlertViewBackground.png"] stretchableImageWithLeftCapWidth: 15 topCapHeight: 30]; + } } return _backgroundImage; @@ -454,11 +485,21 @@ - (void) setCancelButtonIndex:(NSInteger)buttonIndex UIButton* b = [self.buttons objectAtIndex: buttonIndex]; - UIImage* buttonBgNormal = [UIImage imageNamed: @"TSAlertViewCancelButtonBackground.png"]; + UIImage* buttonBgNormal; + if(_isiOS7) { + buttonBgNormal = [UIImage imageNamed: @"TSAlertViewCancelButtonBackgroundiOS7.png"]; + } else { + buttonBgNormal = [UIImage imageNamed: @"TSAlertViewCancelButtonBackground.png"]; + } buttonBgNormal = [buttonBgNormal stretchableImageWithLeftCapWidth: buttonBgNormal.size.width / 2.0 topCapHeight: buttonBgNormal.size.height / 2.0]; [b setBackgroundImage: buttonBgNormal forState: UIControlStateNormal]; - UIImage* buttonBgPressed = [UIImage imageNamed: @"TSAlertViewButtonBackground_Highlighted.png"]; + UIImage* buttonBgPressed; + if(_isiOS7) { + buttonBgPressed = [UIImage imageNamed: @"TSAlertViewButtonBackground_HighlightediOS7.png"]; + } else { + buttonBgPressed = [UIImage imageNamed: @"TSAlertViewButtonBackground_Highlighted.png"]; + } buttonBgPressed = [buttonBgPressed stretchableImageWithLeftCapWidth: buttonBgPressed.size.width / 2.0 topCapHeight: buttonBgPressed.size.height / 2.0]; [b setBackgroundImage: buttonBgPressed forState: UIControlStateHighlighted]; } @@ -472,12 +513,26 @@ - (NSInteger) addButtonWithTitle: (NSString *) t { UIButton* b = [UIButton buttonWithType: UIButtonTypeCustom]; [b setTitle: t forState: UIControlStateNormal]; - - UIImage* buttonBgNormal = [UIImage imageNamed: @"TSAlertViewButtonBackground.png"]; + + if(_isiOS7) { + [b setTitleColor:[UIColor colorWithRed:0/255.0 green:122.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal]; + } + + UIImage* buttonBgNormal; + if(_isiOS7) { + buttonBgNormal = [UIImage imageNamed: @"TSAlertViewButtonBackgroundiOS7.png"]; + } else { + buttonBgNormal = [UIImage imageNamed: @"TSAlertViewButtonBackground.png"]; + } buttonBgNormal = [buttonBgNormal stretchableImageWithLeftCapWidth: buttonBgNormal.size.width / 2.0 topCapHeight: buttonBgNormal.size.height / 2.0]; [b setBackgroundImage: buttonBgNormal forState: UIControlStateNormal]; - UIImage* buttonBgPressed = [UIImage imageNamed: @"TSAlertViewButtonBackground_Highlighted.png"]; + UIImage* buttonBgPressed; + if(_isiOS7) { + buttonBgPressed = [UIImage imageNamed: @"TSAlertViewButtonBackground_HighlightediOS7.png"]; + } else { + buttonBgPressed = [UIImage imageNamed: @"TSAlertViewButtonBackground_Highlighted.png"]; + } buttonBgPressed = [buttonBgPressed stretchableImageWithLeftCapWidth: buttonBgPressed.size.width / 2.0 topCapHeight: buttonBgPressed.size.height / 2.0]; [b setBackgroundImage: buttonBgPressed forState: UIControlStateHighlighted]; @@ -629,9 +684,12 @@ - (void) onButtonPress: (id) sender - (CGSize) recalcSizeAndLayout: (BOOL) layout { + const CGFloat leftMargin = _isiOS7 ? kTSAlertView_LeftMarginiOS7 : kTSAlertView_LeftMargin; + const CGFloat bottomMargin = _isiOS7 ? kTSAlertView_BottomMarginiOS7 : kTSAlertView_BottomMargin; + BOOL stacked = !(self.buttonLayout == TSAlertViewButtonLayoutNormal && [self.buttons count] == 2 ); - CGFloat maxWidth = self.width - (kTSAlertView_LeftMargin * 2); + CGFloat maxWidth = self.width - (leftMargin * 2); CGSize titleLabelSize = [self titleLabelSize]; CGSize messageViewSize = [self messageLabelSize]; @@ -640,7 +698,7 @@ - (CGSize) recalcSizeAndLayout: (BOOL) layout CGFloat inputRowHeight = self.style == TSAlertViewStyleInput ? inputTextFieldSize.height + kTSAlertView_RowMargin : 0; - CGFloat totalHeight = kTSAlertView_TopMargin + titleLabelSize.height + kTSAlertView_RowMargin + messageViewSize.height + inputRowHeight + kTSAlertView_RowMargin + buttonsAreaSize.height + kTSAlertView_BottomMargin; + CGFloat totalHeight = kTSAlertView_TopMargin + titleLabelSize.height + kTSAlertView_RowMargin + messageViewSize.height + inputRowHeight + kTSAlertView_RowMargin + buttonsAreaSize.height + bottomMargin; if ( totalHeight > self.maxHeight ) { @@ -661,7 +719,7 @@ - (CGSize) recalcSizeAndLayout: (BOOL) layout CGFloat y = kTSAlertView_TopMargin; if ( self.title != nil ) { - self.titleLabel.frame = CGRectMake( kTSAlertView_LeftMargin, y, titleLabelSize.width, titleLabelSize.height ); + self.titleLabel.frame = CGRectMake( leftMargin, y, titleLabelSize.width, titleLabelSize.height ); [self addSubview: self.titleLabel]; y += titleLabelSize.height + kTSAlertView_RowMargin; } @@ -671,7 +729,7 @@ - (CGSize) recalcSizeAndLayout: (BOOL) layout { if ( self.usesMessageTextView ) { - self.messageTextView.frame = CGRectMake( kTSAlertView_LeftMargin, y, messageViewSize.width, messageViewSize.height ); + self.messageTextView.frame = CGRectMake( leftMargin, y, messageViewSize.width, messageViewSize.height ); [self addSubview: self.messageTextView]; y += messageViewSize.height + kTSAlertView_RowMargin; @@ -681,7 +739,7 @@ - (CGSize) recalcSizeAndLayout: (BOOL) layout } else { - self.messageLabel.frame = CGRectMake( kTSAlertView_LeftMargin, y, messageViewSize.width, messageViewSize.height ); + self.messageLabel.frame = CGRectMake( leftMargin, y, messageViewSize.width, messageViewSize.height ); [self addSubview: self.messageLabel]; y += messageViewSize.height + kTSAlertView_RowMargin; } @@ -690,7 +748,7 @@ - (CGSize) recalcSizeAndLayout: (BOOL) layout // input if ( self.style == TSAlertViewStyleInput ) { - self.inputTextField.frame = CGRectMake( kTSAlertView_LeftMargin, y, inputTextFieldSize.width, inputTextFieldSize.height ); + self.inputTextField.frame = CGRectMake( leftMargin, y, inputTextFieldSize.width, inputTextFieldSize.height ); [self addSubview: self.inputTextField]; y += inputTextFieldSize.height + kTSAlertView_RowMargin; } @@ -702,7 +760,7 @@ - (CGSize) recalcSizeAndLayout: (BOOL) layout CGFloat buttonWidth = maxWidth; for ( UIButton* b in self.buttons ) { - b.frame = CGRectMake( kTSAlertView_LeftMargin, y, buttonWidth, buttonHeight ); + b.frame = CGRectMake( leftMargin, y, buttonWidth, buttonHeight ); [self addSubview: b]; y += buttonHeight + kTSAlertView_RowMargin; } @@ -710,7 +768,7 @@ - (CGSize) recalcSizeAndLayout: (BOOL) layout else { CGFloat buttonWidth = (maxWidth - kTSAlertView_ColumnMargin) / 2.0; - CGFloat x = kTSAlertView_LeftMargin; + CGFloat x = leftMargin; for ( UIButton* b in self.buttons ) { b.frame = CGRectMake( x, y, buttonWidth, buttonHeight ); @@ -726,7 +784,9 @@ - (CGSize) recalcSizeAndLayout: (BOOL) layout - (CGSize) titleLabelSize { - CGFloat maxWidth = self.width - (kTSAlertView_LeftMargin * 2); + const CGFloat leftMargin = _isiOS7 ? kTSAlertView_LeftMarginiOS7 : kTSAlertView_LeftMargin; + + CGFloat maxWidth = self.width - (leftMargin * 2); CGSize s = [self.titleLabel.text sizeWithFont: self.titleLabel.font constrainedToSize: CGSizeMake(maxWidth, 1000) lineBreakMode: self.titleLabel.lineBreakMode]; if ( s.width < maxWidth ) s.width = maxWidth; @@ -736,7 +796,9 @@ - (CGSize) titleLabelSize - (CGSize) messageLabelSize { - CGFloat maxWidth = self.width - (kTSAlertView_LeftMargin * 2); + const CGFloat leftMargin = _isiOS7 ? kTSAlertView_LeftMarginiOS7 : kTSAlertView_LeftMargin; + + CGFloat maxWidth = self.width - (leftMargin * 2); CGSize s = [self.messageLabel.text sizeWithFont: self.messageLabel.font constrainedToSize: CGSizeMake(maxWidth, 1000) lineBreakMode: self.messageLabel.lineBreakMode]; if ( s.width < maxWidth ) s.width = maxWidth; @@ -749,7 +811,9 @@ - (CGSize) inputTextFieldSize if ( self.style == TSAlertViewStyleNormal) return CGSizeZero; - CGFloat maxWidth = self.width - (kTSAlertView_LeftMargin * 2); + const CGFloat leftMargin = _isiOS7 ? kTSAlertView_LeftMarginiOS7 : kTSAlertView_LeftMargin; + + CGFloat maxWidth = self.width - (leftMargin * 2); CGSize s = [self.inputTextField sizeThatFits: CGSizeZero]; @@ -758,7 +822,9 @@ - (CGSize) inputTextFieldSize - (CGSize) buttonsAreaSize_SideBySide { - CGFloat maxWidth = self.width - (kTSAlertView_LeftMargin * 2); + const CGFloat leftMargin = _isiOS7 ? kTSAlertView_LeftMarginiOS7 : kTSAlertView_LeftMargin; + + CGFloat maxWidth = self.width - (leftMargin * 2); CGSize bs = [[self.buttons objectAtIndex:0] sizeThatFits: CGSizeZero]; @@ -769,7 +835,9 @@ - (CGSize) buttonsAreaSize_SideBySide - (CGSize) buttonsAreaSize_Stacked { - CGFloat maxWidth = self.width - (kTSAlertView_LeftMargin * 2); + const CGFloat leftMargin = _isiOS7 ? kTSAlertView_LeftMarginiOS7 : kTSAlertView_LeftMargin; + + CGFloat maxWidth = self.width - (leftMargin * 2); int buttonCount = [self.buttons count]; CGSize bs = [[self.buttons objectAtIndex:0] sizeThatFits: CGSizeZero]; diff --git a/TSAlertView/TSAlertViewBackgroundiOS7.png b/TSAlertView/TSAlertViewBackgroundiOS7.png new file mode 100755 index 0000000..5b511e4 Binary files /dev/null and b/TSAlertView/TSAlertViewBackgroundiOS7.png differ diff --git a/TSAlertView/TSAlertViewButtonBackground_HighlightediOS7.png b/TSAlertView/TSAlertViewButtonBackground_HighlightediOS7.png new file mode 100755 index 0000000..807f199 Binary files /dev/null and b/TSAlertView/TSAlertViewButtonBackground_HighlightediOS7.png differ diff --git a/TSAlertView/TSAlertViewButtonBackgroundiOS7.png b/TSAlertView/TSAlertViewButtonBackgroundiOS7.png new file mode 100755 index 0000000..13572c1 Binary files /dev/null and b/TSAlertView/TSAlertViewButtonBackgroundiOS7.png differ diff --git a/TSAlertView/TSAlertViewCancelButtonBackgroundiOS7.png b/TSAlertView/TSAlertViewCancelButtonBackgroundiOS7.png new file mode 100755 index 0000000..13572c1 Binary files /dev/null and b/TSAlertView/TSAlertViewCancelButtonBackgroundiOS7.png differ