浏览代码

jumbomoji size calculation code

somehow I can not get it to change the actual font size though :(
Simon Laux 2 年之前
父节点
当前提交
c89394cbb0
共有 1 个文件被更改,包括 46 次插入0 次删除
  1. 46 0
      deltachat-ios/Chat/Views/Cells/TextMessageCell.swift

+ 46 - 0
deltachat-ios/Chat/Views/Cells/TextMessageCell.swift

@@ -13,6 +13,28 @@ class TextMessageCell: BaseMessageCell {
 
 
     override func update(dcContext: DcContext, msg: DcMsg, messageStyle: UIRectCorner, showAvatar: Bool, showName: Bool, searchText: String?, highlight: Bool) {
     override func update(dcContext: DcContext, msg: DcMsg, messageStyle: UIRectCorner, showAvatar: Bool, showName: Bool, searchText: String?, highlight: Bool) {
         messageLabel.text = msg.text
         messageLabel.text = msg.text
+        
+        var fontSize = UIFont.preferredFont(for: .body, weight: .regular).pointSize
+        // calculate jumbomoji size
+        if msg.text != nil {
+            let text = msg.text! // simon: not sure how we can get rid of this `!`
+            let charCount = text.count
+            // simon: as far as I understood, this iterates over the whole string to find out how many unicode clusters there are,
+            // so we might wanna cache it here instead of calculating it twice
+            if charCount <= 8 && text.containsOnlyEmoji {
+                if charCount <= 2 {
+                    fontSize *= 3.0
+                } else if charCount <= 4 {
+                    fontSize *= 2.5
+                } else if charCount <= 6 {
+                    fontSize *= 1.75
+                } else {
+                    fontSize *= 1.35
+                }
+            }
+        }
+        messageLabel.font = messageLabel.font.withSize(fontSize)
+        // messageLabel.traitCollection
 
 
         super.update(dcContext: dcContext,
         super.update(dcContext: dcContext,
                      msg: msg,
                      msg: msg,
@@ -28,3 +50,27 @@ class TextMessageCell: BaseMessageCell {
     }
     }
     
     
 }
 }
+
+
+
+// required extentions to Character and String
+// thanks to https://stackoverflow.com/a/39425959
+
+extension Character {
+    var isSimpleEmoji: Bool {
+        guard let firstScalar = unicodeScalars.first else {
+            return false
+        }
+        return firstScalar.properties.isEmoji && firstScalar.value > 0x238C
+    }
+    var isCombinedIntoEmoji: Bool {
+        unicodeScalars.count > 1 && unicodeScalars.first?.properties.isEmoji ?? false
+    }
+    var isEmoji: Bool { isSimpleEmoji || isCombinedIntoEmoji }
+}
+
+extension String {
+    var containsOnlyEmoji: Bool {
+        return !isEmpty && !contains { !$0.isEmoji }
+    }
+}