We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Attribute Modifier Utility

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

ModifierUtils

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

ModifierUtils is a utility class for AttributeModifier, used to provide methods for attribute operations. For example, it can determine whether a given instance is of a specified component type. This is applicable to scenarios where different component types need to be distinguished and differentiated attribute settings need to be applied within a unified AttributeModifier.

Since: 26.0.0

Atomic service API: This API can be used in atomic services since API version 26.0.0.

System capability: SystemCapability.ArkUI.ArkUI.Full

Model restriction: This API can be used only in the stage model.

isInstanceOf<T extends CommonMethod<T>>

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

isInstanceOf<T extends CommonMethod<T>>(instance: T, componentName: string): boolean

Checks whether a given instance is of a specified component type. For example, when implementing unified attribute modification logic for multiple component types in a custom AttributeModifier, this method can be used to determine the component type of the current instance, so that different attribute settings can be applied to different components.

Since: 26.0.0

Atomic service API: This API can be used in atomic services since API version 26.0.0.

System capability: SystemCapability.ArkUI.ArkUI.Full

Model restriction: This API can be used only in the stage model.

Parameters

Expand
Name Type Mandatory Description
instance T Yes Instance to check. T is the component attribute type that inherits from universal attributes (CommonMethod).
componentName string Yes Name of the component type to check. The value is the component class name (such as 'Text' or 'Button') and must exactly match the component class name (case-sensitive). Returns false if an invalid or nonexistent component class name is passed in.

Return value

Expand
Type Description
boolean Returns true if the instance is of the specified component type; returns false otherwise.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // xxx.ets
  2. // Click the button and check the log to determine whether the component enters the exclusive branch.
  3. import { ModifierUtils } from '@kit.ArkUI';
  4. class MyModifier implements AttributeModifier<TextAttribute | ButtonAttribute> {
  5. isDark: boolean = false;
  6. constructor(dark?: boolean) {
  7. this.isDark = dark ?? false;
  8. }
  9. applyNormalAttribute(instance: TextAttribute | ButtonAttribute): void {
  10. if (ModifierUtils.isInstanceOf(instance, 'Text')) {
  11. console.info('This is TextAttribute');
  12. const textInstance = instance as TextAttribute;
  13. if (this.isDark) {
  14. textInstance.backgroundColor(Color.Blue);
  15. } else {
  16. textInstance.backgroundColor(Color.Green);
  17. }
  18. } else if (ModifierUtils.isInstanceOf(instance, 'Button')) {
  19. console.info('This is ButtonAttribute');
  20. const buttonInstance = instance as ButtonAttribute;
  21. if (this.isDark) {
  22. buttonInstance.type(ButtonType.Circle);
  23. buttonInstance.backgroundColor(Color.Blue);
  24. } else {
  25. buttonInstance.type(ButtonType.Normal);
  26. buttonInstance.backgroundColor(Color.Green);
  27. }
  28. }
  29. }
  30. }
  31. @Entry
  32. @Component
  33. struct MultiComponentAttributeDemo {
  34. @State myModifier: MyModifier = new MyModifier();
  35. build() {
  36. Column() {
  37. Text('Text')
  38. .fontSize(50)
  39. .attributeModifier(this.myModifier)
  40. .onClick(() => {
  41. this.myModifier.isDark = !this.myModifier.isDark;
  42. })
  43. Button('Button')
  44. .attributeModifier(this.myModifier)
  45. .onClick(() => {
  46. this.myModifier.isDark = !this.myModifier.isDark;
  47. })
  48. }
  49. .justifyContent(FlexAlign.SpaceEvenly)
  50. .width('100%')
  51. .height('50%')
  52. }
  53. }
Search in References
Enter a keyword.