-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathHtmlText.kt
More file actions
368 lines (357 loc) · 12.6 KB
/
Copy pathHtmlText.kt
File metadata and controls
368 lines (357 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package de.charlex.compose.material3
import android.graphics.Typeface
import android.os.Build.VERSION.SDK_INT
import android.text.Html
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StrikethroughSpan
import android.text.style.StyleSpan
import android.text.style.URLSpan
import android.text.style.UnderlineSpan
import androidx.annotation.StringRes
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.semantics.CustomAccessibilityAction
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.customActions
import androidx.compose.ui.semantics.onClick
import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.em
import androidx.core.text.getSpans
/**
* Simple Text composable to show the text with html styling from string resources.
* Supported are:
*
* <b>Bold</b>
*
* <i>Italic</i>
*
* <u>Underlined</u>
*
* <strike>Strikethrough</strike>
*
* <a href="https://google.de">Link</a>
*
* @see androidx.compose.material.Text
*
*/
@Composable
fun HtmlText(
modifier: Modifier = Modifier,
@StringRes textId: Int,
urlSpanStyle: SpanStyle = SpanStyle(
color = MaterialTheme.colorScheme.tertiary,
textDecoration = TextDecoration.Underline
),
colorMapping: Map<Color, Color> = emptyMap(),
color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified,
fontStyle: FontStyle? = null,
fontWeight: FontWeight? = null,
fontFamily: FontFamily? = null,
letterSpacing: TextUnit = TextUnit.Unspecified,
textDecoration: TextDecoration? = null,
textAlign: TextAlign? = null,
lineHeight: TextUnit = TextUnit.Unspecified,
overflow: TextOverflow = TextOverflow.Clip,
softWrap: Boolean = true,
maxLines: Int = Int.MAX_VALUE,
inlineContent: Map<String, InlineTextContent> = mapOf(),
onTextLayout: (TextLayoutResult) -> Unit = {},
style: TextStyle = LocalTextStyle.current,
onUriClick: ((String) -> Unit)? = null,
) {
val context = LocalContext.current
val annotatedString = context.resources.getText(textId).toAnnotatedString(urlSpanStyle, colorMapping)
HtmlText(
modifier = modifier,
annotatedString = annotatedString,
color = color,
fontSize = fontSize,
fontStyle = fontStyle,
fontWeight = fontWeight,
fontFamily = fontFamily,
letterSpacing = letterSpacing,
textDecoration = textDecoration,
textAlign = textAlign,
lineHeight = lineHeight,
overflow = overflow,
softWrap = softWrap,
maxLines = maxLines,
inlineContent = inlineContent,
onTextLayout = onTextLayout,
style = style,
onUriClick = onUriClick
)
}
/**
* Simple Text composable to show the text with html styling from a String.
* Supported are:
*
* <b>Bold</b>
*
* <i>Italic</i>
*
* <u>Underlined</u>
*
* <strike>Strikethrough</strike>
*
* <a href="https://google.de">Link</a>
*
* @see androidx.compose.material.Text
*
*/
@Composable
fun HtmlText(
modifier: Modifier = Modifier,
text: String,
urlSpanStyle: SpanStyle = SpanStyle(
color = MaterialTheme.colorScheme.tertiary,
textDecoration = TextDecoration.Underline
),
colorMapping: Map<Color, Color> = emptyMap(),
color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified,
fontStyle: FontStyle? = null,
fontWeight: FontWeight? = null,
fontFamily: FontFamily? = null,
letterSpacing: TextUnit = TextUnit.Unspecified,
textDecoration: TextDecoration? = null,
textAlign: TextAlign? = null,
lineHeight: TextUnit = TextUnit.Unspecified,
overflow: TextOverflow = TextOverflow.Clip,
softWrap: Boolean = true,
maxLines: Int = Int.MAX_VALUE,
inlineContent: Map<String, InlineTextContent> = mapOf(),
onTextLayout: (TextLayoutResult) -> Unit = {},
style: TextStyle = LocalTextStyle.current,
onUriClick: ((String) -> Unit)? = null,
) {
val annotatedString = if (SDK_INT < 24) {
Html.fromHtml(text)
} else {
Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY)
}.toAnnotatedString(urlSpanStyle, colorMapping)
HtmlText(
modifier = modifier,
annotatedString = annotatedString,
color = color,
fontSize = fontSize,
fontStyle = fontStyle,
fontWeight = fontWeight,
fontFamily = fontFamily,
letterSpacing = letterSpacing,
textDecoration = textDecoration,
textAlign = textAlign,
lineHeight = lineHeight,
overflow = overflow,
softWrap = softWrap,
maxLines = maxLines,
inlineContent = inlineContent,
onTextLayout = onTextLayout,
style = style,
onUriClick = onUriClick
)
}
/**
* Simple Text composable to show the text with html styling from a String.
* Supported are:
*
* <b>Bold</b>
*
* <i>Italic</i>
*
* <u>Underlined</u>
*
* <strike>Strikethrough</strike>
*
* <a href="https://google.de">Link</a>
*
* @see androidx.compose.material.Text
*
*/
@Composable
fun HtmlText(
modifier: Modifier = Modifier,
annotatedString: AnnotatedString,
color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified,
fontStyle: FontStyle? = null,
fontWeight: FontWeight? = null,
fontFamily: FontFamily? = null,
letterSpacing: TextUnit = TextUnit.Unspecified,
textDecoration: TextDecoration? = null,
textAlign: TextAlign? = null,
lineHeight: TextUnit = TextUnit.Unspecified,
overflow: TextOverflow = TextOverflow.Clip,
softWrap: Boolean = true,
maxLines: Int = Int.MAX_VALUE,
inlineContent: Map<String, InlineTextContent> = mapOf(),
onTextLayout: (TextLayoutResult) -> Unit = {},
style: TextStyle = LocalTextStyle.current,
onUriClick: ((String) -> Unit)? = null,
) {
val clickable =
annotatedString.getStringAnnotations(0, annotatedString.length - 1).any { it.tag == "url" }
val uriHandler = LocalUriHandler.current
val layoutResult = remember { mutableStateOf<TextLayoutResult?>(null) }
val urls = remember(layoutResult, annotatedString) {
annotatedString.getStringAnnotations("url", 0, annotatedString.lastIndex)
}
Text(
modifier = modifier.then(if (clickable) Modifier
.pointerInput(Unit) {
detectTapGestures(onTap = { pos ->
layoutResult.value?.let { layoutResult ->
val position = layoutResult.getOffsetForPosition(pos)
annotatedString
.getStringAnnotations(position, position)
.firstOrNull()
?.let { sa ->
if (sa.tag == "url") { // NON-NLS
val url = sa.item
onUriClick?.let { it(url) } ?: uriHandler.openUri(url)
}
}
}
})
}
.semantics {
if (urls.size == 1) {
role = Role.Button
onClick("Link (${annotatedString.substring(urls[0].start, urls[0].end)}") {
val url = urls[0].item
onUriClick?.let { it(url) } ?: uriHandler.openUri(url)
true
}
} else {
customActions = urls.map {
CustomAccessibilityAction("Link (${annotatedString.substring(it.start, it.end)})") {
val url = it.item
onUriClick?.let { it(url) } ?: uriHandler.openUri(url)
true
}
}
}
} else Modifier),
text = annotatedString,
color = color,
fontSize = fontSize,
fontStyle = fontStyle,
fontWeight = fontWeight,
fontFamily = fontFamily,
letterSpacing = letterSpacing,
textDecoration = textDecoration,
textAlign = textAlign,
lineHeight = lineHeight,
overflow = overflow,
softWrap = softWrap,
maxLines = maxLines,
inlineContent = inlineContent,
onTextLayout = {
layoutResult.value = it
onTextLayout(it)
},
style = style
)
}
fun CharSequence.toAnnotatedString(
urlSpanStyle: SpanStyle = SpanStyle(
color = Color.Blue,
textDecoration = TextDecoration.Underline
),
colorMapping: Map<Color, Color> = emptyMap(),
): AnnotatedString {
return if (this is Spanned) {
this.toAnnotatedString(urlSpanStyle, colorMapping)
} else {
buildAnnotatedString {
append(this@toAnnotatedString.toString())
}
}
}
fun Spanned.toAnnotatedString(
urlSpanStyle: SpanStyle = SpanStyle(
color = Color.Blue,
textDecoration = TextDecoration.Underline
),
colorMapping: Map<Color, Color> = emptyMap(),
): AnnotatedString {
return buildAnnotatedString {
append(this@toAnnotatedString.toString())
val urlSpans = getSpans<URLSpan>()
val styleSpans = getSpans<StyleSpan>()
val colorSpans = getSpans<ForegroundColorSpan>()
val underlineSpans = getSpans<UnderlineSpan>()
val strikethroughSpans = getSpans<StrikethroughSpan>()
val relativeSizeSpans = getSpans<RelativeSizeSpan>()
urlSpans.forEach { urlSpan ->
val start = getSpanStart(urlSpan)
val end = getSpanEnd(urlSpan)
addStyle(urlSpanStyle, start, end)
addStringAnnotation("url", urlSpan.url, start, end) // NON-NLS
}
colorSpans.forEach { colorSpan ->
val start = getSpanStart(colorSpan)
val end = getSpanEnd(colorSpan)
addStyle(
SpanStyle(color = colorMapping.getOrElse(Color(colorSpan.foregroundColor)) { Color(colorSpan.foregroundColor) }),
start,
end
)
}
styleSpans.forEach { styleSpan ->
val start = getSpanStart(styleSpan)
val end = getSpanEnd(styleSpan)
when (styleSpan.style) {
Typeface.BOLD -> addStyle(SpanStyle(fontWeight = FontWeight.Bold), start, end)
Typeface.ITALIC -> addStyle(SpanStyle(fontStyle = FontStyle.Italic), start, end)
Typeface.BOLD_ITALIC -> addStyle(
SpanStyle(fontWeight = FontWeight.Bold, fontStyle = FontStyle.Italic),
start,
end
)
}
}
underlineSpans.forEach { underlineSpan ->
val start = getSpanStart(underlineSpan)
val end = getSpanEnd(underlineSpan)
addStyle(SpanStyle(textDecoration = TextDecoration.Underline), start, end)
}
strikethroughSpans.forEach { strikethroughSpan ->
val start = getSpanStart(strikethroughSpan)
val end = getSpanEnd(strikethroughSpan)
addStyle(SpanStyle(textDecoration = TextDecoration.LineThrough), start, end)
}
relativeSizeSpans.forEach { relativeSizeSpan ->
val start = getSpanStart(relativeSizeSpan)
val end = getSpanEnd(relativeSizeSpan)
addStyle(style = SpanStyle(fontSize = relativeSizeSpan.sizeChange.em), start, end)
}
}
}