Skip to content

Commit

Permalink
This solves issue scalessec#68 with title and message alignment:
Browse files Browse the repository at this point in the history
* The key it to use the same width for both UILabels. This will be the largest value of the 2 expected widths based on the text content.
* The available width for the UILabel was too large; the style.horizontalPadding wasn't respected.
  • Loading branch information
funnel20 committed Jan 26, 2018
1 parent e149762 commit f4ecae1
Showing 1 changed file with 10 additions and 6 deletions.
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

0 comments on commit f4ecae1

Please sign in to comment.