126 lines
5.0 KiB
Swift
126 lines
5.0 KiB
Swift
import UIKit
|
||
|
||
// MARK: - 阅读器主题
|
||
|
||
/// EPUB 阅读器主题配置结构体
|
||
/// 定义阅读器内容区域和工具栏的颜色方案
|
||
/// 内置浅色、深色、黄色、绿色、粉色、蓝色六套预设主题
|
||
public struct RDEPUBReaderTheme: Equatable {
|
||
// MARK: 内容区颜色
|
||
|
||
/// 内容区域的背景色
|
||
public var contentBackgroundColor: UIColor
|
||
/// 内容区域的正文文字颜色
|
||
public var contentTextColor: UIColor
|
||
|
||
// MARK: 工具栏颜色
|
||
|
||
/// 工具栏背景色
|
||
public var toolBackgroundColor: UIColor
|
||
/// 工具栏控件文字/图标颜色
|
||
public var toolControlTextColor: UIColor
|
||
/// 工具栏控件未选中状态的边框颜色
|
||
public var toolControlBorderUnselectColor: UIColor
|
||
/// 工具栏中分隔线的颜色
|
||
public var toolLineColor: UIColor
|
||
|
||
/// 初始化主题配置
|
||
/// - Parameters:
|
||
/// - contentBackgroundColor: 内容区背景色
|
||
/// - contentTextColor: 内容区文字颜色
|
||
/// - toolBackgroundColor: 工具栏背景色
|
||
/// - toolControlTextColor: 工具栏控件颜色
|
||
/// - toolControlBorderUnselectColor: 控件未选中边框色
|
||
/// - toolLineColor: 分隔线颜色
|
||
public init(
|
||
contentBackgroundColor: UIColor,
|
||
contentTextColor: UIColor,
|
||
toolBackgroundColor: UIColor,
|
||
toolControlTextColor: UIColor,
|
||
toolControlBorderUnselectColor: UIColor,
|
||
toolLineColor: UIColor
|
||
) {
|
||
self.contentBackgroundColor = contentBackgroundColor
|
||
self.contentTextColor = contentTextColor
|
||
self.toolBackgroundColor = toolBackgroundColor
|
||
self.toolControlTextColor = toolControlTextColor
|
||
self.toolControlBorderUnselectColor = toolControlBorderUnselectColor
|
||
self.toolLineColor = toolLineColor
|
||
}
|
||
|
||
// MARK: 内置预设主题
|
||
|
||
/// 浅色主题:白底黑字,适合日间阅读
|
||
public static let light = RDEPUBReaderTheme(
|
||
contentBackgroundColor: .white,
|
||
contentTextColor: .black,
|
||
toolBackgroundColor: .white,
|
||
toolControlTextColor: .black,
|
||
toolControlBorderUnselectColor: UIColor.lightGray.withAlphaComponent(0.5),
|
||
toolLineColor: UIColor.lightGray.withAlphaComponent(0.5)
|
||
)
|
||
|
||
/// 深色主题:黑底白字,适合夜间阅读
|
||
public static let dark = RDEPUBReaderTheme(
|
||
contentBackgroundColor: .black,
|
||
contentTextColor: .white,
|
||
toolBackgroundColor: .black,
|
||
toolControlTextColor: .white,
|
||
toolControlBorderUnselectColor: UIColor.lightGray.withAlphaComponent(0.5),
|
||
toolLineColor: UIColor.lightGray.withAlphaComponent(0.5)
|
||
)
|
||
|
||
/// 黄色护眼主题:暖色调背景,减少蓝光刺激
|
||
public static let yellow = RDEPUBReaderTheme(
|
||
contentBackgroundColor: UIColor(red: 0.89, green: 0.87, blue: 0.79, alpha: 1),
|
||
contentTextColor: .black,
|
||
toolBackgroundColor: UIColor(red: 0.89, green: 0.87, blue: 0.79, alpha: 1),
|
||
toolControlTextColor: .black,
|
||
toolControlBorderUnselectColor: UIColor.lightGray.withAlphaComponent(0.5),
|
||
toolLineColor: UIColor.lightGray.withAlphaComponent(0.5)
|
||
)
|
||
|
||
/// 绿色护眼主题:模拟纸质书的柔和绿色背景
|
||
public static let green = RDEPUBReaderTheme(
|
||
contentBackgroundColor: UIColor(red: 0.87, green: 0.91, blue: 0.82, alpha: 1),
|
||
contentTextColor: .black,
|
||
toolBackgroundColor: UIColor(red: 0.87, green: 0.91, blue: 0.82, alpha: 1),
|
||
toolControlTextColor: .black,
|
||
toolControlBorderUnselectColor: UIColor.lightGray.withAlphaComponent(0.5),
|
||
toolLineColor: UIColor.lightGray.withAlphaComponent(0.5)
|
||
)
|
||
|
||
/// 粉色主题:柔和粉色背景
|
||
public static let pink = RDEPUBReaderTheme(
|
||
contentBackgroundColor: UIColor(red: 1, green: 0.89, blue: 0.91, alpha: 1),
|
||
contentTextColor: .black,
|
||
toolBackgroundColor: UIColor(red: 1, green: 0.89, blue: 0.91, alpha: 1),
|
||
toolControlTextColor: .black,
|
||
toolControlBorderUnselectColor: UIColor.lightGray.withAlphaComponent(0.5),
|
||
toolLineColor: UIColor.lightGray.withAlphaComponent(0.5)
|
||
)
|
||
|
||
/// 蓝色主题:冷色调蓝色背景
|
||
public static let blue = RDEPUBReaderTheme(
|
||
contentBackgroundColor: UIColor(red: 0.8, green: 0.84, blue: 0.89, alpha: 1),
|
||
contentTextColor: .black,
|
||
toolBackgroundColor: UIColor(red: 0.8, green: 0.84, blue: 0.89, alpha: 1),
|
||
toolControlTextColor: .black,
|
||
toolControlBorderUnselectColor: UIColor.lightGray.withAlphaComponent(0.5),
|
||
toolLineColor: UIColor.lightGray.withAlphaComponent(0.5)
|
||
)
|
||
}
|
||
|
||
// MARK: - CSS 颜色转换
|
||
|
||
extension RDEPUBReaderTheme {
|
||
/// 将内容区背景色转换为 CSS 颜色字符串,注入 EPUB 的 HTML 中
|
||
var themeBackgroundColorCSS: String {
|
||
contentBackgroundColor.ss_cssString
|
||
}
|
||
|
||
/// 将内容区文字颜色转换为 CSS 颜色字符串,注入 EPUB 的 HTML 中
|
||
var themeTextColorCSS: String {
|
||
contentTextColor.ss_cssString
|
||
}
|
||
} |