简体   繁体   中英

How to set unicode emoji to the the title of a UIButton in Swift 3?

EDIT: found a way to convert it with lots of conversion, but seems sloppy:

let myStr = "1f601"
let str = String(Character(UnicodeScalar(Int(myStr, radix: 16)!)!))

I'm trying to do the following:

for _ in 1...2 {
    let emojiButton               = UIButton()
    emojiButton.backgroundColor   = UIColor.green
    emojiButton.heightAnchor.constraint(equalToConstant: 20.0).isActive = true
    emojiButton.titleLabel!.font = UIFont(name: "AppleColorEmoji", size: CGFloat(16.0))
    emojiButton.setTitle(str, for: .normal)

    stackView.addArrangedSubview(emojiButton)

}

This for some reason gives two different symbols rather than the expected emoji, here's the result:

在此处输入图片说明

So now I'm confused as to why the %C format isn't showing unicode, and also why it's showing two different characters.

let myStr = "\u{1f601}"

这可能是在Swift中指定Unicode点的最简单方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM