11from abc import ABC , abstractmethod
2+ from typing import Any
23
34from .utils import IS_ANDROID , get_android_context
45from .view import ViewBase
@@ -21,6 +22,14 @@ def set_text(self, text: str) -> None:
2122 def get_text (self ) -> str :
2223 pass
2324
25+ @abstractmethod
26+ def set_text_color (self , color : Any ) -> None :
27+ pass
28+
29+ @abstractmethod
30+ def set_text_size (self , size : float ) -> None :
31+ pass
32+
2433
2534if IS_ANDROID :
2635 # ========================================
@@ -38,12 +47,37 @@ def __init__(self, text: str = "") -> None:
3847 self .native_instance = self .native_class (context )
3948 self .set_text (text )
4049
41- def set_text (self , text : str ) -> None :
50+ def set_text (self , text : str ):
4251 self .native_instance .setText (text )
52+ return self
4353
4454 def get_text (self ) -> str :
4555 return self .native_instance .getText ().toString ()
4656
57+ def set_text_color (self , color : Any ):
58+ # Accept int ARGB or hex string
59+ if isinstance (color , str ):
60+ c = color .strip ()
61+ if c .startswith ("#" ):
62+ c = c [1 :]
63+ if len (c ) == 6 :
64+ c = "FF" + c
65+ color_int = int (c , 16 )
66+ else :
67+ color_int = int (color )
68+ try :
69+ self .native_instance .setTextColor (color_int )
70+ except Exception :
71+ pass
72+ return self
73+
74+ def set_text_size (self , size_sp : float ):
75+ try :
76+ self .native_instance .setTextSize (float (size_sp ))
77+ except Exception :
78+ pass
79+ return self
80+
4781else :
4882 # ========================================
4983 # iOS class
@@ -59,8 +93,41 @@ def __init__(self, text: str = "") -> None:
5993 self .native_instance = self .native_class .alloc ().init ()
6094 self .set_text (text )
6195
62- def set_text (self , text : str ) -> None :
96+ def set_text (self , text : str ):
6397 self .native_instance .setText_ (text )
98+ return self
6499
65100 def get_text (self ) -> str :
66101 return self .native_instance .text ()
102+
103+ def set_text_color (self , color : Any ):
104+ # Accept int ARGB or hex string
105+ if isinstance (color , str ):
106+ c = color .strip ()
107+ if c .startswith ("#" ):
108+ c = c [1 :]
109+ if len (c ) == 6 :
110+ c = "FF" + c
111+ color_int = int (c , 16 )
112+ else :
113+ color_int = int (color )
114+ try :
115+ UIColor = ObjCClass ("UIColor" )
116+ a = ((color_int >> 24 ) & 0xFF ) / 255.0
117+ r = ((color_int >> 16 ) & 0xFF ) / 255.0
118+ g = ((color_int >> 8 ) & 0xFF ) / 255.0
119+ b = (color_int & 0xFF ) / 255.0
120+ color_obj = UIColor .colorWithRed_green_blue_alpha_ (r , g , b , a )
121+ self .native_instance .setTextColor_ (color_obj )
122+ except Exception :
123+ pass
124+ return self
125+
126+ def set_text_size (self , size : float ):
127+ try :
128+ UIFont = ObjCClass ("UIFont" )
129+ font = UIFont .systemFontOfSize_ (float (size ))
130+ self .native_instance .setFont_ (font )
131+ except Exception :
132+ pass
133+ return self
0 commit comments