表情符号选择器Ios Swift [英] Emoji Picker Ios Swift

查看:100
本文介绍了表情符号选择器Ios Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我看到一个应用程序,其中包含表情符号选择器,以对某些事物做出反应。我想在我的应用程序中实现类似的概念,任何人都知道我可以如何创建类似这张照片的内容。我当时正在考虑使用collectionview,并将每个单元格都设为自己的表情符号。有什么想法或更好的方法吗?

I saw an app recently that incorporated an emoji picker as a way to react to certain things. I wanted to implement a similar concept into my app, anyone know how I can create something like this photo. I was thinking to use a collectionview and have each cell be its own emoji. Any thoughts or better methods?

< img src = https://i.stack.imgur.com/SrhLf.png alt =在此处输入图片描述>

推荐答案

要将表情符号放入collectionView,请执行以下操作:

To put emojis into a collectionView, do such:

var emojiList: [[String]] = []
let sectionTitle: [String] = ["Emoticons", "Dingbats", "Transport and map symbols", "Enclosed Characters"]

override func layoutSubviews() {
    super.layoutSubviews()
    fetchEmojis()
}

func fetchEmojis(){

    let emojiRanges = [
        0x1F601...0x1F64F,
        0x2702...0x27B0,
        0x1F680...0x1F6C0,
        0x1F170...0x1F251
    ]

    for range in emojiRanges {
        var array: [String] = []
        for i in range {
            if let unicodeScalar = UnicodeScalar(i){
                array.append(String(describing: unicodeScalar))
            }
        }

        emojiList.append(array)
    }
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! DragCell
    cell.imageView.image = emojiList[indexPath.section][indexPath.item].image()
    return cell
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return emojiList[section].count
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return emojiList.count
}

具有以下扩展名:

extension String {

    func image() -> UIImage? {
        let size = CGSize(width: 100, height: 100)
        UIGraphicsBeginImageContextWithOptions(size, false, 0);
        UIColor.clear.set()

        let stringBounds = (self as NSString).size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 75)])
        let originX = (size.width - stringBounds.width)/2
        let originY = (size.height - stringBounds.height)/2
        print(stringBounds)
        let rect = CGRect(origin: CGPoint(x: originX, y: originY), size: size)
        UIRectFill(rect)

        (self as NSString).draw(in: rect, withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 75)])

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

}

请记住,Unicode表情符号范围可能不包括所有表情符号,您可能想要查找并修改您应用的范围

Keep in mind that the unicode emoji ranges might not include all the emoji and you might want to look up and modify the ranges for your app

这篇关于表情符号选择器Ios Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆