ReaderHive/ReaderHive/Libs/NovelTool/NRNovelReadSetManager.swift
2025-12-08 16:46:19 +08:00

199 lines
6.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// NRNovelReadSetManager.swift
// ReaderHive
//
// Created by 鸿 on 2025/11/27.
//
import UIKit
class NRNovelReadSetManager: NSObject {
static let manager = NRNovelReadSetManager()
private(set) var readSet: NRNovelReadSet = UserDefaults.nr_object(forKey: kNRNovelReadSetDefaultsKey, as: NRNovelReadSet.self) ?? NRNovelReadSet()
///
let animateDuration = 0.2
let topViewHeight: CGFloat = UIScreen.navBarHeight
let bottomViewHeight: CGFloat = UIScreen.safeBottom + 105 + 52
let contentTopViewHeight: CGFloat = UIScreen.navBarHeight
let contentBottomViewHeight: CGFloat = UIScreen.safeBottom + 30
///
var readRect: CGRect {
return .init(x: 16, y: 0, width: UIScreen.width - 32, height: UIScreen.height - contentTopViewHeight - contentBottomViewHeight)
}
var currentTheme: NRReadTheme {
if isNight {
return nightTheme
} else {
return NRReadTheme.theme(for: readSet.theme)
}
}
///
lazy var nightTheme: NRReadTheme = {
return NRReadTheme(type: .theme1, backgroundColor: ._24232_B, textColor: ._9_D_9_CA_8, statusBarStyle: .default)
}()
///
var lineSpacing: CGFloat {
switch self.readSet.lineSpacingType {
case .small:
return 8
case .standard:
return 10
case .large:
return 12
}
}
///
var paragraphSpacing: CGFloat {
switch self.readSet.paragraphSpacingType {
case .small:
return 15
case .standard:
return 17
case .large:
return 19
}
}
var textColor: UIColor {
return currentTheme.textColor
}
var textFont: UIFont {
return UIFont.font(ofSize: self.readSet.fontSize, weight: .regular)
}
var titleFont: UIFont {
return UIFont.font(ofSize: self.readSet.fontSize + 10, weight: .semibold)
}
var miniFontSize: CGFloat = 12
var maxFontSize: CGFloat = 24
var isNight: Bool {
return readSet.isNight
}
func updateTheme(type: NRReadThemeType) {
readSet.theme = type
UserDefaults.nr_setObject(readSet, forKey: kNRNovelReadSetDefaultsKey)
NotificationCenter.default.post(name: NRNovelReadSetManager.didChangedThemeNotification, object: nil)
}
func updateIsNight(isNight: Bool) {
readSet.isNight = isNight
UserDefaults.nr_setObject(readSet, forKey: kNRNovelReadSetDefaultsKey)
NotificationCenter.default.post(name: NRNovelReadSetManager.didChangedThemeNotification, object: nil)
}
func updateLineSpacing(type: NRNovelReadSet.SpacingType) {
readSet.lineSpacingType = type
UserDefaults.nr_setObject(readSet, forKey: kNRNovelReadSetDefaultsKey)
NotificationCenter.default.post(name: NRNovelReadSetManager.didChangedTextLayoutNotification, object: nil)
}
func updateParagraphSpacing(type: NRNovelReadSet.SpacingType) {
readSet.paragraphSpacingType = type
UserDefaults.nr_setObject(readSet, forKey: kNRNovelReadSetDefaultsKey)
NotificationCenter.default.post(name: NRNovelReadSetManager.didChangedTextLayoutNotification, object: nil)
}
func updateFontSize(size: CGFloat) {
readSet.fontSize = size
UserDefaults.nr_setObject(readSet, forKey: kNRNovelReadSetDefaultsKey)
NotificationCenter.default.post(name: NRNovelReadSetManager.didChangedTextLayoutNotification, object: nil)
}
///
/// isPaging: YES (:UIColor,...,)
func attributes(isTitle:Bool, isPageing:Bool = false) ->[NSAttributedString.Key:Any] {
//
let paragraphStyle = NSMutableParagraphStyle()
// (lineSpacing)()
paragraphStyle.lineHeightMultiple = 1.0
if isTitle {
//
paragraphStyle.lineSpacing = 0
//
paragraphStyle.paragraphSpacing = 0
//
paragraphStyle.alignment = .center
}else{
//
paragraphStyle.lineSpacing = lineSpacing
//
paragraphStyle.lineBreakMode = .byCharWrapping
// paragraphStyle.lineBreakMode = .byWordWrapping
//
paragraphStyle.paragraphSpacing = paragraphSpacing
//
paragraphStyle.alignment = .justified
}
let font: UIFont
if isTitle {
font = self.titleFont
} else {
font = self.textFont
}
if isPageing {
return [.font: font, .paragraphStyle: paragraphStyle]
}else{
return [.foregroundColor: textColor, .font: font, .paragraphStyle: paragraphStyle]
}
}
func emptyAttributes() -> [NSAttributedString.Key:Any] {
//
let paragraphStyle = NSMutableParagraphStyle()
// (lineSpacing)()
paragraphStyle.lineHeightMultiple = 1.0
//
paragraphStyle.lineSpacing = 0
//
paragraphStyle.paragraphSpacing = 0
//
paragraphStyle.alignment = .center
return [.font: self.textFont, .paragraphStyle: paragraphStyle]
}
}
extension NRNovelReadSetManager {
///
static let didChangedThemeNotification = Notification.Name(rawValue: "NRNovelReadSetManager.didChangedThemeNotification")
///
static let didChangedTextLayoutNotification = Notification.Name(rawValue: "NRNovelReadSetManager.didChangedTextLayoutNotification")
}