### Per-Instance Configuration Example Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Shows how to create a new PanguTextConfig with copyFromGlobal=false or copy from an existing config, and then manually inject with the config. Use this for personalized configuration when manually injecting or formatting text. ```kotlin // Assume this is your TextView. val textView: TextView // Create a new configuration. // You can set [copyFromGlobal] to false to not copy from the global configuration. val config = PanguTextConfig(copyFromGlobal = false) { excludePatterns.add("https?://\\S+".toRegex()) excludePatterns.add("\\[.*?]".toRegex()) cjkSpacingRatio = 7f } // You can also copy and create a new configuration from any configuration. val config2 = config.copy { excludePatterns.clear() excludePatterns.add("https?://\\S+".toRegex()) excludePatterns.add("\\[.*?]".toRegex()) cjkSpacingRatio = 7f } // Manually inject and configure. textView.injectPanguText(config = config2) ``` -------------------------------- ### Global Configuration Example Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Shows how to get and modify the global PanguText configuration. Configure before calling PanguTextFactory2.inject(...) when using the Inject to LayoutInflater method, otherwise the configuration will not take effect. ```kotlin // Get global configuration. val config = PanguText.globalConfig // Enable or disable the feature. config.isEnabled = true // Process Spanned text. // Enabled by default. Turn it off if you want to skip already styled text. // When disabled, Spanned text will return the original text. config.isProcessedSpanned = true // Whether to automatically re-measure the text width after processing. // Note: After [PanguText] injects text and changes the text, // the width of [TextView] will not be calculated automatically. // At this time, this feature will call [TextView.setText] to re-execute the measurements, // which can fix issues in some dynamic layouts (such as `RecyclerView`) where text width changes each time, // but may cause performance issues. You can choose to disable this feature. // To prevent unnecessary performance overhead, // this feature only takes effect on [TextView] with `maxLines` set to 1 or `singleLine`. config.isAutoRemeasureText = true // Set patterns to exclude during formatting using regular expressions. // For example, exclude all URLs. config.excludePatterns.add("https?://\\S+".toRegex()) // For example, exclude emoji placeholders like "[doge]". // If you use [ImageSpan] to display emoji images, you can choose to exclude these placeholders. config.excludePatterns.add("\\[.*?]".toRegex()) // Set the spacing ratio for CJK characters. // This determines the final layout effect. // It is recommended to keep the default ratio and adjust it according to personal preference. config.cjkSpacingRatio = 7f ``` -------------------------------- ### 配置存储库 (build.gradle.kts) Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/guide/quick-start.md 配置 PanguText 的依赖存储库。在项目的 build.gradle.kts 中添加 Maven Central 和可选的公共存储库。 ```kotlin repositories { google() mavenCentral() // (可选) 你可以添加此 URL 以使用我们的公共存储库 // 当 Sonatype-OSS 发生故障无法发布依赖时,此存储库作为备选进行添加 // 详情请前往:https://github.com/HighCapable/maven-repository // 中国大陆用户请将下方的 "raw.githubusercontent.com" 修改为 "raw.gitmirror.com" maven("https://raw.githubusercontent.com/HighCapable/maven-repository/main/repository/releases") } ``` -------------------------------- ### 全局配置 PanguText.globalConfig Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/library/android.md 通过全局静态实例 PanguText.globalConfig 获取全局配置,可设置开关、处理 Spanned 文本、自动重新测量文本宽度、排除正则模式以及 CJK 空白占位间距比例。注意:若使用注入布局装载器方案,需在 PanguTextFactory2.inject(...) 执行前配置,否则配置不生效。 ```kotlin // 获取全局配置 val config = PanguText.globalConfig // 开关,禁用将使所有功能失效 config.isEnabled = true // 处理 Spanned 文本 // 默认启用,如需跳过已带样式的文本可将其关闭 // 关闭后遇到 Spanned 文本将返回原始文本 config.isProcessedSpanned = true // 是否要在处理后自动重新测量文本宽度 // 注意:[PanguText] 注入文本并更改文本后,[TextView] 的宽度将不会自动计算 // 目前,此功能将调用 [TextView.setText] 重新执行测量结果, // 该测量可以在某些动态布局 (例如 `RecyclerView`) 中每次修复文本宽度, // 但可能会导致性能问题,你可以选择禁用此功能 // 为了防止不必要的性能开销,此功能仅在 `maxlines` 设置为 1 或 `singleLine` 的 [TextView] 上生效 config.isAutoRemeasureText = true // 设置在格式化过程中以正则形式定义需要排除的内容 // 例如排除全部 URL config.excludePatterns.add("https?://\\S+".toRegex()) // 例如排除类似 "[doge]" 的 emoji 占位符, // 如果你需要使用 [ImageSpan] 显示 emoji 图片,你可以选择排除这些占位符 config.excludePatterns.add("\\[.*?]".toRegex()) // 设置 CJK 空白占位间距比例 // 这会决定最终的排版效果,建议保持默认比例,然后再以此跟随个人喜好进行调整 config.cjkSpacingRatio = 7f ``` -------------------------------- ### 自定义 View 实现 PanguTextView 接口 Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/library/android.md 在自定义 View 中继承 PanguTextView 接口并实现 configurePanguText 方法进行配置。注意:接口配置优先级高于 XML 属性,单独配置覆盖全局配置,未配置项跟随全局配置。 ```kotlin class MyTextView(context: Context, attrs: AttributeSet? = null) : AppCompatTextView(context, attrs), PanguTextView { override fun configurePanguText(config: PanguTextConfig) { // 配置你的 [PanguTextConfig] } } ``` -------------------------------- ### Add PanguText Android dependency using Version Catalog in build.gradle.kts Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Configure the dependency in your project's `build.gradle.kts` file using the version catalog. This is the recommended method. ```kotlin implementation(libs.pangutext.android) ``` -------------------------------- ### 手动注入时传入 config 参数 Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/library/android.md 在手动注入或格式化文本时,可创建新的 PanguTextConfig 并传入,支持从全局配置复制或从任意配置复制。示例展示了创建新配置、复制配置并手动注入。 ```kotlin // 假设这就是你的 TextView val textView: TextView // 创建一个新配置 // 你可以设置 [copyFromGlobal] 为 false 来不从全局配置中复制配置 val config = PanguTextConfig(copyFromGlobal = false) { excludePatterns.add("https?://\\S+".toRegex()) excludePatterns.add("\\[.*?]".toRegex()) cjkSpacingRatio = 7f } // 你还可以从任意一个配置中复制并创建新配置 val config2 = config.copy { excludePatterns.clear() excludePatterns.add("https?://\\S+".toRegex()) excludePatterns.add("\\[.*?]".toRegex()) cjkSpacingRatio = 7f } // 手动注入并配置 textView.injectPanguText(config = config2) ``` -------------------------------- ### Add PanguText Android dependency using Version Catalog in libs.versions.toml Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Add the PanguText Android version and library entries to your project's `gradle/libs.versions.toml` file. Replace `` with the version displayed at the top of the document. ```toml [versions] pangutext-android = "" [libraries] pangutext-android = { module = "com.highcapable.pangutext:pangutext-android", version.ref = "pangutext-android" } ``` -------------------------------- ### 使用 PanguText.format 格式化文本 Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/library/android.md PanguText.format 有两个重载版本:传入 resources 和 textSize 时使用 SpannableString 方案,不修改文本内容;只传入文本时使用空白字符插入方案,会执行文本修正规则并返回插入空白字符的字符串。 ```kotlin // 假设这就是你的 TextView val textView: TextView // 使用 SpannableString 方案格式化文本 // 需要传入当前 TextView 的 Resources 以及字体大小 // 如果传入的文本自身为 Spannable 类型,则不会创建新的 SpannableString,而是返回原始对象 val text = PanguText.format(textView.resources, textView.textSize, "Xiaoming今年16岁") // 设置文本 textView.text = text // 直接使用空白字符以插入破坏的方式格式化文本 // 这个方案会为文本增加额外的空白字符 " " (HSP) // 下方的结果会输出字符串 "Xiaoming 今年 16 岁" // 你也可以在方法末位自定义要使用的空白字符 val text = PanguText.format("Xiaoming今年16岁") // 设置文本 textView.text = text ``` -------------------------------- ### Add PanguText Android dependency using traditional method in build.gradle.kts Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Configure the dependency in your project's `build.gradle.kts` file using the traditional method. Replace `` with the version displayed at the top of the document. ```kotlin implementation("com.highcapable.pangutext:pangutext-android:") ``` -------------------------------- ### Configure repositories in build.gradle.kts Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/guide/quick-start.md Add Maven Central and optionally the HighCapable public repository to your project's build.gradle.kts. The public repository is a backup when Sonatype-OSS fails. ```kotlin repositories { google() mavenCentral() // (Optional) You can add this URL to use our public repository // When Sonatype-OSS fails and cannot publish dependencies, this repository is added as a backup // For details, please visit: https://github.com/HighCapable/maven-repository maven("https://raw.githubusercontent.com/HighCapable/maven-repository/main/repository/releases") } ``` -------------------------------- ### Custom View Integration Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/library/android.md Shows how to create a custom TextView that overrides setText to apply PanguText formatting. ```APIDOC ## Custom View Integration ### Description Shows how to create a custom TextView that overrides setText to apply PanguText formatting. ### Method Override setText in a custom View ### Example ```kotlin class MyTextView(context: Context, attrs: AttributeSet? = null) : AppCompatTextView(context, attrs) { override fun setText(text: CharSequence?, type: BufferType?) { // 在这里手动进行注入 val panguText = text?.let { PanguText.format(resources, textSize, it) } super.setText(panguText, type) } } ``` ### Warning `TextView` 在注入 PanguText 后,如果你在 XML 布局中使用了 `android:singleLine="true"` 或在代码中使用了 `TextView.setSingleLine(true)` 并且配合 `android:elipsize="..."`, 那么这种方式设置单行文本可能会造成文本超出屏幕后其中会中显示出无法解析的 `OBJ` 字符 (被省略号截断),因为 `TextView` 不会在测量时计算文本中的 `Span`,这会导致文本宽度计算错误。 解决方案为在 XML 布局中使用 `android:maxLines="1"` 或在代码中使用 `TextView.setMaxLines(1)` 来代替。 > 示例如下 ```xml ``` ``` -------------------------------- ### Inject PanguTextFactory2 into AppCompatActivity with ViewBinding Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/library/android.md Inject PanguTextFactory2 into an AppCompatActivity using ViewBinding. This is the recommended integration method and works with recycled layouts like ListView and RecyclerView. ```kotlin class MainActivity : AppCompactActivity() { val binding by lazy { ActivityMainBinding.inflate(layoutInflater) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // 在这里注入 PanguTextFactory2.inject(this) setContentView(binding.root) } } ``` -------------------------------- ### TextView.setHintWithPangu(hint: String) Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Sets hint text with PanguText style on a TextView. ```APIDOC ## TextView.setHintWithPangu(hint: String) ### Description Sets hint text with PanguText style on a TextView. ### Method setHintWithPangu ### Parameters - **hint** (String) - Required - The hint text to set with PanguText style. ### Request Example ```kotlin // Assume this is your TextView. val textView: TextView // Set Hint with PanguText style. textView.setHintWithPangu("输入Xiaoming的年龄") ``` ### Response No return value. ``` -------------------------------- ### 自定义 View 继承 AppCompatTextView 并重写 setText Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/library/android.md 自定义 View 继承 AppCompatTextView 并重写 setText 方法,在内部调用 PanguText.format 进行手动注入。注意:使用单行文本时,应使用 maxLines 代替 singleLine 以避免 OBJ 字符显示问题。 ```kotlin class MyTextView(context: Context, attrs: AttributeSet? = null) : AppCompatTextView(context, attrs) { override fun setText(text: CharSequence?, type: BufferType?) { // 在这里手动进行注入 val panguText = text?.let { PanguText.format(resources, textSize, it) } super.setText(panguText, type) } } ``` -------------------------------- ### 使用 setTextWithPangu 和 setHintWithPangu 设置文本 Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/library/android.md 通过扩展方法 setTextWithPangu 和 setHintWithPangu 直接设置带有 PanguText 样式的文本或 Hint。 ```kotlin // 假设这就是你的 TextView val textView: TextView // 设置带有 PanguText 样式的文本 textView.setTextWithPangu("Xiaoming今年16岁") // 设置带有 PanguText 样式的 Hint textView.setHintWithPangu("输入Xiaoming的年龄") ``` -------------------------------- ### Format text with PanguText.format using SpannableString and whitespace methods Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Two overloaded versions: one uses SpannableString and requires Resources and text size, the other uses whitespace characters (HSP) and supports custom whitespace. ```kotlin // Assume this is your TextView. val textView: TextView // Format text using SpannableString method. // Requires passing the current TextView's Resources and text size. // If the input text is already Spannable, // it will return the original object without creating a new SpannableString. val text = PanguText.format(textView.resources, textView.textSize, "Xiaoming今年16岁") // Set text. textView.text = text // Directly format text using whitespace characters for insertion. // This method adds extra whitespace characters " " (HSP) to the text. // The result below will output the string "Xiaoming 今年 16 岁". // You can also customize the whitespace character at the end of the method. val text = PanguText.format("Xiaoming今年16岁") // Set text. textView.text = text ``` -------------------------------- ### 手动注入 PanguText 到 TextView 和 EditText Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/library/android.md 在 TextView 或 EditText 上调用 injectPanguText 或 injectRealTimePanguText 方法,可注入 PanguText 样式。injectRealTimePanguText 用于动态注入,重新调用 setText 也会自动生效。默认同时注入 Hint,可通过 injectHint 参数关闭。 ```kotlin // 假设这就是你的 TextView val textView: TextView // 假设这就是你的 EditText val editText: EditText // 注入到现有文本 textView.injectPanguText() editText.injectPanguText() // 可以选择是否同时注入 Hint (默认是) textView.injectPanguText(injectHint = false) editText.injectPanguText(injectHint = false) // 动态注入,重新调用 setText 也会自动生效 textView.injectRealTimePanguText() // 动态注入主要针对于 EditText 的输入状态 editText.injectRealTimePanguText() // 同样可以选择是否同时注入 Hint (默认是) textView.injectRealTimePanguText(injectHint = false) editText.injectRealTimePanguText(injectHint = false) ``` -------------------------------- ### XML 布局属性配置 Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/library/android.md 在 XML 布局中为 TextView、EditText 或继承于它们的视图使用 panguText_ 前缀属性进行个性化配置。注意:Android Studio 可能无补全提示,需自行补全,并添加 xmlns:app 声明。 ```xml ``` -------------------------------- ### Inject PanguTextFactory2 into plain Activity Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md For applications not using AppCompatActivity or ViewBinding, inject the factory directly into the Activity before setContentView. ```kotlin class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Inject here. PanguTextFactory2.inject(this) setContentView(R.layout.activity_main) } } ``` -------------------------------- ### Inject PanguTextFactory2 into AppCompatActivity Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Recommended integration method for AppCompatActivity. Inject PanguTextFactory2 before setContentView to take over layout inflation. This handles recycled layouts like ListView and RecyclerView, and affects Fragment, Dialog, PopupWindow, and Toast (foreground only in higher system versions). ```kotlin class MainActivity : AppCompatActivity() { val binding by lazy { ActivityMainBinding.inflate(layoutInflater) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Inject here. PanguTextFactory2.inject(this) setContentView(binding.root) } } ``` -------------------------------- ### TextView.injectPanguText(injectHint: Boolean = true) Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Injects PanguText formatting into an existing TextView or EditText. Optionally injects the hint text. ```APIDOC ## TextView.injectPanguText(injectHint: Boolean = true) ### Description Injects PanguText formatting into an existing TextView or EditText. Optionally injects the hint text. ### Method injectPanguText ### Parameters - **injectHint** (Boolean) - Optional - Whether to inject the hint text. Default is true. ### Request Example ```kotlin // Assume this is your TextView. val textView: TextView // Assume this is your EditText. val editText: EditText // Inject into existing text. textView.injectPanguText() editText.injectPanguText() // Optionally choose whether to inject Hint (default is true). textView.injectPanguText(injectHint = false) editText.injectPanguText(injectHint = false) ``` ### Response No return value. ``` -------------------------------- ### Custom View implementing PanguTextView interface Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Extend your View and implement the PanguTextView interface to configure PanguText in a custom View. The interface configuration overrides XML layout attributes, and individual configurations override global ones. ```kotlin class MyTextView(context: Context, attrs: AttributeSet? = null) : AppCompatTextView(context, attrs), PanguTextView { override fun configurePanguText(config: PanguTextConfig) { // Configure your [PanguTextConfig]. } } ``` -------------------------------- ### PanguText.format Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/library/android.md Formats text with PanguText. Two overloads: one using SpannableString with resources and text size, another inserting whitespace characters. ```APIDOC ## PanguText.format ### Description Formats text with PanguText. Two overloads: one using SpannableString with resources and text size, another inserting whitespace characters. ### Method format ### Parameters #### Overload 1: format(resources, textSize, text, ...) - **resources** (Resources) - Required - The Resources of the current TextView. - **textSize** (Float) - Required - The text size of the current TextView. - **text** (CharSequence) - Required - The text to format. - **config** (optional) - Optional - Configuration for formatting. #### Overload 2: format(text, ...) - **text** (CharSequence) - Required - The text to format. - **config** (optional) - Optional - Configuration for formatting. ### Description of Overloads - `PanguText.format(resources, textSize, text, ...)` scans the original text and patches PanguMarginSpan without modifying text content or applying text correction rules. - `PanguText.format(text, ...)` preserves existing regex replacements including text correction rules and returns text with inserted whitespace characters. ### Example ```kotlin // 使用 SpannableString 方案格式化文本 // 需要传入当前 TextView 的 Resources 以及字体大小 // 如果传入的文本自身为 Spannable 类型,则不会创建新的 SpannableString,而是返回原始对象 val text = PanguText.format(textView.resources, textView.textSize, "Xiaoming今年16岁") // 设置文本 textView.text = text // 直接使用空白字符以插入破坏的方式格式化文本 // 这个方案会为文本增加额外的空白字符 " " (HSP) // 下方的结果会输出字符串 "Xiaoming 今年 16 岁" // 你也可以在方法末位自定义要使用的空白字符 val text = PanguText.format("Xiaoming今年16岁") // 设置文本 textView.text = text ``` ``` -------------------------------- ### XML 布局中使用 maxLines 避免单行文本问题 Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/zh-cn/library/android.md 当使用单行文本时,应使用 android:maxLines="1" 代替 android:singleLine="true",以避免文本超出屏幕后显示无法解析的 OBJ 字符。 ```xml ``` -------------------------------- ### TextView.injectRealTimePanguText(injectHint: Boolean = true) Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Dynamically injects PanguText formatting into a TextView or EditText. Re-calling setText will automatically take effect. Mainly targets the input state of EditText. ```APIDOC ## TextView.injectRealTimePanguText(injectHint: Boolean = true) ### Description Dynamically injects PanguText formatting into a TextView or EditText. Re-calling setText will automatically take effect. Mainly targets the input state of EditText. ### Method injectRealTimePanguText ### Parameters - **injectHint** (Boolean) - Optional - Whether to inject the hint text. Default is true. ### Request Example ```kotlin // Assume this is your TextView. val textView: TextView // Assume this is your EditText. val editText: EditText // Dynamic injection, re-calling setText will automatically take effect. textView.injectRealTimePanguText() // Dynamic injection mainly targets the input state of EditText. editText.injectRealTimePanguText() // Optionally choose whether to inject Hint (default is true). textView.injectRealTimePanguText(injectHint = false) editText.injectRealTimePanguText(injectHint = false) ``` ### Response No return value. ``` -------------------------------- ### Patch root layout with PanguTextPatcher Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Patches the entire root layout, automatically patching all TextView instances or subclasses under it. ```kotlin // Assume you have a root layout. val root: ViewGroup // Patch the root layout. PanguTextPatcher.patch(root) ``` -------------------------------- ### Inject PanguTextFactory2 in AppBindingActivity Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md For projects using BetterAndroid's AppBindingActivity, override onPrepareContentView to inject the factory into the LayoutInflater before it is used. ```kotlin class MainActivity : AppBindingActivity() { override fun onPrepareContentView(savedInstanceState: Bundle?): LayoutInflater { val inflater = super.onPrepareContentView(savedInstanceState) // Inject here. PanguTextFactory2.inject(inflater) return inflater } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Your code here. } } ``` -------------------------------- ### Set text with PanguText style using setTextWithPangu and setHintWithPangu Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Extends TextView's setText method to directly set text with PanguText style. ```kotlin // Assume this is your TextView. val textView: TextView // Set text with PanguText style. textView.setTextWithPangu("Xiaoming今年16岁") // Set Hint with PanguText style. textView.setHintWithPangu("输入Xiaoming的年龄") ``` -------------------------------- ### PanguTextPatcher.patch(root: ViewGroup) Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Patches an entire root layout, automatically patching all TextView instances or their subclasses under the root layout. ```APIDOC ## PanguTextPatcher.patch(root: ViewGroup) ### Description Patches an entire root layout, automatically patching all TextView instances or their subclasses under the root layout. ### Method PanguTextPatcher.patch ### Parameters - **root** (ViewGroup) - Required - The root layout to patch. ### Request Example ```kotlin // Assume you have a root layout. val root: ViewGroup // Patch the root layout. PanguTextPatcher.patch(root) ``` ### Response No return value. ``` -------------------------------- ### Manual injection into TextView and EditText Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Injects PanguText into existing TextView or EditText instances. Supports optional hint injection (default true) and dynamic injection for real-time updates. ```kotlin // Assume this is your TextView. val textView: TextView // Assume this is your EditText. val editText: EditText // Inject into existing text. textView.injectPanguText() editText.injectPanguText() // Optionally choose whether to inject Hint (default is true). textView.injectPanguText(injectHint = false) editText.injectPanguText(injectHint = false) // Dynamic injection, re-calling setText will automatically take effect. textView.injectRealTimePanguText() // Dynamic injection mainly targets the input state of EditText. editText.injectRealTimePanguText() // Optionally choose whether to inject Hint (default is true). textView.injectRealTimePanguText(injectHint = false) editText.injectRealTimePanguText(injectHint = false) ``` -------------------------------- ### PanguText.format(resources: Resources, textSize: Float, text: String, ...) Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Formats text using SpannableString method. Requires the current TextView's Resources and text size. If the input text is already Spannable, it returns the original object without creating a new SpannableString. ```APIDOC ## PanguText.format(resources: Resources, textSize: Float, text: String, ...) ### Description Formats text using SpannableString method. Requires the current TextView's Resources and text size. If the input text is already Spannable, it returns the original object without creating a new SpannableString. ### Method PanguText.format ### Parameters - **resources** (Resources) - Required - The current TextView's Resources. - **textSize** (Float) - Required - The current TextView's text size. - **text** (String) - Required - The text to format. - **config** (optional) - Optional - Configuration parameter for personalized configuration. ### Request Example ```kotlin // Assume this is your TextView. val textView: TextView // Format text using SpannableString method. // Requires passing the current TextView's Resources and text size. // If the input text is already Spannable, // it will return the original object without creating a new SpannableString. val text = PanguText.format(textView.resources, textView.textSize, "Xiaoming今年16岁") // Set text. textView.text = text ``` ### Response Returns a formatted SpannableString or the original object if already Spannable. ``` -------------------------------- ### PanguText.format(text: String, ...) Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Formats text using whitespace characters for insertion. This method adds extra whitespace characters " " (HSP) to the text. You can also customize the whitespace character at the end of the method. ```APIDOC ## PanguText.format(text: String, ...) ### Description Formats text using whitespace characters for insertion. This method adds extra whitespace characters " " (HSP) to the text. You can also customize the whitespace character at the end of the method. ### Method PanguText.format ### Parameters - **text** (String) - Required - The text to format. - **config** (optional) - Optional - Configuration parameter for personalized configuration. ### Request Example ```kotlin // Directly format text using whitespace characters for insertion. // This method adds extra whitespace characters " " (HSP) to the text. // The result below will output the string "Xiaoming 今年 16 岁". // You can also customize the whitespace character at the end of the method. val text = PanguText.format("Xiaoming今年16岁") // Set text. textView.text = text ``` ### Response Returns a formatted string with inserted whitespace characters. ``` -------------------------------- ### TextView.setTextWithPangu(text: String) Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Sets text with PanguText style on a TextView. ```APIDOC ## TextView.setTextWithPangu(text: String) ### Description Sets text with PanguText style on a TextView. ### Method setTextWithPangu ### Parameters - **text** (String) - Required - The text to set with PanguText style. ### Request Example ```kotlin // Assume this is your TextView. val textView: TextView // Set text with PanguText style. textView.setTextWithPangu("Xiaoming今年16岁") ``` ### Response No return value. ``` -------------------------------- ### XML configuration for TextView with PanguText attributes Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Use these XML attributes on a TextView to enable PanguText, process spanned text, auto-remeasure, set exclusion patterns, and adjust CJK spacing ratio. Note that Android Studio may not provide auto-completion hints for these attributes, so complete them manually and add the xmlns:app declaration. ```xml ``` -------------------------------- ### Patch a single TextView with PanguTextPatcher Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Patches a single View that is a TextView or subclass. ```kotlin // Assume this is your TextView. val textView: TextView // Patch a single View. PanguTextPatcher.patch(textView) ``` -------------------------------- ### Configure Java version in build.gradle.kts Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/guide/quick-start.md Set the Java version to 17 or above for both compile options and Kotlin JVM target. This is required for the project. ```kotlin android { compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } } tasks.withType().configureEach { compilerOptions { jvmTarget.set(JvmTarget.JVM_17) } } ``` -------------------------------- ### PanguTextPatcher.patch(view: TextView) Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Patches a single View that is of type TextView or a subclass of TextView. ```APIDOC ## PanguTextPatcher.patch(view: TextView) ### Description Patches a single View that is of type TextView or a subclass of TextView. ### Method PanguTextPatcher.patch ### Parameters - **view** (TextView) - Required - The TextView or subclass to patch. ### Request Example ```kotlin // Assume this is your TextView. val textView: TextView // Patch a single View. PanguTextPatcher.patch(textView) ``` ### Response No return value. ``` -------------------------------- ### Custom View with PanguText Source: https://github.com/betterandroid/pangutext/blob/main/docs-source/src/en/library/android.md Extend AppCompatTextView and override setText to manually inject PanguText formatting. Use this when you need to apply PanguText to a custom View. Note: using android:singleLine="true" or setSingleLine(true) with ellipsize may cause unresolvable OBJ characters; use maxLines="1" instead. ```kotlin class MyTextView(context: Context, attrs: AttributeSet? = null) : AppCompatTextView(context, attrs) { override fun setText(text: CharSequence?, type: BufferType?) { // Manually inject here. val panguText = text?.let { PanguText.format(resources, textSize, it) } super.setText(panguText, type) } } ``` ```xml ```