2020-08-21 02:29:01 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
2020-10-30 07:11:24 +00:00
|
|
|
import Mastodon
|
2020-08-21 02:29:01 +00:00
|
|
|
import UIKit
|
2021-02-22 07:10:34 +00:00
|
|
|
import ViewModels
|
2020-08-21 02:29:01 +00:00
|
|
|
|
|
|
|
extension String {
|
2021-02-02 18:02:30 +00:00
|
|
|
static var separator: Self {
|
|
|
|
(Locale.autoupdatingCurrent.groupingSeparator ?? ",").appending(" ")
|
|
|
|
}
|
|
|
|
|
2021-01-19 00:46:38 +00:00
|
|
|
func height(width: CGFloat, font: UIFont) -> CGFloat {
|
|
|
|
(self as NSString).boundingRect(
|
|
|
|
with: CGSize(width: width, height: .greatestFiniteMagnitude),
|
|
|
|
options: .usesLineFragmentOrigin,
|
|
|
|
attributes: [.font: font],
|
|
|
|
context: nil)
|
|
|
|
.height
|
|
|
|
}
|
|
|
|
|
2020-08-21 02:29:01 +00:00
|
|
|
func countEmphasizedAttributedString(count: Int, highlighted: Bool = false) -> NSAttributedString {
|
|
|
|
let countRange = (self as NSString).range(of: String.localizedStringWithFormat("%ld", count))
|
|
|
|
|
|
|
|
let attributed = NSMutableAttributedString(
|
|
|
|
string: self,
|
|
|
|
attributes: [
|
|
|
|
.font: UIFont.preferredFont(forTextStyle: .body),
|
|
|
|
.foregroundColor: highlighted ? UIColor.tertiaryLabel : UIColor.secondaryLabel
|
|
|
|
])
|
|
|
|
attributed.addAttributes(
|
|
|
|
[
|
|
|
|
.font: UIFont.preferredFont(forTextStyle: .headline),
|
|
|
|
.foregroundColor: highlighted ? UIColor.secondaryLabel : UIColor.label
|
|
|
|
],
|
|
|
|
range: countRange)
|
|
|
|
|
|
|
|
return attributed
|
|
|
|
}
|
2020-10-30 07:11:24 +00:00
|
|
|
|
2021-02-22 07:10:34 +00:00
|
|
|
func localizedBolding(displayName: String,
|
|
|
|
emojis: [Emoji],
|
|
|
|
label: AnimatedAttachmentLabel,
|
|
|
|
identityContext: IdentityContext) -> NSAttributedString {
|
2020-10-30 07:11:24 +00:00
|
|
|
let mutableString = NSMutableAttributedString(
|
|
|
|
string: String.localizedStringWithFormat(
|
|
|
|
NSLocalizedString(self, comment: ""),
|
|
|
|
displayName))
|
|
|
|
|
|
|
|
let range = (mutableString.string as NSString).range(of: displayName)
|
|
|
|
|
|
|
|
if range.location != NSNotFound,
|
|
|
|
let boldFontDescriptor = label.font.fontDescriptor.withSymbolicTraits([.traitBold]) {
|
|
|
|
let boldFont = UIFont(descriptor: boldFontDescriptor, size: label.font.pointSize)
|
|
|
|
|
|
|
|
mutableString.setAttributes([NSAttributedString.Key.font: boldFont], range: range)
|
|
|
|
}
|
|
|
|
|
2021-02-22 07:10:34 +00:00
|
|
|
mutableString.insert(emojis: emojis, view: label, identityContext: identityContext)
|
2020-10-30 07:11:24 +00:00
|
|
|
mutableString.resizeAttachments(toLineHeight: label.font.lineHeight)
|
|
|
|
|
|
|
|
return mutableString
|
|
|
|
}
|
2021-02-02 04:36:16 +00:00
|
|
|
|
|
|
|
func appendingWithSeparator(_ string: Self) -> Self {
|
|
|
|
appending(Self.separator).appending(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
mutating func appendWithSeparator(_ string: Self) {
|
|
|
|
append(Self.separator.appending(string))
|
|
|
|
}
|
|
|
|
}
|