Skip to content

Commit

Permalink
Use boundingRectWithSize: instead of sizeWithFont: in iOS7. fixes sca…
Browse files Browse the repository at this point in the history
  • Loading branch information
scalessec committed Dec 30, 2013
1 parent c6c8011 commit 8770ab7
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions Toast/Toast/Toast+UIView.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ - (void)toastTimerDidFinish:(NSTimer *)timer;
- (void)handleToastTapped:(UITapGestureRecognizer *)recognizer;
- (CGPoint)centerPointForPosition:(id)position withToast:(UIView *)toast;
- (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UIImage *)image;
- (CGSize)sizeForString:(NSString *)string font:(UIFont *)font constrainedToSize:(CGSize)constrainedSize lineBreakMode:(NSLineBreakMode)lineBreakMode;

@end

Expand Down Expand Up @@ -224,6 +225,18 @@ - (CGPoint)centerPointForPosition:(id)point withToast:(UIView *)toast {
return [self centerPointForPosition:CSToastDefaultPosition withToast:toast];
}

- (CGSize)sizeForString:(NSString *)string font:(UIFont *)font constrainedToSize:(CGSize)constrainedSize lineBreakMode:(NSLineBreakMode)lineBreakMode {
if ([string respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = lineBreakMode;
NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle};
CGRect boundingRect = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
return CGSizeMake(ceilf(boundingRect.size.width), ceilf(boundingRect.size.height));
}

return [string sizeWithFont:font constrainedToSize:constrainedSize lineBreakMode:lineBreakMode];
}

- (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UIImage *)image {
// sanity
if((message == nil) && (title == nil) && (image == nil)) return nil;
Expand Down Expand Up @@ -277,7 +290,7 @@ - (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UI

// size the title label according to the length of the text
CGSize maxSizeTitle = CGSizeMake((self.bounds.size.width * CSToastMaxWidth) - imageWidth, self.bounds.size.height * CSToastMaxHeight);
CGSize expectedSizeTitle = [title sizeWithFont:titleLabel.font constrainedToSize:maxSizeTitle lineBreakMode:titleLabel.lineBreakMode];
CGSize expectedSizeTitle = [self sizeForString:title font:titleLabel.font constrainedToSize:maxSizeTitle lineBreakMode:titleLabel.lineBreakMode];
titleLabel.frame = CGRectMake(0.0, 0.0, expectedSizeTitle.width, expectedSizeTitle.height);
}

Expand All @@ -293,7 +306,7 @@ - (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UI

// size the message label according to the length of the text
CGSize maxSizeMessage = CGSizeMake((self.bounds.size.width * CSToastMaxWidth) - imageWidth, self.bounds.size.height * CSToastMaxHeight);
CGSize expectedSizeMessage = [message sizeWithFont:messageLabel.font constrainedToSize:maxSizeMessage lineBreakMode:messageLabel.lineBreakMode];
CGSize expectedSizeMessage = [self sizeForString:message font:messageLabel.font constrainedToSize:maxSizeMessage lineBreakMode:messageLabel.lineBreakMode];
messageLabel.frame = CGRectMake(0.0, 0.0, expectedSizeMessage.width, expectedSizeMessage.height);
}

Expand All @@ -320,7 +333,6 @@ - (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UI
} else {
messageWidth = messageHeight = messageLeft = messageTop = 0.0;
}


CGFloat longerWidth = MAX(titleWidth, messageWidth);
CGFloat longerLeft = MAX(titleLeft, messageLeft);
Expand Down

0 comments on commit 8770ab7

Please sign in to comment.