mastodon-app-ufficiale-ipho.../Mastodon/Extension/ActiveLabel.swift

72 lines
1.8 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`
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
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 .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) {
activeEntities.removeAll()
2021-04-01 08:39:15 +02:00
if let parseResult = try? MastodonStatusContent.parse(status: content) {
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) {
configure(content: note)
}
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
}
}