mastodon-ios/Mastodon/Extension/ActiveLabel.swift

84 lines
2.4 KiB
Swift
Raw Normal View History

2021-01-29 09:47:32 +01:00
//
// ActiveLabel.swift
// Mastodon
//
// Created by sxiaojian on 2021/1/29.
//
import UIKit
import Foundation
import ActiveLabel
import os.log
2021-01-29 09:47:32 +01:00
extension ActiveLabel {
enum Style {
case `default`
case statusHeader
case statusName
2021-04-01 08:39:15 +02:00
case profileField
2021-01-29 09:47:32 +01:00
}
convenience init(style: Style) {
self.init()
numberOfLines = 0
2021-02-23 08:16:55 +01:00
lineSpacing = 5
mentionColor = Asset.Colors.Label.highlight.color
hashtagColor = Asset.Colors.Label.highlight.color
URLColor = Asset.Colors.Label.highlight.color
emojiPlaceholderColor = .systemFill
2021-04-01 08:39:15 +02:00
#if DEBUG
2021-01-29 09:47:32 +01:00
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
2021-04-01 08:39:15 +02:00
#endif
switch style {
case .default:
font = .preferredFont(forTextStyle: .body)
textColor = Asset.Colors.Label.primary.color
case .statusHeader:
font = UIFontMetrics(forTextStyle: .footnote).scaledFont(for: .systemFont(ofSize: 13, weight: .medium), maximumPointSize: 17)
textColor = Asset.Colors.Label.secondary.color
numberOfLines = 1
case .statusName:
font = .systemFont(ofSize: 17, weight: .semibold)
textColor = Asset.Colors.Label.primary.color
numberOfLines = 1
2021-04-01 08:39:15 +02:00
case .profileField:
font = .preferredFont(forTextStyle: .body)
textColor = Asset.Colors.Label.primary.color
numberOfLines = 1
}
2021-01-29 09:47:32 +01:00
}
}
extension ActiveLabel {
2021-04-01 08:39:15 +02:00
/// status content
func configure(content: String, emojiDict: MastodonStatusContent.EmojiDict) {
activeEntities.removeAll()
if let parseResult = try? MastodonStatusContent.parse(content: content, emojiDict: emojiDict) {
text = parseResult.trimmed
activeEntities = parseResult.activeEntities
} else {
text = ""
}
2021-01-29 09:47:32 +01:00
}
2021-04-01 08:39:15 +02:00
/// account note
func configure(note: String, emojiDict: MastodonStatusContent.EmojiDict) {
configure(content: note, emojiDict: emojiDict)
2021-04-01 08:39:15 +02:00
}
2021-01-29 09:47:32 +01:00
}
2021-04-01 08:39:15 +02:00
extension ActiveLabel {
/// account field
func configure(field: String) {
activeEntities.removeAll()
let parseResult = MastodonField.parse(field: field)
text = parseResult.value
activeEntities = parseResult.activeEntities
2021-04-01 08:39:15 +02:00
}
}