diff --git a/.gitignore b/.gitignore index e43b0f9..229a754 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,9 @@ .DS_Store +*.perspective +*.perspectivev3 +*.pbxuser +*.xcworkspace +xcuserdata +build/ +DerivedData + diff --git a/MSLabel.m b/MSLabel.m index 1e58d73..2834aca 100644 --- a/MSLabel.m +++ b/MSLabel.m @@ -8,6 +8,8 @@ #import "MSLabel.h" +#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) + // small buffer to allow for characters like g,y etc static const int kAlignmentBuffer = 5; @@ -16,7 +18,6 @@ @interface MSLabel () - (void)setup; - (NSArray *)stringsFromText:(NSString *)string; -- (NSMutableArray *)stringsWithWordsWrappedFromArray:(NSArray *)strings; - (NSMutableArray *)arrayOfCharactersInString:(NSString *)string; - (NSString *)lastWordInString:(NSString *)string; @@ -67,7 +68,7 @@ - (void)drawTextInRect:(CGRect)rect [self.textColor set]; } - int numLines = slicedStrings.count; + NSInteger numLines = slicedStrings.count; if (numLines > self.numberOfLines && self.numberOfLines != 0) { numLines = self.numberOfLines; } @@ -108,9 +109,9 @@ - (void)drawTextInRect:(CGRect)rect // calculate draw X based on textAlignmentment - if (self.textAlignment == UITextAlignmentCenter) { + if (self.textAlignment == NSTextAlignmentCenter) { drawX = floorf((self.frame.size.width - [line sizeWithFont:self.font].width) / 2); - } else if (self.textAlignment == UITextAlignmentRight) { + } else if (self.textAlignment == NSTextAlignmentRight) { drawX = (self.frame.size.width - [line sizeWithFont:self.font].width); } @@ -118,7 +119,25 @@ - (void)drawTextInRect:(CGRect)rect CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), self.shadowOffset, 0, self.shadowColor.CGColor); - [line drawAtPoint:CGPointMake(drawX, drawY) forWidth:self.frame.size.width withFont:self.font fontSize:self.font.pointSize lineBreakMode:UILineBreakModeClip baselineAdjustment:UIBaselineAdjustmentNone]; + if (SYSTEM_VERSION_LESS_THAN(@"7.0")) { + // NOTE: Used to be UILineBreakModeClip but is now deprecated. Checking the headers UILineBreakModeClip == NSLineBreakByClipping, + // so this is safe even below iOS 6 if using xcode > 4.0. + [line drawAtPoint:CGPointMake(drawX, drawY) forWidth:self.frame.size.width withFont:self.font fontSize:self.font.pointSize lineBreakMode:NSLineBreakByClipping baselineAdjustment:UIBaselineAdjustmentNone]; + } else { + NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; + paragraphStyle.lineBreakMode = NSLineBreakByClipping; + + NSShadow *shadowStyle = [[NSShadow alloc] init]; + shadowStyle.shadowColor = self.shadowColor; + shadowStyle.shadowOffset = self.shadowOffset; + + [line drawAtPoint:CGPointMake(drawX, drawY) withAttributes:@{ + NSParagraphStyleAttributeName : paragraphStyle, + NSFontAttributeName : self.font, + NSForegroundColorAttributeName : self.textColor, + NSShadowAttributeName : shadowStyle + }]; + } } } @@ -141,13 +160,12 @@ - (void)setLineHeight:(int)lineHeight - (void)setup { _lineHeight = 12; - self.minimumFontSize = 12; _verticalAlignment = MSLabelVerticalAlignmentMiddle; } - (NSArray *)stringsFromText:(NSString *)string { - if (self.lineBreakMode == UILineBreakModeWordWrap) { + if (self.lineBreakMode == (SYSTEM_VERSION_LESS_THAN(@"6.0") ? UILineBreakModeWordWrap : NSLineBreakByWordWrapping)) { return [self stringsWithWordsWrappedFromString:string]; } @@ -185,7 +203,7 @@ - (NSArray *)stringsFromText:(NSString *)string { break; } } - if (self.lineBreakMode == UILineBreakModeWordWrap) { + if (self.lineBreakMode == (SYSTEM_VERSION_LESS_THAN(@"6.0") ? UILineBreakModeWordWrap : NSLineBreakByWordWrapping)) { [slicedString addObject:line]; } else { [slicedString addObject:[line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]]; @@ -194,11 +212,6 @@ - (NSArray *)stringsFromText:(NSString *)string { [characterArray removeObjectsAtIndexes:charsToRemove]; } - if (self.lineBreakMode == UILineBreakModeWordWrap) { - slicedString = [self stringsWithWordsWrappedFromArray:slicedString]; - } - - return slicedString; } @@ -207,7 +220,7 @@ - (NSMutableArray *)stringsWithWordsWrappedFromString:(NSString *)string { NSCharacterSet *delimiterCharacterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSArray *words = [string componentsSeparatedByCharactersInSet:delimiterCharacterSet]; - NSMutableArray *outputLines = [[[NSMutableArray alloc] init] autorelease]; + NSMutableArray *outputLines = [[NSMutableArray alloc] init]; int lineNumber = 0;