Created
May 30, 2016 13:08
-
-
Save Phuket2/a40bd8769269577af0caf4ad6f4ea470 to your computer and use it in GitHub Desktop.
TextRender.py
This file contains hidden or 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
| import ui | |
| import copy | |
| class UIText(object): | |
| dict_list = [] | |
| def __init__(self, r = ui.Rect(), *args, **kwargs): | |
| r = ui.Rect(*r) | |
| # defaults | |
| self.rect = r | |
| self.text = 'SAMPLE' | |
| self.font_name = 'Arial Rounded MT Bold' | |
| self.font_size = 0 | |
| self.text_color = 'white' | |
| self.margin = (0, 0) | |
| self.origin = (0,0) | |
| self.use_shadow = True | |
| self.shadow_params = ('yellow', 4, 4, 2) | |
| self.do_kwargs(**kwargs) | |
| def do_kwargs(self, **kwargs): | |
| for k, v in kwargs.items(): | |
| if hasattr(self, k): | |
| setattr(self, k, v) | |
| def get_max_fontsize(self): | |
| r = ui.Rect(*self.rect).inset(*self.margin) | |
| last_w = last_h = 0 | |
| for i in range(5, 1000): | |
| w, h = ui.measure_string(self.text, max_width=0, | |
| font=(self.font_name, i), alignment=ui.ALIGN_CENTER, | |
| line_break_mode=ui.LB_TRUNCATE_TAIL) | |
| if w > r.width or h > r.height: | |
| return (i - 1, ui.Rect(0, 0, last_w, last_h)) | |
| last_w, last_h = w, h | |
| def draw_text(self, **kwargs): | |
| self.do_kwargs(**kwargs) | |
| r = ui.Rect(*self.rect).inset(*self.margin).translate(*self.origin) | |
| my_center = r.center() | |
| font_size = self.font_size | |
| if not font_size: | |
| result = self.get_max_fontsize() | |
| r1 = ui.Rect(*result[1]) | |
| font_size = result[0] | |
| else: | |
| w, h = ui.measure_string(self.text, max_width=0, | |
| font=(self.font_name, font_size), alignment=ui.ALIGN_CENTER, | |
| line_break_mode=ui.LB_TRUNCATE_TAIL) | |
| r1 = ui.Rect(0, 0, w, h) | |
| r1.center(my_center) | |
| with ui.GState(): | |
| if self.use_shadow: | |
| ui.set_shadow(*self.shadow_params) | |
| ui.draw_string(self.text, rect=r1, | |
| font=(self.font_name, font_size), color = self.text_color, | |
| alignment=ui.ALIGN_CENTER, | |
| line_break_mode=ui.LB_TRUNCATE_TAIL) | |
| def __enter__(self): | |
| __class__.dict_list.append(copy.copy(self.__dict__)) | |
| return self | |
| def __exit__(self, type, value, traceback): | |
| self.__dict__.update(__class__.dict_list.pop()) | |
| class MyClass(ui.View): | |
| def __init__(self, *args, **kwargs): | |
| super().__init__(*args, **kwargs) | |
| self.r = (100, 100, 400, 400) | |
| self.text_class = UIText(r = self.r, text_color = 'black') | |
| def draw(self): | |
| r = self.r #(100, 100, 400, 400) | |
| ui.set_color('teal') | |
| s = ui.Path.rounded_rect(*r, 24) | |
| s.fill() | |
| #r1 = ui.Rect(*r).inset(40, 40) | |
| self.text_class.draw_text(text = 'PP') | |
| if __name__ == '__main__': | |
| w = 600 | |
| h = 800 | |
| f = (0, 0, w, h) | |
| mc = MyClass(frame = f, bg_color = 'white') | |
| #print(get_max_fontsize(mc.bounds, '12345', 'Arial Rounded MT Bold')) | |
| mc.present('sheet') | |
| cls = UIText(r = (100, 100, 400, 400)) | |
| with cls as cl: | |
| cl.font_name = 'yellow' | |
| with mc.text_class as txt: | |
| txt.font_name = 'black' | |
| print(mc.text_class.font_name) | |
| with mc.text_class as txt2: | |
| txt2.font_name = 'green' | |
| print(mc.text_class.font_name) | |
| with mc.text_class as txt3: | |
| txt3.font_name = 'orange' | |
| print(mc.text_class.font_name) | |
| print(mc.text_class.font_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment