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

Added NSAttributedString for message label #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions SIAlertView/SIAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView);
@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, strong) NSAttributedString * messageAttributedString;

- (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;
- (void)setDestructiveButtonImage:(UIImage *)destructiveButtonImage forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
Expand Down
53 changes: 49 additions & 4 deletions SIAlertView/SIAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ + (void)initialize
appearance.buttonColor = [UIColor colorWithWhite:0.4 alpha:1];
appearance.cancelButtonColor = [UIColor colorWithWhite:0.3 alpha:1];
appearance.destructiveButtonColor = [UIColor whiteColor];

appearance.cornerRadius = 2;
appearance.shadowRadius = 8;
}
Expand Down Expand Up @@ -337,6 +338,9 @@ - (void)setTitle:(NSString *)title
- (void)setMessage:(NSString *)message
{
_message = message;
if (message) {
self.messageAttributedString = nil;
}
[self invalidateLayout];
}

Expand Down Expand Up @@ -727,7 +731,13 @@ - (void)validateLayout
if (y > CONTENT_PADDING_TOP) {
y += GAP;
}
self.messageLabel.text = self.message;
if (self.message) {
self.messageLabel.text = self.message;
}
else if(self.messageAttributedString) {
self.messageLabel.attributedText = self.messageAttributedString;
}

CGFloat height = [self heightForMessageLabel];
self.messageLabel.frame = CGRectMake(CONTENT_PADDING_LEFT, y, self.containerView.bounds.size.width - CONTENT_PADDING_LEFT * 2, height);
y += height;
Expand Down Expand Up @@ -765,7 +775,7 @@ - (CGFloat)preferredHeight
if (self.title) {
height += [self heightForTitleLabel];
}
if (self.message) {
if (self.message || self.messageAttributedString) {
if (height > CONTENT_PADDING_TOP) {
height += GAP;
}
Expand Down Expand Up @@ -809,13 +819,21 @@ - (CGFloat)heightForTitleLabel
- (CGFloat)heightForMessageLabel
{
CGFloat minHeight = MESSAGE_MIN_LINE_COUNT * self.messageLabel.font.lineHeight;
if (self.messageLabel) {
if (self.messageLabel && self.message) {
CGFloat maxHeight = MESSAGE_MAX_LINE_COUNT * self.messageLabel.font.lineHeight;
CGSize size = [self.message sizeWithFont:self.messageLabel.font
constrainedToSize:CGSizeMake(CONTAINER_WIDTH - CONTENT_PADDING_LEFT * 2, maxHeight)
lineBreakMode:self.messageLabel.lineBreakMode];
return MAX(minHeight, size.height);
}
else if (self.messageLabel && self.messageAttributedString)
{
CGFloat maxHeight = MESSAGE_MAX_LINE_COUNT * self.messageLabel.font.lineHeight;
CGRect rect = [self.messageAttributedString boundingRectWithSize:CGSizeMake(CONTAINER_WIDTH - CONTENT_PADDING_LEFT * 2, maxHeight)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return MAX(minHeight, rect.size.height);
}
return minHeight;
}

Expand Down Expand Up @@ -897,7 +915,21 @@ - (void)updateMessageLabel
#endif
}
self.messageLabel.text = self.message;
} else {
}
else if (self.messageAttributedString)
{
if (!self.messageLabel) {
self.messageLabel = [[UILabel alloc] initWithFrame:self.bounds];
self.messageLabel.textAlignment = NSTextAlignmentCenter;
self.messageLabel.backgroundColor = [UIColor clearColor];
self.messageLabel.font = self.messageFont;
self.messageLabel.textColor = self.messageColor;
self.messageLabel.numberOfLines = MESSAGE_MAX_LINE_COUNT;
[self.containerView addSubview:self.messageLabel];
}
self.messageLabel.attributedText = self.messageAttributedString;
}
else {
[self.messageLabel removeFromSuperview];
self.messageLabel = nil;
}
Expand Down Expand Up @@ -1084,6 +1116,19 @@ - (void)setDestructiveButtonColor:(UIColor *)buttonColor
[self setColor:buttonColor toButtonsOfType:SIAlertViewButtonTypeDestructive];
}

- (void)setMessageAttributedString:(NSAttributedString *)messageAttributedString
{
if (_messageAttributedString == messageAttributedString) {
return;
}
_messageAttributedString = messageAttributedString;
if (messageAttributedString) {
self.message = nil;
}

[self invalidateLayout];
}


- (void)setDefaultButtonImage:(UIImage *)defaultButtonImage forState:(UIControlState)state
{
Expand Down