feat: add PanguTextPatcher

This commit is contained in:
2025-03-05 21:48:55 +08:00
parent 6946e5e9be
commit 15e07c01dd
4 changed files with 150 additions and 17 deletions

View File

@@ -150,6 +150,39 @@ However, please inject before the `LayoutInflater` instance is used to load the
:::
#### Using the Patching Tool
You can use `PanguTextPatcher` to patch existing `View` or `ViewGroup` instances.
Patch the entire root layout, and `PanguTextPatcher` will automatically patch all `TextView` or its subclasses under the root layout.
> The following example
```kotlin
// Assume you have a root layout.
val root: ViewGroup
// Patch the root layout.
PanguTextPatcher.patch(root)
```
Patch a single `View`, which is of type `TextView` or a subclass of `TextView`.
> The following example
```kotlin
// Assume this is your TextView.
val textView: TextView
// Patch a single View.
PanguTextPatcher.patch(textView)
```
::: warning
When using `PanguTextPatcher` in recycled layouts such as `RecyclerView`, `ListView`, or `ViewPager`, you need to patch the `itemView` in `onCreateViewHolder` or `onBindViewHolder`,
otherwise it will not take effect.
:::
#### Manual Injection or Text Formatting
`PanguText` also supports manual injection, allowing you to inject it into the desired `TextView` or `EditText`.
@@ -356,6 +389,7 @@ Don't forget to add the declaration `xmlns:app="http://schemas.android.com/apk/r
:::
In custom `View`, you can extend your `View` to implement the `PanguTextView` interface to achieve the same functionality.
This feature is also effective for the [Using the Patching Tool](#using-the-patching-tool) method.
> The following example

View File

@@ -152,6 +152,38 @@ class MainActivity : Activity() {
:::
#### 使用修补工具
你可以使用 `PanguTextPatcher` 修补现有的 `View``ViewGroup` 实例。
修补整个根布局,`PanguTextPatcher` 会自动修补根布局下的所有 `TextView` 或继承于其的组件。
> 示例如下
```kotlin
// 假设你有一个根布局
val root: ViewGroup
// 修补根布局
PanguTextPatcher.patch(root)
```
修补单个 `View`,类型为 `TextView` 或继承于 `TextView` 的组件。
> 示例如下
```kotlin
// 假设这就是你的 TextView
val textView: TextView
// 修补单个 View
PanguTextPatcher.patch(textView)
```
::: warning
`RecyclerView``ListView``ViewPager` 等回收式布局中使用 `PanguTextPatcher` 时,你需要在 `onCreateViewHolder``onBindViewHolder` 中获取到 `itemView` 后进行修补,否则不会生效。
:::
#### 手动注入或格式化文本
`PanguText` 同样支持手动注入,你可以在需要的 `TextView``EditText` 上手动进行注入。
@@ -351,7 +383,7 @@ textView.injectPanguText(config = config2)
:::
在自定义 `View` 中,你可以将你的 `View` 继承于 `PanguTextView` 接口以同样实现上述功能。
在自定义 `View` 中,你可以将你的 `View` 继承于 `PanguTextView` 接口以同样实现上述功能,此功能对 [使用修补工具](#使用修补工具) 方案同样有效
> 示例如下