From 8770ab7b8cd4972083372ab1d96b0bfb6ff07104 Mon Sep 17 00:00:00 2001 From: Charles Scalesse Date: Mon, 30 Dec 2013 10:44:44 -0500 Subject: [PATCH] Use boundingRectWithSize: instead of sizeWithFont: in iOS7. fixes #20 --- Toast/Toast/Toast+UIView.m | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Toast/Toast/Toast+UIView.m b/Toast/Toast/Toast+UIView.m index 9e2ad71..1232e7d 100644 --- a/Toast/Toast/Toast+UIView.m +++ b/Toast/Toast/Toast+UIView.m @@ -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 @@ -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; @@ -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); } @@ -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); } @@ -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);