Переглянути джерело

Merge pull request #1807 from deltachat/jumbomoji

jumbomoji size calculation code
cyBerta 2 роки тому
батько
коміт
d8cae9744f

+ 4 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		21D54500299415B9008B54D5 /* Character+Extentions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D544FF299415B9008B54D5 /* Character+Extentions.swift */; };
 		21D6C941260623F500D0755A /* NotificationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D6C9392606190600D0755A /* NotificationManager.swift */; };
 		3008CB7224F93EB900E6A617 /* AudioMessageCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3008CB7124F93EB900E6A617 /* AudioMessageCell.swift */; };
 		3008CB7424F9436C00E6A617 /* AudioPlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3008CB7324F9436C00E6A617 /* AudioPlayerView.swift */; };
@@ -250,6 +251,7 @@
 
 /* Begin PBXFileReference section */
 		08432784282DC739B8EAC1E2 /* Pods-DcShare.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DcShare.debug.xcconfig"; path = "Pods/Target Support Files/Pods-DcShare/Pods-DcShare.debug.xcconfig"; sourceTree = "<group>"; };
+		21D544FF299415B9008B54D5 /* Character+Extentions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Character+Extentions.swift"; sourceTree = "<group>"; };
 		21D6C9392606190600D0755A /* NotificationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationManager.swift; sourceTree = "<group>"; };
 		21EE28844E7A690D73BF5285 /* Pods-deltachat-iosTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-deltachat-iosTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-deltachat-iosTests/Pods-deltachat-iosTests.debug.xcconfig"; sourceTree = "<group>"; };
 		2F7009234DB9408201A6CDCB /* Pods_deltachat_iosTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_deltachat_iosTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -651,6 +653,7 @@
 				302B84CD2397F6CD001C261F /* URL+Extension.swift */,
 				AE0AA957247834A400D42A7F /* Date+Extension.swift */,
 				AE6EC5232497663200A400E4 /* UIImageView+Extensions.swift */,
+				21D544FF299415B9008B54D5 /* Character+Extentions.swift */,
 			);
 			path = Extensions;
 			sourceTree = "<group>";
@@ -1570,6 +1573,7 @@
 				3080A028277DE12D00E74565 /* InputBarSendButton.swift in Sources */,
 				AE0AA958247834A400D42A7F /* Date+Extension.swift in Sources */,
 				307D822E241669C7006D2490 /* LocationManager.swift in Sources */,
+				21D54500299415B9008B54D5 /* Character+Extentions.swift in Sources */,
 				30238CFF28A5554C00EF14AC /* FileHelper.swift in Sources */,
 				AE851AD0227DF50900ED86F0 /* GroupChatDetailViewController.swift in Sources */,
 				30FDB72124D838240066C48D /* BaseMessageCell.swift in Sources */,

+ 1 - 1
deltachat-ios/Chat/Views/Cells/BaseMessageCell.swift

@@ -452,7 +452,7 @@ public class BaseMessageCell: UITableViewCell {
             quoteView.isHidden = true
         }
 
-        messageLabel.attributedText = MessageUtils.getFormattedSearchResultMessage(messageText: msg.text,
+        messageLabel.attributedText = MessageUtils.getFormattedTextMessage(messageText: msg.text,
                                                                                    searchText: searchText,
                                                                                    highlight: highlight)
 

+ 17 - 0
deltachat-ios/Extensions/Character+Extentions.swift

@@ -0,0 +1,17 @@
+import Foundation
+
+// required for jumbomoji logic
+// 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 }
+}
+

+ 7 - 0
deltachat-ios/Extensions/String+Extension.swift

@@ -81,4 +81,11 @@ extension String {
             return "\(number)"
         }
     }
+
+    // required for jumbomoji logic
+    // thanks to https://stackoverflow.com/a/39425959
+    var containsOnlyEmoji: Bool {
+        // Character.isEmoji is defined in deltachat-ios/Extensions/Character+Extension.swift
+        return !isEmpty && !contains { !$0.isEmoji }
+    }
 }

+ 18 - 2
deltachat-ios/Helper/MessageUtils.swift

@@ -122,10 +122,26 @@ public class MessageUtils {
         return "\(date) \(padlock) \(sendingState)"
     }
 
-    public static func getFormattedSearchResultMessage(messageText: String?, searchText: String?, highlight: Bool) -> NSAttributedString? {
+    public static func getFormattedTextMessage(messageText: String?, searchText: String?, highlight: Bool) -> NSAttributedString? {
         if let messageText = messageText {
+
+            var fontSize = UIFont.preferredFont(for: .body, weight: .regular).pointSize
+            // calculate jumbomoji size
+            let charCount = messageText.count
+            if charCount <= 8 && messageText.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
+                }
+            }
+
             let fontAttributes: [NSAttributedString.Key: Any] = [
-                .font: UIFont.preferredFont(for: .body, weight: .regular),
+                .font: UIFont.systemFont(ofSize: fontSize),
                 .foregroundColor: DcColors.defaultTextColor
             ]
             let mutableAttributedString = NSMutableAttributedString(string: messageText, attributes: fontAttributes)