2017-07-07 1 views
-4

J'ai récemment vu une application qui incorporait un sélecteur emoji comme un moyen de réagir à certaines choses. Je voulais mettre en œuvre un concept similaire dans mon application, tout le monde sait comment je peux créer quelque chose comme cette photo. Je pensais utiliser une collection et avoir chaque cellule comme emoji. Des pensées ou de meilleures méthodes?Emoji Picker Ios Swift

enter image description here

Répondre

2

Pour mettre emojis en CollectionView, faire comme:

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 
} 

Avec cette 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 
} 

} Gardez à l'esprit que les plages de emoji unicode pourraient ne pas inclure tous les emoji et vous pourriez vouloir rechercher et modifier les gammes pour votre application

+0

Je reçois erreur d'identificateur non résolu pour NSAttributedStringKey – user6520705

+0

Ma réponse fonctionne dans swift 4 ... il y a d'autres réponses sur d'autres postes de débordement de pile – Ali