Emoji Picker Ios Swift [closed]

纵饮孤独 提交于 2019-12-13 22:12:09

问题


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?


回答1:


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
}

With this extension:

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
}

} 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



来源:https://stackoverflow.com/questions/44977646/emoji-picker-ios-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!