Skip to content

Commit

Permalink
MultiLined Buttons
Browse files Browse the repository at this point in the history
Sometimes the buttons in alerts may need to have large title like "I
understood", "Confirm and Accept terms of conditions" etc.

With this revision essential changes are done to make CXAlertView
support larger button titles making it more usable unlike ios alert
view where titles are truncated.
  • Loading branch information
ramakrishnachunduri authored and ramakrishnachunduri committed Nov 25, 2013
1 parent 112c993 commit 8d60fd5
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 54 deletions.
174 changes: 121 additions & 53 deletions CXAlertView/CXAlertView.m
Expand Up @@ -13,6 +13,14 @@
#import "CXAlertButtonContainerView.h"
#import <QuartzCore/QuartzCore.h>

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0
#define LBM NSLineBreakByTruncatingTail
#define BT_LBM NSLineBreakByWordWrapping
#else
#define LBM UILineBreakModeTailTruncation
#define BT_LBM UILineBreakModeWordWrap
#endif

#import "LFGlassView.h"

static CGFloat const kDefaultScrollViewPadding = 10.;
Expand Down Expand Up @@ -87,11 +95,13 @@ - (void)transitionInCompletion:(void(^)(void))completion;
- (void)transitionOutCompletion:(void(^)(void))completion;

// Buttons
- (UIFont*)fontForButtonType:(CXAlertViewButtonType)type;
- (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type handler:(CXAlertButtonHandler)handler font:(UIFont *)font;
- (CXAlertButtonItem *)buttonItemWithType:(CXAlertViewButtonType)type font:(UIFont *)font;
- (void)buttonAction:(CXAlertButtonItem *)buttonItem;
- (void)setButtonImage:(UIImage *)image forState:(UIControlState)state andButtonType:(CXAlertViewButtonType)type;
- (void)updateAllButtonsFont;

// Blur
- (void)updateBlurBackground;
@end
Expand Down Expand Up @@ -150,11 +160,7 @@ - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTi
messageLabel.text = message;
messageLabel.frame = CGRectMake( self.vericalPadding, 0, self.containerWidth - self.vericalPadding*2, [self heightWithText:message font:messageLabel.font]);

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0
messageLabel.lineBreakMode = NSLineBreakByTruncatingTail;
#else
messageLabel.lineBreakMode = UILineBreakModeTailTruncation;
#endif
messageLabel.lineBreakMode=LBM;

return [self initWithTitle:title contentView:messageLabel cancelButtonTitle:cancelButtonTitle];
}
Expand All @@ -176,6 +182,10 @@ - (id)initWithTitle:(NSString *)title contentView:(UIView *)contentView cancelBu
_contentScrollViewMaxHeight = kDefaultContentScrollViewMaxHeight;
_contentScrollViewMinHeight = kDefaultContentScrollViewMinHeight;
_bottomScrollViewHeight = kDefaultBottomScrollViewHeight;

_buttonFont=[UIFont systemFontOfSize:[UIFont buttonFontSize]];
_cancelButtonFont = [UIFont boldSystemFontOfSize:[UIFont buttonFontSize]];
_customButtonFont=_buttonFont;

_showButtonLine = YES;
_showBlurBackground = YES;
Expand All @@ -188,23 +198,12 @@ - (id)initWithTitle:(NSString *)title contentView:(UIView *)contentView cancelBu
}
return self;
}

// Buttons
- (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type handler:(CXAlertButtonHandler)handler
{
UIFont *font = nil;
switch (type) {
case CXAlertViewButtonTypeCancel:
font = self.cancelButtonFont;
break;
case CXAlertViewButtonTypeCustom:
font = self.customButtonFont;
break;
case CXAlertViewButtonTypeDefault:
default:
font = self.buttonFont;
break;
}
[self addButtonWithTitle:title type:type handler:handler font:font];
UIFont *font=[self fontForButtonType:type];
[self addButtonWithTitle:title type:type handler:handler font:font];
}

- (void)setDefaultButtonImage:(UIImage *)defaultButtonImage forState:(UIControlState)state
Expand Down Expand Up @@ -708,48 +707,117 @@ - (void)transitionOutCompletion:(void(^)(void))completion
}

// Buttons
-(UIFont*)fontForButtonType:(CXAlertViewButtonType)type
{
UIFont *font = nil;
if(type==CXAlertViewButtonTypeCancel)
{
font = self.cancelButtonFont;
}
else if(type==CXAlertViewButtonTypeCustom)
{
font = self.customButtonFont;
}
else
{
font = self.buttonFont;
}

return font;
}

-(CGFloat)heightThatFitsButton:(CXAlertButtonItem*)button
{
CGSize desiredSize=button.frame.size;
desiredSize.height=desiredSize.height;
desiredSize.width=desiredSize.width-20;

UIFont *fnt=[self fontForButtonType:button.type];
CGFloat btht=[button.title sizeWithFont:fnt constrainedToSize:desiredSize lineBreakMode:BT_LBM].height;
return btht+22;
}

-(void)setMaxSizeForAllButtons
{
CGFloat maxHeight=22;
for(CXAlertButtonItem *button in self.buttons)
{
CGFloat ht=[self heightThatFitsButton:button];
if(ht>maxHeight)
{
maxHeight=ht;
}
}

for(CXAlertButtonItem *button in self.buttons)
{
CGRect rect=button.frame;
rect.size.height=maxHeight;
button.frame=rect;
}

_bottomScrollView.contentSize = CGSizeMake( _bottomScrollView.contentSize.width, maxHeight);
_bottomScrollViewHeight=maxHeight;
}

- (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type handler:(CXAlertButtonHandler)handler font:(UIFont *)font
{
CXAlertButtonItem *button = [self buttonItemWithType:type font:font];

button.title = title;
button.action = handler;
button.type = type;
button.defaultRightLineVisible = _showButtonLine;
[button setTitle:title forState:UIControlStateNormal];
if ([_buttons count] == 0) {
button.defaultRightLineVisible = NO;
button.frame = CGRectMake( self.containerWidth/4, 0, self.containerWidth/2, self.buttonHeight);
}
else {
// correct first button
CXAlertButtonItem *firstButton = [_buttons objectAtIndex:0];
firstButton.defaultRightLineVisible = _showButtonLine;
CGRect newFrame = firstButton.frame;
newFrame.origin.x = 0;
[firstButton setNeedsDisplay];

CGFloat last_x = self.containerWidth/2 * [_buttons count];
button.frame = CGRectMake( last_x + self.containerWidth/2, 0, self.containerWidth/2, self.buttonHeight);
button.alpha = 0.;
if (self.isVisible) {
[UIView animateWithDuration:0.3 animations:^{
firstButton.frame = newFrame;
button.alpha = 1.;
button.frame = CGRectMake( last_x, 0, self.containerWidth/2, self.buttonHeight);
}];
}
else {
firstButton.frame = newFrame;
button.alpha = 1.;
button.frame = CGRectMake( last_x, 0, self.containerWidth/2, self.buttonHeight);
}
}

[_buttons addObject:button];
[_bottomScrollView addSubview:button];
CGFloat newContentWidth = self.bottomScrollView.contentSize.width + CGRectGetWidth(button.frame);
_bottomScrollView.contentSize = CGSizeMake( newContentWidth, self.buttonHeight);

button.titleLabel.textAlignment=UITextAlignmentCenter;
[button.titleLabel setNumberOfLines:0];
button.titleLabel.lineBreakMode=BT_LBM;
[button setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)];


if ([_buttons count] == 0)
{
button.defaultRightLineVisible = NO;
button.frame = CGRectMake( self.containerWidth/4, 0, self.containerWidth/2, self.buttonHeight);

[_buttons addObject:button];
[self setMaxSizeForAllButtons];
}
else
{
// correct first button
CXAlertButtonItem *firstButton = [_buttons objectAtIndex:0];
firstButton.defaultRightLineVisible = _showButtonLine;
CGRect newFrame = firstButton.frame;
newFrame.origin.x = 0;
[firstButton setNeedsDisplay];

CGFloat last_x = self.containerWidth/2 * [_buttons count];
button.frame = CGRectMake( last_x + self.containerWidth/2, 0, self.containerWidth/2, self.buttonHeight);
button.alpha = 0.;

[_buttons addObject:button];

if (self.isVisible) {
[UIView animateWithDuration:0.3 animations:^{
firstButton.frame = newFrame;
button.alpha = 1.;
button.frame = CGRectMake( last_x, 0, self.containerWidth/2, self.buttonHeight);
[self setMaxSizeForAllButtons];
}];
}
else {
firstButton.frame = newFrame;
button.alpha = 1.;
button.frame = CGRectMake( last_x, 0, self.containerWidth/2, self.buttonHeight);
[self setMaxSizeForAllButtons];
}
}

[_bottomScrollView addSubview:button];

CGFloat newContentWidth = self.bottomScrollView.contentSize.width + CGRectGetWidth(button.frame);
_bottomScrollView.contentSize = CGSizeMake( newContentWidth, _bottomScrollView.contentSize.height);
}

- (CXAlertButtonItem *)buttonItemWithType:(CXAlertViewButtonType)type font:(UIFont *)font
Expand Down
2 changes: 1 addition & 1 deletion CXAlertViewDemo/CXAlertViewDemo/CXViewController.m
Expand Up @@ -11,7 +11,7 @@


#define defaultMessageTitle @"Chris Xu"
#define multiLinedMessageContent @"This is an alert view developed by Chris Xu and enhanced by other contributors which allows you to serve following purposes \n\n - show ios7 styled alerts in ios 5 and 6 \n\n - fully customozable alertview with changable colors,radiuses,fonts..etc \n\n - multilined alert texts and button titles \n\n - much more to come"
#define multiLinedMessageContent @"This is an alert view developed by Chris Xu and enhanced by other contributors which allows you to serve following purposes \n\n - show ios7 styled alerts in ios 5 and 6 \n\n - multilined alert texts and button titles \n\n - fully customozable alertview with changable colors,radiuses,fonts..etc \n\n - much more to come"
#define multiLineMessageCancelText @"I dont understand clearly"
#define multiLineMessageAcceptedText @"I understood and accepting it"

Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -10,6 +10,8 @@ If you ever try to mess up with the UIAlertView, it is easy. Go to add at least

---
![ScreenShot 1](screenshot1.png)-![ScreenShot 2](screenshot2.png)
---
![Multilined Button Titles](screenshot-multilined-buttons.png)

##Installation

Expand Down
Binary file added screenshot-multilined-buttons.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8d60fd5

Please sign in to comment.