How to show Emoji in UILabel iOS

2019-02-03 04:34发布

问题:

I have to show the textview text smily in UILabel.

In the UILabel -

lbl.text = @"Happy to help you \U0001F431;

its showing properly.

In UITextView -

I tried to convert UITextView text in string and then log is -

%F0%9F%99%88%F0%9F%99%89%F0%9F%99%8A

How to encode which i can show in UILabel, anybody please suggest me.

回答1:

NSString *str = @"Happy to help you \U0001F431";

NSData *data = [str dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *valueUnicode = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];


NSData *dataa = [valueUnicode dataUsingEncoding:NSUTF8StringEncoding];
NSString *valueEmoj = [[NSString alloc] initWithData:dataa encoding:NSNonLossyASCIIStringEncoding];

_lbl.text = valueEmoj;

SWIFT - 3 OR HIGHER

    let str = "Happy to help you \U0001F431"

    let data : NSData = str.dataUsingEncoding(NSNonLossyASCIIStringEncoding)!
    let valueUnicode : String = String(data: data, encoding: NSUTF8StringEncoding)!

    let dataa   : NSData = valueUniCode.dataUsingEncoding(NSUTF8StringEncoding)!
    let valueEmoj : String = String(data: dataa, encoding: NSNonLossyASCIIStringEncoding)!


回答2:

You can use ⌃ ⌘ Space shortcut to show the symbols panels and just insert the emoji you're looking for directly without unicode:

lbl.text = @"Happy to help you