mastodon-ios/MastodonSDK/Sources/MastodonUI/Extension/MetaLabel.swift

140 lines
5.4 KiB
Swift
Raw Normal View History

2021-07-22 13:34:24 +02:00
//
// MetaText.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-7-22.
//
import UIKit
2021-07-23 13:10:27 +02:00
import Meta
2021-07-22 13:34:24 +02:00
import MetaTextKit
import MastodonAsset
2021-07-22 13:34:24 +02:00
extension MetaLabel {
public enum Style {
2021-07-22 13:34:24 +02:00
case statusHeader
case statusName
case statusUsername
2022-02-08 12:50:18 +01:00
case statusSpoilerOverlay
case statusSpoilerBanner
case notificationTitle
2021-07-23 13:10:27 +02:00
case profileFieldName
case profileFieldValue
case profileCardName
case profileCardUsername
2021-07-23 13:10:27 +02:00
case recommendAccountName
case titleView
case settingTableFooter
case autoCompletion
2021-09-14 12:21:15 +02:00
case accountListName
case accountListUsername
2021-09-24 13:58:50 +02:00
case sidebarHeadline(isSelected: Bool)
case sidebarSubheadline(isSelected: Bool)
2021-07-22 13:34:24 +02:00
}
public convenience init(style: Style) {
2021-07-22 13:34:24 +02:00
self.init()
layer.masksToBounds = true
lineBreakMode = .byTruncatingTail
2021-07-22 13:34:24 +02:00
textContainer.lineBreakMode = .byTruncatingTail
textContainer.lineFragmentPadding = 0
2021-09-24 13:58:50 +02:00
setup(style: style)
}
public func setup(style: Style) {
2021-07-22 13:34:24 +02:00
let font: UIFont
let textColor: UIColor
2021-09-24 13:58:50 +02:00
2021-07-22 13:34:24 +02:00
switch style {
case .statusHeader:
font = UIFontMetrics(forTextStyle: .footnote).scaledFont(for: .systemFont(ofSize: 13, weight: .bold))
2021-07-22 13:34:24 +02:00
textColor = Asset.Colors.Label.secondary.color
2021-09-24 13:58:50 +02:00
2021-07-22 13:34:24 +02:00
case .statusName:
font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 15, weight: .semibold))
2021-07-22 13:34:24 +02:00
textColor = Asset.Colors.Label.primary.color
2021-09-24 13:58:50 +02:00
case .statusUsername:
font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 15, weight: .regular))
textColor = Asset.Colors.Label.secondary.color
2022-02-08 12:50:18 +01:00
case .statusSpoilerOverlay:
font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 20, weight: .semibold))
textColor = Asset.Colors.Label.primary.color
textAlignment = .center
paragraphStyle.alignment = .center
2022-02-08 12:50:18 +01:00
case .statusSpoilerBanner:
font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .regular))
textColor = Asset.Colors.Label.primary.color
case .notificationTitle:
font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 14, weight: .regular))
textColor = Asset.Colors.Label.secondary.color
2021-09-24 13:58:50 +02:00
2021-07-23 13:10:27 +02:00
case .profileFieldName:
font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 13, weight: .semibold))
textColor = Asset.Colors.Label.secondary.color
2021-09-24 13:58:50 +02:00
2021-07-23 13:10:27 +02:00
case .profileFieldValue:
font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .regular))
2021-07-23 13:10:27 +02:00
textColor = Asset.Colors.Label.primary.color
2021-09-24 13:58:50 +02:00
case .profileCardName:
font = .systemFont(ofSize: 17, weight: .semibold)
textColor = Asset.Colors.Label.primary.color
case .profileCardUsername:
font = .systemFont(ofSize: 15, weight: .regular)
textColor = Asset.Colors.Label.secondary.color
2021-07-23 13:10:27 +02:00
case .titleView:
font = .systemFont(ofSize: 17, weight: .semibold)
textColor = Asset.Colors.Label.primary.color
textAlignment = .center
paragraphStyle.alignment = .center
2021-09-24 13:58:50 +02:00
2021-07-23 13:10:27 +02:00
case .recommendAccountName:
font = .systemFont(ofSize: 18, weight: .semibold)
textColor = .white
2021-09-24 13:58:50 +02:00
2021-07-23 13:10:27 +02:00
case .settingTableFooter:
font = .preferredFont(forTextStyle: .footnote)
textColor = Asset.Colors.Label.secondary.color
2021-07-23 13:10:27 +02:00
numberOfLines = 0
textContainer.maximumNumberOfLines = 0
paragraphStyle.alignment = .center
case .autoCompletion:
font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .semibold), maximumPointSize: 22)
textColor = Asset.Colors.brandBlue.color
2021-09-14 12:21:15 +02:00
case .accountListName:
font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .regular), maximumPointSize: 22)
textColor = Asset.Colors.Label.primary.color
case .accountListUsername:
font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 15, weight: .regular), maximumPointSize: 20)
textColor = Asset.Colors.Label.secondary.color
2021-09-24 13:58:50 +02:00
case .sidebarHeadline(let isSelected):
font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 22, weight: .regular), maximumPointSize: 20)
textColor = isSelected ? .white : Asset.Colors.Label.primary.color
case .sidebarSubheadline(let isSelected):
font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 13, weight: .regular), maximumPointSize: 18)
textColor = isSelected ? .white : Asset.Colors.Label.secondary.color
2021-07-22 13:34:24 +02:00
}
2021-09-24 13:58:50 +02:00
2021-07-22 13:34:24 +02:00
self.font = font
self.textColor = textColor
textAttributes = [
.font: font,
.foregroundColor: textColor
]
2021-07-23 13:10:27 +02:00
linkAttributes = [
.font: font,
.foregroundColor: Asset.Colors.brandBlue.color
]
}
}