Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TSAlertView/TSAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
114 changes: 91 additions & 23 deletions TSAlertView/TSAlertView.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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 <QuartzCore/QuartzCore.h>

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 ];
Expand Down Expand Up @@ -223,7 +233,6 @@ - (void)dealloc
[super dealloc];
}


- (void) TSAlertView_commonInit
{
self.backgroundColor = [UIColor clearColor];
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}
Expand All @@ -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];

Expand Down Expand Up @@ -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];
Expand All @@ -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 )
{
Expand All @@ -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;
}
Expand All @@ -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;

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -702,15 +760,15 @@ - (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;
}
}
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 );
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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];

Expand All @@ -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];

Expand All @@ -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];
Expand Down
Binary file added TSAlertView/TSAlertViewBackgroundiOS7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added TSAlertView/TSAlertViewButtonBackgroundiOS7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.