Skip to content

Commit 473b8c3

Browse files
committed
Added line spacing customization
1 parent cebb0e1 commit 473b8c3

2 files changed

Lines changed: 79 additions & 10 deletions

File tree

codeview/src/main/java/io/github/kbiakov/codeview/adapters/AbstractCodeAdapter.kt

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,15 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
177177
tvLineContent.typeface = options.font
178178

179179
val isLine = viewType == ViewHolderType.Line.viewType
180-
val viewHeight = if (isLine) R.dimen.line_height else R.dimen.line_border_height
181-
lineView.layoutParams.height = context.resources.getDimension(viewHeight).toInt()
182-
183-
if (isLine) {
180+
options.format.apply {
181+
val height = if (isLine) lineHeight else borderHeight
182+
lineView.layoutParams.height = dpToPx(context, height)
183+
}
184+
return if (isLine) {
184185
val holder = LineViewHolder(lineView)
185186
holder.setIsRecyclable(false)
186-
return holder
187-
} else {
188-
return BorderViewHolder(lineView)
189-
}
187+
holder
188+
} else BorderViewHolder(lineView)
190189
}
191190

192191
override fun onBindViewHolder(holder: ViewHolder, pos: Int) {
@@ -213,14 +212,17 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
213212

214213
@SuppressLint("SetTextI18n")
215214
private fun setupLine(pos: Int, line: String, holder: ViewHolder) {
215+
val fontSize = options.format.fontSize
216+
217+
holder.tvLineContent.textSize = fontSize
216218
holder.tvLineContent.text = html(line)
217219
holder.tvLineContent.setTextColor(options.theme.noteColor.color())
218220

219221
if (options.shortcut && pos == MaxShortcutLines) {
220-
holder.tvLineNum.textSize = 10f
222+
holder.tvLineNum.textSize = fontSize * Format.ShortcutScale
221223
holder.tvLineNum.text = context.getString(R.string.dots)
222224
} else {
223-
holder.tvLineNum.textSize = 12f
225+
holder.tvLineNum.textSize = fontSize
224226
holder.tvLineNum.text = "${pos + 1}"
225227
}
226228
}
@@ -313,6 +315,7 @@ data class Options(
313315
var language: String? = null,
314316
var theme: ColorThemeData = ColorTheme.DEFAULT.theme(),
315317
var font: Typeface = FontCache.get(context).getTypeface(context),
318+
var format: Format = Format.Compact,
316319
var shadows: Boolean = false,
317320
var shortcut: Boolean = false,
318321
var shortcutNote: String = context.getString(R.string.show_all),
@@ -401,3 +404,21 @@ data class Options(
401404
fun get(context: Context) = Options(context)
402405
}
403406
}
407+
408+
data class Format(val scaleFactor: Float = 1f,
409+
val lineHeight: Int = (LineHeight * scaleFactor).toInt(),
410+
val borderHeight: Int = (BorderHeight * scaleFactor).toInt(),
411+
val fontSize: Float = FontSize.toFloat()) {
412+
413+
companion object Default {
414+
private const val LineHeight = 18
415+
private const val BorderHeight = 3
416+
private const val FontSize = 12
417+
418+
internal const val ShortcutScale = 0.83f
419+
420+
val ExtraCompact = Format(0.88f)
421+
val Compact = Format()
422+
val Medium = Format(1.33f)
423+
}
424+
}

example/src/main/java/io/github/kbiakov/codeviewexample/ListingsActivity.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.github.kbiakov.codeview.CodeView;
1111
import io.github.kbiakov.codeview.OnCodeLineClickListener;
1212
import io.github.kbiakov.codeview.adapters.CodeWithDiffsAdapter;
13+
import io.github.kbiakov.codeview.adapters.Format;
1314
import io.github.kbiakov.codeview.adapters.Options;
1415
import io.github.kbiakov.codeview.highlight.ColorTheme;
1516
import io.github.kbiakov.codeview.highlight.ColorThemeData;
@@ -59,6 +60,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5960
"js", // language
6061
ColorTheme.MONOKAI.theme(), // theme (data)
6162
FontCache.get(this).getTypeface(this), // font
63+
Format.Default.getCompact(), // format
6264
true, // shadows
6365
true, // shortcut
6466
getString(R.string.show_all), // shortcut note
@@ -117,5 +119,51 @@ public void onCodeLineClicked(int n, @NotNull String line) {
117119

118120
codeView.getOptions()
119121
.shortcut(10, "Show all");
122+
123+
// - Playground
124+
125+
codeView.getOptions()
126+
.withFont(Font.Consolas)
127+
.setShortcut(false);
128+
codeView.setCode("" +
129+
"package io.github.kbiakov.codeviewexample;\n" +
130+
"\n" +
131+
"import android.os.Bundle;\n" +
132+
"import android.support.annotation.Nullable;\n" +
133+
"import android.support.v7.app.AppCompatActivity;\n" +
134+
"import android.util.Log;\n" +
135+
"\n" +
136+
"import org.jetbrains.annotations.NotNull;\n" +
137+
"\n" +
138+
"import io.github.kbiakov.codeview.CodeView;\n" +
139+
"import io.github.kbiakov.codeview.OnCodeLineClickListener;\n" +
140+
"import io.github.kbiakov.codeview.adapters.CodeWithDiffsAdapter;\n" +
141+
"import io.github.kbiakov.codeview.adapters.Options;\n" +
142+
"import io.github.kbiakov.codeview.highlight.ColorTheme;\n" +
143+
"import io.github.kbiakov.codeview.highlight.ColorThemeData;\n" +
144+
"import io.github.kbiakov.codeview.highlight.Font;\n" +
145+
"import io.github.kbiakov.codeview.highlight.FontCache;\n" +
146+
"import io.github.kbiakov.codeview.views.DiffModel;\n" +
147+
"\n" +
148+
"public class ListingsActivity extends AppCompatActivity {\n" +
149+
"\n" +
150+
" @Override\n" +
151+
" protected void onCreate(@Nullable Bundle savedInstanceState) {\n" +
152+
" super.onCreate(savedInstanceState);\n" +
153+
" setContentView(R.layout.activity_listings);\n" +
154+
"\n" +
155+
" final CodeView codeView = (CodeView) findViewById(R.id.code_view);\n" +
156+
"\n" +
157+
" /*\n" +
158+
" * 1: set code content\n" +
159+
" */\n" +
160+
"\n" +
161+
" // auto language recognition\n" +
162+
" codeView.setCode(getString(R.string.listing_js));\n" +
163+
"\n" +
164+
" // specify language for code listing\n" +
165+
" codeView.setCode(getString(R.string.listing_py), \"py\");" +
166+
" }\n" +
167+
"}", "java");
120168
}
121169
}

0 commit comments

Comments
 (0)