-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from akiomik/linkable-text
Linkable text
- Loading branch information
Showing
5 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
app/src/main/java/io/github/akiomik/seiun/ui/components/LinkableText.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.github.akiomik.seiun.ui.components | ||
|
||
import android.text.method.LinkMovementMethod | ||
import android.text.util.Linkify | ||
import android.widget.TextView | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import androidx.core.text.util.LinkifyCompat | ||
import io.github.akiomik.seiun.R | ||
|
||
// https://stackoverflow.com/a/68670583/1918609 | ||
@Composable | ||
fun LinkableText(text: String, modifier: Modifier = Modifier) { | ||
val context = LocalContext.current | ||
val customLinkifyTextView = remember { TextView(context) } | ||
|
||
AndroidView(modifier = modifier, factory = { customLinkifyTextView }) { textView -> | ||
textView.text = text | ||
LinkifyCompat.addLinks( | ||
textView, | ||
Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES or Linkify.PHONE_NUMBERS | ||
) | ||
textView.movementMethod = LinkMovementMethod.getInstance() | ||
textView.setTextAppearance(R.style.linkable_text) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<style name="linkable_text" parent="TextAppearance.AppCompat"> | ||
<item name="android:textColor">@color/white</item> | ||
<item name="android:textSize">16sp</item> | ||
<item name="android:lineHeight">24sp</item> | ||
</style> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<style name="linkable_text" parent="TextAppearance.AppCompat"> | ||
<item name="android:textSize">16sp</item> | ||
<item name="android:lineHeight">24sp</item> | ||
</style> | ||
</resources> |