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

Fix for issue #68 with title and message text alignment #118

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 10 additions & 6 deletions Toast/UIView+Toast.m
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ - (UIView *)toastViewForMessage:(NSString *)message title:(NSString *)title imag
imageRect.size.height = imageView.bounds.size.height;
}

CGSize expectedSizeTitle = CGSizeZero;
if (title != nil) {
titleLabel = [[UILabel alloc] init];
titleLabel.numberOfLines = style.titleNumberOfLines;
Expand All @@ -269,13 +270,13 @@ - (UIView *)toastViewForMessage:(NSString *)message title:(NSString *)title imag
titleLabel.text = title;

// size the title label according to the length of the text
CGSize maxSizeTitle = CGSizeMake((self.bounds.size.width * style.maxWidthPercentage) - imageRect.size.width, self.bounds.size.height * style.maxHeightPercentage);
CGSize expectedSizeTitle = [titleLabel sizeThatFits:maxSizeTitle];
CGSize maxSizeTitle = CGSizeMake((self.bounds.size.width * style.maxWidthPercentage) - imageRect.size.width - 2 * style.horizontalPadding, self.bounds.size.height * style.maxHeightPercentage);
expectedSizeTitle = [titleLabel sizeThatFits:maxSizeTitle];
// UILabel can return a size larger than the max size when the number of lines is 1
expectedSizeTitle = CGSizeMake(MIN(maxSizeTitle.width, expectedSizeTitle.width), MIN(maxSizeTitle.height, expectedSizeTitle.height));
titleLabel.frame = CGRectMake(0.0, 0.0, expectedSizeTitle.width, expectedSizeTitle.height);
}

CGSize expectedSizeMessage = CGSizeZero;
if (message != nil) {
messageLabel = [[UILabel alloc] init];
messageLabel.numberOfLines = style.messageNumberOfLines;
Expand All @@ -287,16 +288,17 @@ - (UIView *)toastViewForMessage:(NSString *)message title:(NSString *)title imag
messageLabel.alpha = 1.0;
messageLabel.text = message;

CGSize maxSizeMessage = CGSizeMake((self.bounds.size.width * style.maxWidthPercentage) - imageRect.size.width, self.bounds.size.height * style.maxHeightPercentage);
CGSize expectedSizeMessage = [messageLabel sizeThatFits:maxSizeMessage];
CGSize maxSizeMessage = CGSizeMake((self.bounds.size.width * style.maxWidthPercentage) - imageRect.size.width - 2 * style.horizontalPadding, self.bounds.size.height * style.maxHeightPercentage);
expectedSizeMessage = [messageLabel sizeThatFits:maxSizeMessage];
// UILabel can return a size larger than the max size when the number of lines is 1
expectedSizeMessage = CGSizeMake(MIN(maxSizeMessage.width, expectedSizeMessage.width), MIN(maxSizeMessage.height, expectedSizeMessage.height));
messageLabel.frame = CGRectMake(0.0, 0.0, expectedSizeMessage.width, expectedSizeMessage.height);
}

CGRect titleRect = CGRectZero;

if(titleLabel != nil) {
// Ensure to set both title and message frames to the same width, so the 'textAlignment' property will have effect:
titleLabel.frame = CGRectMake(0.0, 0.0, MAX(expectedSizeTitle.width, expectedSizeMessage.width), expectedSizeTitle.height);
titleRect.origin.x = imageRect.origin.x + imageRect.size.width + style.horizontalPadding;
titleRect.origin.y = style.verticalPadding;
titleRect.size.width = titleLabel.bounds.size.width;
Expand All @@ -306,6 +308,8 @@ - (UIView *)toastViewForMessage:(NSString *)message title:(NSString *)title imag
CGRect messageRect = CGRectZero;

if(messageLabel != nil) {
// Ensure to set both title and message frames to the same width, so the 'textAlignment' property will have effect:
messageLabel.frame = CGRectMake(0.0, 0.0, MAX(expectedSizeTitle.width, expectedSizeMessage.width), expectedSizeMessage.height);
messageRect.origin.x = imageRect.origin.x + imageRect.size.width + style.horizontalPadding;
messageRect.origin.y = titleRect.origin.y + titleRect.size.height + style.verticalPadding;
messageRect.size.width = messageLabel.bounds.size.width;
Expand Down