metatext-app-ios-iphone-ipad/Extensions/String+Extensions.swift

47 lines
1.7 KiB
Swift

// Copyright © 2020 Metabolist. All rights reserved.
import Mastodon
import UIKit
extension String {
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
}
func localizedBolding(displayName: String, emojis: [Emoji], label: UILabel) -> NSAttributedString {
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)
}
mutableString.insert(emojis: emojis, view: label)
mutableString.resizeAttachments(toLineHeight: label.font.lineHeight)
return mutableString
}
}