Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes for calculation of the MessageLabel height and custom maximumNumberOfLines #103

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions SIAlertView/SIAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView);
@property (nonatomic, strong) UIColor *destructiveButtonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@property (nonatomic, assign) CGFloat cornerRadius NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 2.0
@property (nonatomic, assign) CGFloat shadowRadius NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 8.0
@property (nonatomic, assign) NSInteger maximumNumberOfLines NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 5

- (void)setDefaultButtonImage:(UIImage *)defaultButtonImage forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (void)setCancelButtonImage:(UIImage *)cancelButtonImage forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
Expand Down
19 changes: 8 additions & 11 deletions SIAlertView/SIAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#define DEBUG_LAYOUT 0

#define MESSAGE_MIN_LINE_COUNT 3
#define MESSAGE_MAX_LINE_COUNT 5
#define GAP 10
#define CANCEL_BUTTON_PADDING_TOP 5
#define CONTENT_PADDING_LEFT 10
Expand Down Expand Up @@ -247,6 +246,7 @@ + (void)initialize
appearance.destructiveButtonColor = [UIColor whiteColor];
appearance.cornerRadius = 2;
appearance.shadowRadius = 8;
appearance.maximumNumberOfLines = 5;
}

- (id)init
Expand Down Expand Up @@ -839,20 +839,17 @@ - (CGFloat)heightForTitleLabel

- (CGFloat)heightForMessageLabel
{
CGFloat minHeight = MESSAGE_MIN_LINE_COUNT * self.messageLabel.font.lineHeight;
CGFloat minHeight = ceilf(MESSAGE_MIN_LINE_COUNT * self.messageLabel.font.lineHeight);
if (self.messageLabel) {
CGFloat maxHeight = MESSAGE_MAX_LINE_COUNT * self.messageLabel.font.lineHeight;
CGFloat maxHeight = ceilf(self.maximumNumberOfLines * self.messageLabel.font.lineHeight);

#ifdef __IPHONE_7_0
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = self.messageLabel.lineBreakMode;

NSDictionary *attributes = @{NSFontAttributeName:self.messageLabel.font,
NSParagraphStyleAttributeName: paragraphStyle.copy};

NSDictionary *attributes = @{
NSFontAttributeName:self.messageLabel.font,
};
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
CGRect rect = [self.titleLabel.text boundingRectWithSize:CGSizeMake(CONTAINER_WIDTH - CONTENT_PADDING_LEFT * 2, maxHeight)
CGRect rect = [self.messageLabel.text boundingRectWithSize:CGSizeMake(CONTAINER_WIDTH - CONTENT_PADDING_LEFT * 2, maxHeight)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
Expand Down Expand Up @@ -941,7 +938,7 @@ - (void)updateMessageLabel
self.messageLabel.backgroundColor = [UIColor clearColor];
self.messageLabel.font = self.messageFont;
self.messageLabel.textColor = self.messageColor;
self.messageLabel.numberOfLines = MESSAGE_MAX_LINE_COUNT;
self.messageLabel.numberOfLines = self.maximumNumberOfLines;
[self.containerView addSubview:self.messageLabel];
#if DEBUG_LAYOUT
self.messageLabel.backgroundColor = [UIColor redColor];
Expand Down