标题粗体独立控制 #186

This commit is contained in:
HapeLee
2025-11-01 00:39:11 +08:00
parent 0513fc143f
commit 030033e79b
4 changed files with 104 additions and 29 deletions
@@ -219,6 +219,12 @@ object ReadBookConfig {
config.textBold = value
}
var titleBold: Int
get() = config.titleBold
set(value) {
config.titleBold = value
}
var textItalic: Boolean
get() = config.textItalic
set(value) {
@@ -490,6 +496,7 @@ object ReadBookConfig {
var titleSize: Int = 0,
var titleTopSpacing: Int = 0,
var titleBottomSpacing: Int = 0,
var titleBold: Int = 500,
var paragraphIndent: String = "  ",//段落缩进
var underline: Boolean = false, //下划线
var paddingBottom: Int = 6,
@@ -10,6 +10,7 @@ import io.legado.app.constant.EventBus
import io.legado.app.databinding.DialogTipConfigBinding
import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.config.ReadTipConfig
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.selector
import io.legado.app.utils.hexString
import io.legado.app.utils.observeEvent
@@ -47,6 +48,35 @@ class TipConfigDialog : BaseBottomSheetDialogFragment(R.layout.dialog_tip_config
2 -> binding.rgTitleMode.check(R.id.rb_title_mode3)
else -> { }
}
val weightOptions = context?.resources?.getStringArray(R.array.text_font_weight)
val weightValues = listOf(2, 1, 0)
val initialIndex = weightValues.indexOf(ReadBookConfig.titleBold)
binding.textFontWeightConverter.text = weightOptions?.getOrNull(initialIndex) ?: "自定义"
binding.textFontWeightConverter.setOnClickListener {
context?.alert(titleResource = R.string.text_font_weight_converter) {
weightOptions?.let { options ->
items(options.toList()) { _, i ->
ReadBookConfig.titleBold = weightValues[i]
binding.sliderFontWeight.value =
ReadBookConfig.titleBold.toFloat().coerceAtLeast(100f)
binding.textFontWeightConverter.text = options.getOrNull(i) ?: ""
postEvent(EventBus.UP_CONFIG, arrayListOf(8, 9, 6))
}
}
}
}
binding.sliderFontWeight.apply {
valueFrom = 100f
valueTo = 900f
stepSize = 1f
value = ReadBookConfig.titleBold.toFloat().coerceAtLeast(100f)
addOnChangeListener { _, newValue, _ ->
binding.textFontWeightConverter.text = "自定义"
ReadBookConfig.titleBold = newValue.toInt()
postEvent(EventBus.UP_CONFIG, arrayListOf(8, 9, 6))
}
}
binding.dsbTitleSize.progress = ReadBookConfig.titleSize
binding.dsbTitleTop.progress = ReadBookConfig.titleTopSpacing
@@ -915,40 +915,52 @@ object ChapterProvider {
private fun getPaints(typeface: Typeface?): Pair<TextPaint, TextPaint> {
val bold = Typeface.create(typeface, Typeface.BOLD)
val normal = Typeface.create(typeface, Typeface.NORMAL)
val (titleFont, textFont) = when (ReadBookConfig.textBold) {
1 -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
Pair(Typeface.create(typeface, 900, false), bold)
else
Pair(bold, bold)
}
val titleFont = when (ReadBookConfig.titleBold) {
1 -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
Typeface.create(typeface, 900, false)
else
bold
2 -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
Pair(normal, Typeface.create(typeface, 300, false))
else
Pair(normal, normal)
}
2 -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
Typeface.create(typeface, 300, false)
else
normal
in 100..900 -> {
in 100..900 -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
Typeface.create(typeface, ReadBookConfig.titleBold, false)
else
normal
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Pair(normal, Typeface.create(typeface, ReadBookConfig.textBold, false))
} else {
Pair(normal, normal)
}
}
else -> Pair(bold, normal)
else -> bold
}
val textFont = when (ReadBookConfig.textBold) {
1 -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
Typeface.create(typeface, 900, false)
else
bold
2 -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
Typeface.create(typeface, 300, false)
else
normal
in 100..900 -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
Typeface.create(typeface, ReadBookConfig.textBold, false)
else
normal
else -> bold
}
//标题
val tPaint = TextPaint()
tPaint.color = ReadBookConfig.textColor
tPaint.letterSpacing = ReadBookConfig.letterSpacing
tPaint.typeface = titleFont
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && ReadBookConfig.textBold in 100..900)
tPaint.setFontVariationSettings("'wght' ${ReadBookConfig.textBold}")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && ReadBookConfig.titleBold in 100..900)
tPaint.setFontVariationSettings("'wght' ${ReadBookConfig.titleBold}")
tPaint.textSize = with(ReadBookConfig) { textSize + titleSize }.toFloat().spToPx()
tPaint.isAntiAlias = true
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q && AppConfig.optimizeRender) {
+31 -5
View File
@@ -75,11 +75,36 @@
</com.google.android.material.chip.ChipGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_font_weight_converter"
android:textSize="16sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.button.MaterialButton
android:id="@+id/textFontWeightConverter"
style="@style/Widget.Material3Expressive.Button.IconButton.Tonal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
android:textSize="12sp" />
<com.google.android.material.slider.Slider
android:id="@+id/sliderFontWeight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<io.legado.app.ui.widget.DetailSeekBar
android:id="@+id/dsb_title_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:max="10"
app:max="20"
app:title="@string/title_font_size" />
<LinearLayout
@@ -89,9 +114,9 @@
<io.legado.app.ui.widget.SimpleCounterView
android:id="@+id/dsb_title_top"
android:layout_width="140dp"
android:layout_width="120dp"
android:layout_height="wrap_content"
app:max="100"
app:max="200"
app:title="@string/title_margin_top" />
<View
@@ -101,10 +126,11 @@
<io.legado.app.ui.widget.SimpleCounterView
android:id="@+id/dsb_title_bottom"
android:layout_width="140dp"
android:layout_width="120dp"
android:layout_height="wrap_content"
app:max="100"
app:max="200"
app:title="@string/title_margin_bottom" />
</LinearLayout>