Max
Max

Reputation: 2299

emoji is clipped in UILabel in iOS

Hello I'm working on chat app in which user can send smiley (emoji) to other user. For text its working perfect but when it contains emoji then it is correct. I have used method for this

- (CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize {
    CGSize constraintSize;
    constraintSize.height = MAXFLOAT;
    constraintSize.width = width;
    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont fontWithName:fontName size:fontSize], NSFontAttributeName,
                                          nil];

    CGRect frame = [text boundingRectWithSize:constraintSize
                                      options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                   attributes:attributesDictionary
                                      context:nil];

    CGSize stringSize = frame.size;
    return stringSize;
}

and the results are something like image 1 image 2 image 3

As from images you can see what i want to say and what i need. I need some between two lines when there is emoji text in UILabel. Any help will be appreciated. Thanks in advance.

Edit: I have given answer on this. But if some have some quick hack for this then that will be great for me.

Upvotes: 2

Views: 1377

Answers (2)

E. Rivera
E. Rivera

Reputation: 10938

I just increased the required height of the label and adjusted its origin.

    // Expand label to avoid cutoff emojis
    label.frame.size.height += 8.0
    label.frame.origin.y -= 4.0

Upvotes: 1

Max
Max

Reputation: 2299

I have made a good work to do it manually and it looks good for me now. I have added linespacing for label text and it works good for me. I have set text like

NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",message_text]];
            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            [style setLineSpacing:4];
            [attrString addAttribute:NSParagraphStyleAttributeName
                               value:style
                               range:NSMakeRange(0, [message_text length])];
            lblChatMSG.attributedText = attrString;

and my method to get height of text is

- (CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize {
    CGSize constraintSize;
    constraintSize.height = MAXFLOAT;
    constraintSize.width = width;

    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.lineSpacing = 4;

    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont fontWithName:fontName size:fontSize], NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName,
                                          nil];

    CGRect frame = [text boundingRectWithSize:constraintSize
                                      options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                   attributes:attributesDictionary
                                      context:nil];

    CGSize stringSize = frame.size;
    return stringSize;
} 

And the result is image result for emoji with good space in lines

Upvotes: 1

Related Questions