aboutsummaryrefslogtreecommitdiff
path: root/iPhone
diff options
context:
space:
mode:
authorGravatar gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2014-08-27 14:00:06 +0000
committerGravatar gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2014-08-27 14:00:06 +0000
commit3f51f8dc7f6193c7e9610e49b0853856645c2656 (patch)
tree000108c792bb121457322a4938f80da4380aba03 /iPhone
parentbeb4c1c4a1ccae659f777f004d866f1b372d518d (diff)
Protect against inserting nil into dictionaries.
The iOS 7 APIs for drawing strings require NSDictionary parameters so make sure there's no way to insert a nil value when creating the dictionaries or the app will crash. DELTA=35 (10 added, 0 deleted, 25 changed) DELTA_BY_EXTENSION=m=35
Diffstat (limited to 'iPhone')
-rwxr-xr-xiPhone/GTMFadeTruncatingLabel.m60
1 files changed, 35 insertions, 25 deletions
diff --git a/iPhone/GTMFadeTruncatingLabel.m b/iPhone/GTMFadeTruncatingLabel.m
index 3be7688..5ca6d25 100755
--- a/iPhone/GTMFadeTruncatingLabel.m
+++ b/iPhone/GTMFadeTruncatingLabel.m
@@ -53,7 +53,13 @@
// |sizeWithFont:| is deprecated in iOS 7, replaced by |sizeWithAttributes:|
CGSize size = [self.text sizeWithFont:self.font];
#else
- CGSize size = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
+ CGSize size = CGSizeZero();
+ if (self.font) {
+ size = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
+ // sizeWithAttributes: may return fractional values, so ceil the width and
+ // height to preserve the behavior of sizeWithFont:.
+ size = CGSizeMake(ceil(size.width), ceil(size.height));
+ }
#endif
if (size.width > requestedRect.size.width) {
UIImage* image = [[self class]
@@ -67,25 +73,27 @@
CGRect shadowRect = CGRectOffset(requestedRect, self.shadowOffset.width,
self.shadowOffset.height);
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
- // |drawInRect:withFont:lineBreakMode:alignment:| is deprecated in iOS 7,
- // replaced by |drawInRect:withAttributes:|
+ // |drawInRect:withFont:lineBreakMode:alignment:| is deprecated in iOS 7,
+ // replaced by |drawInRect:withAttributes:|
CGContextSetFillColorWithColor(context, self.shadowColor.CGColor);
[self.text drawInRect:shadowRect
withFont:self.font
lineBreakMode:self.lineBreakMode
alignment:self.textAlignment];
#else
- NSMutableParagraphStyle* textStyle =
- [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
- textStyle.lineBreakMode = self.lineBreakMode;
- textStyle.alignment = self.textAlignment;
- NSDictionary* attributes = @{
- NSFontAttributeName:self.font,
- NSParagraphStyleAttributeName:textStyle,
- NSForegroundColorAttributeName:self.shadowColor
- };
- [self.text drawInRect:shadowRect
- withAttributes:attributes];
+ if (self.font) {
+ NSMutableParagraphStyle* textStyle =
+ [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
+ textStyle.lineBreakMode = self.lineBreakMode;
+ textStyle.alignment = self.textAlignment;
+ NSDictionary* attributes = @{
+ NSFontAttributeName:self.font,
+ NSParagraphStyleAttributeName:textStyle,
+ NSForegroundColorAttributeName:self.shadowColor
+ };
+ [self.text drawInRect:shadowRect
+ withAttributes:attributes];
+ }
#endif
}
@@ -109,17 +117,19 @@
lineBreakMode:self.lineBreakMode
alignment:self.textAlignment];
#else
- NSMutableParagraphStyle* textStyle =
- [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
- textStyle.lineBreakMode = self.lineBreakMode;
- textStyle.alignment = self.textAlignment;
- NSDictionary* attributes = @{
- NSFontAttributeName:self.font,
- NSParagraphStyleAttributeName:textStyle,
- NSForegroundColorAttributeName:self.textColor
- };
- [self.text drawInRect:requestedRect
- withAttributes:attributes];
+ if (self.font) {
+ NSMutableParagraphStyle* textStyle =
+ [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
+ textStyle.lineBreakMode = self.lineBreakMode;
+ textStyle.alignment = self.textAlignment;
+ NSDictionary* attributes = @{
+ NSFontAttributeName:self.font,
+ NSParagraphStyleAttributeName:textStyle,
+ NSForegroundColorAttributeName:self.textColor
+ };
+ [self.text drawInRect:requestedRect
+ withAttributes:attributes];
+ }
#endif
CGContextRestoreGState(context);
}