Cross-platform text highlighting and syntax highlighting for Compose and View, using regular expressions.
Facilitate the creation of dynamic highlights using regular expressions, ideal for code editors or text editors with custom formatting.
Platform | Support |
---|---|
Android with Jetpack Compose | ✅ |
Android with View | ✅ |
Compose for Desktop | ✅ |
Compose for Web | ✅ |
Compose for iOS | ❌ |
The main class is Highlight
, which generates formatted text in types like SpannedString
or AnnotatedString
, depending on the platform.
You can pass a list of schemes directly through the constructor.
val highlight = Highlight(
TextColorScheme(
regex = "\\bcolor\\b".toRegex(),
match = Match.fully(UiColor.Black)
)
)
// Jetpack Compose
val annotatedString = highlight.toAnnotatedString("Foreground color example.")
// View-based
val spannedString = highlight.toSpannedString("Foreground color example.")
Or use the builder scope to create patterns:
val highlight = Highlight {
textColor {
fully(
regex = "\\bcolor\\b".toRegex(),
value = UiColor.Black
)
}
}
// Jetpack Compose
val annotatedString = highlight.toAnnotatedString("Foreground color example.")
// View-based
val spannedString = highlight.toSpannedString("Foreground color example.")
In Compose environments, use rememberAnnotatedString
to integrate highlighting into a Text
component.
val highlight = rememberHighlight {
spanStyle {
fully(
regex = "\\bstyled\\b".toRegex(),
value = SpanStyle(
color = Color.White,
background = Color.Black,
fontStyle = FontStyle.Italic,
)
)
}
}
Text(
text = highlight.rememberAnnotatedString(
"Example of styled text."
)
)
Or use rememberTextFieldValue
for TextFieldValue
:
val textFieldValue = rememberSaveable { mutableStateOf(TextFieldValue()) }
BasicTextField(
value = highlight.rememberTextFieldValue(
textFieldValue.value
).copy(
composition = null
),
onValueChange = {
textFieldValue.value = it
}
)
In View-based environments, use toSpannedString
, or apply
to apply highlights.
val highlight = Highlight {
backgroundColor {
fully(
regex = "\\bcolor\\b".toRegex(),
value = UiColor.Blue
)
}
textColor {
fully(
regex = "\\bcolor\\b".toRegex(),
value = UiColor.White
)
}
}
// TextView
binding.tvExample.text = highlight.toSpannedString(
"Background color example."
)
// EditText (Editable or Spannable)
highlight.apply(binding.etExample)
Instead of applying the highlight to the entire match using Match.fully(..)
, you can separate it by groups, allowing for more complex and specific highlights.
val highlight = rememberHighlight {
textColor {
groups(
regex = "(\\w+)\\s*=\\s*(\\w+)".toRegex(),
UiColor.Blue,
UiColor.Green
)
}
}
Text(
text = highlight.rememberAnnotatedString("name = Highlight")
)
Simple example | Code highlight |
---|---|
To integrate the Highlight library into your project, you can add it directly from the Maven Central repository.
Add the following dependencies to your build.gradle.kts
file:
dependencies {
// For highlighting in Views
implementation("com.neoutils.highlight:highlight-view:2.2.0")
// For highlighting in Compose
implementation("com.neoutils.highlight:highlight-compose:2.2.0")
}