How to show Emoji in UILabel iOS

2019-02-03 04:15发布

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.

5条回答
Animai°情兽
2楼-- · 2019-02-03 04:36

Swift 3.0 solution update given by @Bhavesh Nayi

    let data =  txtMessage.text!.data(using: .nonLossyASCII)
    let emojiString = String.init(data: data!, encoding: .utf8)

and Send it to Chat Server as a

    let data = message.message.data(using: .utf8)
    let emoji = String.init(data: data!, encoding: .nonLossyASCII)

and Then Show emoji to UILablel

查看更多
乱世女痞
3楼-- · 2019-02-03 04:38

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                                                                     
查看更多
干净又极端
4楼-- · 2019-02-03 04:44

In case of swift 2.3 or lower

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

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

In Case of Swift 3.0 or higher

let textViewData = textViewString.data(using: .nonLossyASCII)
let valueUniCode = String.init(data: textViewData!, encoding: .utf8)!

let emojData   = valueUniCode.data(using: .utf8)
let emojString = String.init(data: emojData, encoding: .nonLossyASCII)
查看更多
戒情不戒烟
5楼-- · 2019-02-03 04:50
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)!
查看更多
做自己的国王
6楼-- · 2019-02-03 04:56

On Xcode version 7.2.1, You can use the below shortcut to show the symbols panels and insert the emoji:

shortcut: (pressed the below three keys together)

Ctrl Command Space
查看更多
登录 后发表回答