mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-04 01:20:12 +00:00
Implement all emoji text message boosting
This commit is contained in:
committed by
Stefan Ceriu
parent
e6711a0236
commit
2ebe534fb3
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// Copyright 2023 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension UnicodeScalar {
|
||||
var isZeroWidthJoiner: Bool {
|
||||
value == 8205
|
||||
}
|
||||
|
||||
var isKeycap: Bool {
|
||||
value == 8419
|
||||
}
|
||||
|
||||
var isNumber: Bool {
|
||||
switch value {
|
||||
case 48...57:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
var containsOnlyEmoji: Bool {
|
||||
guard !isEmpty else {
|
||||
return false
|
||||
}
|
||||
|
||||
var emojiMarkerCount = 0
|
||||
for scalar in unicodeScalars {
|
||||
let isEmojiMarker = scalar.properties.isEmoji ||
|
||||
scalar.properties.isEmojiPresentation ||
|
||||
scalar.isZeroWidthJoiner ||
|
||||
scalar.properties.isDefaultIgnorableCodePoint ||
|
||||
scalar.isKeycap
|
||||
|
||||
guard isEmojiMarker else {
|
||||
return false
|
||||
}
|
||||
|
||||
emojiMarkerCount += 1
|
||||
}
|
||||
|
||||
// Plain numbers like 0 return true for .isEmoji. We don't want that
|
||||
let markersRequiringSiblings = unicodeScalars.filter {
|
||||
$0.properties.isEmoji && $0.isNumber
|
||||
}
|
||||
|
||||
return markersRequiringSiblings.count != emojiMarkerCount && emojiMarkerCount == unicodeScalars.count
|
||||
}
|
||||
}
|
||||
@@ -22,16 +22,35 @@ struct FormattedBodyText: View {
|
||||
|
||||
private let attributedString: AttributedString
|
||||
private let additionalWhitespacesCount: Int
|
||||
private let boostEmojiSize: Bool
|
||||
|
||||
private var attributedComponents: [AttributedStringBuilderComponent] {
|
||||
var attributedString = attributedString
|
||||
attributedString.append(AttributedString(stringLiteral: additionalWhitespacesSuffix))
|
||||
return attributedString.formattedComponents
|
||||
var adjustedAttributedString = attributedString
|
||||
adjustedAttributedString.append(AttributedString(stringLiteral: additionalWhitespacesSuffix))
|
||||
|
||||
let string = String(attributedString.characters)
|
||||
|
||||
if boostEmojiSize,
|
||||
string.containsOnlyEmoji,
|
||||
let range = adjustedAttributedString.range(of: string) {
|
||||
adjustedAttributedString[range].font = .system(size: 48.0)
|
||||
}
|
||||
|
||||
return adjustedAttributedString.formattedComponents
|
||||
}
|
||||
|
||||
init(attributedString: AttributedString, additionalWhitespacesCount: Int = 0) {
|
||||
init(attributedString: AttributedString,
|
||||
additionalWhitespacesCount: Int = 0,
|
||||
boostEmojiSize: Bool = false) {
|
||||
self.attributedString = attributedString
|
||||
self.additionalWhitespacesCount = additionalWhitespacesCount
|
||||
self.boostEmojiSize = boostEmojiSize
|
||||
}
|
||||
|
||||
init(text: String, additionalWhitespacesCount: Int = 0, boostEmojiSize: Bool = false) {
|
||||
self.init(attributedString: AttributedString(text),
|
||||
additionalWhitespacesCount: additionalWhitespacesCount,
|
||||
boostEmojiSize: boostEmojiSize)
|
||||
}
|
||||
|
||||
// These is needed to create the slightly off inlined timestamp effect
|
||||
@@ -123,12 +142,6 @@ struct FormattedBodyText: View {
|
||||
}
|
||||
}
|
||||
|
||||
extension FormattedBodyText {
|
||||
init(text: String, additionalWhitespacesCount: Int = 0) {
|
||||
self.init(attributedString: AttributedString(text), additionalWhitespacesCount: additionalWhitespacesCount)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Previews
|
||||
|
||||
struct FormattedBodyText_Previews: PreviewProvider {
|
||||
|
||||
@@ -24,9 +24,13 @@ struct TextRoomTimelineView: View, TextBasedRoomTimelineViewProtocol {
|
||||
var body: some View {
|
||||
TimelineStyler(timelineItem: timelineItem) {
|
||||
if let attributedString = timelineItem.content.formattedBody {
|
||||
FormattedBodyText(attributedString: attributedString, additionalWhitespacesCount: additionalWhitespaces)
|
||||
FormattedBodyText(attributedString: attributedString,
|
||||
additionalWhitespacesCount: additionalWhitespaces,
|
||||
boostEmojiSize: true)
|
||||
} else {
|
||||
FormattedBodyText(text: timelineItem.body, additionalWhitespacesCount: additionalWhitespaces)
|
||||
FormattedBodyText(text: timelineItem.body,
|
||||
additionalWhitespacesCount: additionalWhitespaces,
|
||||
boostEmojiSize: true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user