Created
April 15, 2014 19:20
-
-
Save hery/10761956 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label | |
{ | |
NSString *text = [label text]; | |
UIFont *font = [label font]; | |
CGRect rect = [label frame]; | |
CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL); | |
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text]; | |
[attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)]; | |
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attStr); | |
CGMutablePathRef path = CGPathCreateMutable(); | |
CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000)); | |
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL); | |
NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame); | |
NSMutableArray *linesArray = [[NSMutableArray alloc]init]; | |
for (id line in lines) | |
{ | |
CTLineRef lineRef = (__bridge CTLineRef )line; | |
CFRange lineRange = CTLineGetStringRange(lineRef); | |
NSRange range = NSMakeRange(lineRange.location, lineRange.length); | |
NSString *lineString = [text substringWithRange:range]; | |
[linesArray addObject:lineString]; | |
} | |
CGPathRelease(path); | |
CFRelease( frame ); | |
CFRelease(frameSetter); | |
return (NSArray *)linesArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment