Add headingTypeface to SpannableTheme, use a custom heading typeface in the sample app

This commit is contained in:
Eric Denman 2018-07-17 08:52:21 -07:00
parent 5ef985670a
commit a071646fee
4 changed files with 28 additions and 3 deletions

View File

@ -173,6 +173,9 @@ public class SpannableTheme {
// by default, text color with `HEADING_DEF_BREAK_COLOR_ALPHA` applied alpha
protected final int headingBreakColor;
// by default, whatever typeface is set on the TextView
protected final Typeface headingTypeface;
// by default `SCRIPT_DEF_TEXT_SIZE_RATIO`
protected final float scriptTextSizeRatio;
@ -214,6 +217,7 @@ public class SpannableTheme {
this.codeTextSize = builder.codeTextSize;
this.headingBreakHeight = builder.headingBreakHeight;
this.headingBreakColor = builder.headingBreakColor;
this.headingTypeface = builder.headingTypeface;
this.scriptTextSizeRatio = builder.scriptTextSizeRatio;
this.thematicBreakColor = builder.thematicBreakColor;
this.thematicBreakHeight = builder.thematicBreakHeight;
@ -368,7 +372,11 @@ public class SpannableTheme {
}
public void applyHeadingTextStyle(@NonNull Paint paint, @IntRange(from = 1, to = 6) int level) {
paint.setFakeBoldText(true);
if (headingTypeface == null) {
paint.setFakeBoldText(true);
} else {
paint.setTypeface(headingTypeface);
}
paint.setTextSize(paint.getTextSize() * HEADING_SIZES[level - 1]);
}
@ -491,6 +499,7 @@ public class SpannableTheme {
private int codeTextSize;
private int headingBreakHeight = -1;
private int headingBreakColor;
private Typeface headingTypeface;
private float scriptTextSizeRatio;
private int thematicBreakColor;
private int thematicBreakHeight = -1;
@ -520,6 +529,7 @@ public class SpannableTheme {
this.codeTextSize = theme.codeTextSize;
this.headingBreakHeight = theme.headingBreakHeight;
this.headingBreakColor = theme.headingBreakColor;
this.headingTypeface = theme.headingTypeface;
this.scriptTextSizeRatio = theme.scriptTextSizeRatio;
this.thematicBreakColor = theme.thematicBreakColor;
this.thematicBreakHeight = theme.thematicBreakHeight;
@ -634,6 +644,12 @@ public class SpannableTheme {
return this;
}
@NonNull
public Builder headingTypeface(Typeface headingTypeface) {
this.headingTypeface = headingTypeface;
return this;
}
@NonNull
public Builder scriptTextSizeRatio(@FloatRange(from = .0F, to = Float.MAX_VALUE) float scriptTextSizeRatio) {
this.scriptTextSizeRatio = scriptTextSizeRatio;

View File

@ -1,6 +1,7 @@
package ru.noties.markwon.sample.extension;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
@ -13,6 +14,7 @@ import java.util.Arrays;
import ru.noties.markwon.SpannableBuilder;
import ru.noties.markwon.SpannableConfiguration;
import ru.noties.markwon.spans.SpannableTheme;
import ru.noties.markwon.tasklist.TaskListExtension;
public class MainActivity extends Activity {
@ -51,8 +53,13 @@ public class MainActivity extends Activity {
final IconSpanProvider spanProvider = IconSpanProvider.create(this, 0);
// create an instance of visitor to process parsed markdown
SpannableConfiguration configuration = SpannableConfiguration.builder(this)
.theme(SpannableTheme.builder()
.headingTypeface(Typeface.MONOSPACE)
.build())
.build();
final IconVisitor visitor = new IconVisitor(
SpannableConfiguration.create(this),
configuration,
builder,
spanProvider
);

View File

@ -11,6 +11,7 @@
android:layout_height="wrap_content"
android:padding="8dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15sp"
tools:text="@string/input"/>
</ScrollView>

View File

@ -6,6 +6,7 @@
# Hello! @ic-android-black-24\n\n
Home 36 black: @ic-home-black-36\n\n
Memory 48 black: @ic-memory-black-48\n\n
### I AM ANOTHER HEADER\n\n
Sentiment Satisfied 64 red: @ic-sentiment_satisfied-red-64
]]>
</string>