Emoji Picker Ios Swift

时间:2017-07-07 18:38:24

标签: ios swift emoji

我最近看到一个应用程序,它将表情符号选择器作为对某些事物做出反应的一种方式。我想在我的应用程序中实现类似的概念,任何人都知道如何创建像这张照片的东西。我正在考虑使用集合视图并让每个单元格成为它自己的表情符号。有什么想法或更好的方法吗?

enter image description here

1 个答案:

答案 0 :(得分:4)

要将emojis放入collectionView,请执行以下操作:

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表情符号范围可能不包括所有表情符号,您可能需要查找并修改应用程序的范围