Text computing height with Emoji expression

Text computing height with Emoji expression
screen snapshot 2016-11-13 PM 9.55.05.png

To meet a demand of
the development community, there is a demand for products to make the same space and QQ comments, commenting on the content to display the nickname, nickname and click to enter personal homepage. Check a lot of information, see the YYText and TTTAttributedLabel’s official article, did not find their final results, try holding the attitude, the true demand, record, share to you, hope you can help a
effect chart

Text computing height with Emoji expression
7E8F2CA3-1F82-4993-9423-AE46D6FFF4EA.png

This is the final renderings, click on the light blue text will jump to the personal home page, but there is a little problem, the text of the spacing also need to fine tune. But the idea has been.
first said he encountered problems. Click on the text link with the YYText, it is very simple to achieve, but the problem is that when the string contains Emoji expression, the line spacing appears on the issue. That’s what we need.

Text computing height with Emoji expression
screen snapshot 2016-11-13 PM 9.22.58.png

But after using YYText or TTTAttributedLabel, it becomes like this

Text computing height with Emoji expression
screen snapshot 2016-11-13 PM 9.26.41.png

spacing is not the same

First of all, for a string containing Emoji expression, use UILabel to show no problem, distance is the same, but after using YYText or TTTAttributedLabel to add the text Click event will become containing Emoji expression for specific characters of the normal high to high.
thought YYText or TTTAttributedLabel itself method can be set, unfortunately, I did not find (if you know, please tell me), but can only use more stupid way.
said the first I thought: as a result of the current row containing Emoji expression is higher than expected, so think of, can obtain the contents of each cycle, then each traversal of the text, whether with friends Emoji expression, do not deal with if there is, if not this set the row the high expression and Emoji containing the same height, the problem is not solved it.
the first step: first to get, each line of text

For each line of text / * * * * * @param text * @param font text font @param maxWidth * * * @return the maximum width of the container to store each line array - * / (NSArray *) getSeparatedLinesFromtext: (* NSString) text font: (UIFont *) font maxWidth: (CGFloat) maxWidth myFont ({CTFontRef = 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 CGMutableP); AthRef = CGPathCreateMutable (path); CGPathAddRect (path, NULL, CGRectMake (0,0, maxWidth, 100000)); CTFrameRef frame = CTFramesetterCreateFrame (frameSetter, CFRangeMake (0, 0), path, NULL); NSArray *lines = (__bridge * NSArray) CTFrameGetLines (frame); NSMutableArray *linesArray = [[NSMutableArray (ID alloc]init]; for line in lines CTLineRef (__bridge) {lineRef = line; lineRange = CTLineRef) CFRange CTLineGetStringRange (lineRef); NSRange range = NSMakeRange (lineRange.location, lineRange.length); NSString *lineString = [text substringWithRange:range]; [linesArray addObject:lineString];} return (NSArray * linesArray);}

Using this method, you can get the contents of each line of text.
second step: to determine whether Emoji expression

To determine whether Emoji expression containing / / + (BOOL) stringContainsEmoji: (NSString *) string BOOL returnValue {__block =NO; [string enumerateSubstringsInRange:NSMakeRange (0 [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^ (NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {const unichar HS = [substring characterAtIndex:0]; / / surrogate pair if (0xd800) {if (0xd800 < HS = & & = HS; < 0xdbff) {if (substring.length > 1) {const unichar LS = [substring characterAtIndex:1]; const int (UC = (HS - 0xd800) * 0x400) + (LS - 0xdc00) + 0x10000 (0x1d000 = < if; UC & & UC < 0x1f77f returnValue = {=YES}});}else if (substring.length > 1) {const unichar LS = [substring characterAtIndex:1]; if (LS = = 0x20e3) {returnValue =YES;}}else {/ / non surrogate if (0x2100 < HS = & HS = & < 0x27ff returnValue =YES}else) {if (0x2B05; < = HS; & & HS = < 0x2b07) {returnValue =YES;}else if (0x2934 < HS = & & = HS; < 0x2935) {returnValue =YES; 0x3297 < if (}else = HS & & Amp; HS < 0x3299) {returnValue = =YES;}else if (HS = = 0xa9 HS = = 0xae HS = || || 0x303d || || HS HS = = 0x3030 = = 0x2b55 HS = 0x2b1c || || || HS HS = = 0x2b1b = = 0x2b50) {returnValue}}}}]; =YES; return returnValue;}

The third step: the cycle to store each line of text content array

- (NSMutableAttributedString) changeLineSpacing: (NSArray * stringList) {NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] init]; for (NSString *string in stringList) {/ / if there is Emoji expression, do not deal with if ([NSString stringContainsEmoji:string]) {NSMutableAttributedString *contentEmojistring = [[NSMutableAttributedString alloc] initWithString:string]; [mutableString appendAttributedString:contentEmojistring];}else {/ / otherwise set for as high as 4 paragraph style. (the height to try slowly according to their own needs) NSMutableAttributedString *unContentEmojistring = [[NSMutableAttributedString alloc] initWithString:string]; NSMutableParagrap HStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 4; [unContentEmojistring addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange (0 [unContentEmojistring length]) [mutableString]; appendAttributedString:unContentEmojistring] return mutableString;}}}; / / return the final string processing completed

This is what you see

Text computing height with Emoji expression
screen snapshot 2016-11-13 PM 9.41.33.png

This is the realization of the demand.