Welcome to the Pythonista Community Forums!

Pythonista is a Python programming environment for iOS. To learn more, head over to the Pythonista Website.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Twitter
Text-Size
  • I want to make a code which shows a text on the screen and highlights any word when you touch it. so i need a function which returns size of any word which is drawn on the screen. so far the most relevant thing i found was scene.render_text. but it gives you an image too.

    i am afraid that while computing a 300 word text, it loads 300 images (which i do not use) to the memory and have negative effect on performance.

    what do you think i can do?

    thnx

  • The best I can think of right now would be to use the ImageDraw module for drawing your text into an image (and then load that image via load_pil_image). The ImageDraw class has a textsize method that you could use to calculate the width of individual words without actually having to draw them first.

    Note that if you have a device with a retina screen, you might need to double all sizes when using the ImageDraw module (because it uses a pixel coordinate system and not points).
  • Thank you. Actually i had seen the ImageDraw option but I preferred to stick to TextLayer. But I guess I have to know how images of scene.render_text are stored. As far as I realized they are not stored in the image directory so I guess they must be stored in the memory. If so if I do this:

    For x in range(len(SomeStringList))
    p=scene.render_text(SomeStringList[x],font,size)
    S[x]=p[0]
    del p

    (storing just size and deleting the scene.render_text object) will it free the memory?

    You know, actually I have the same question about remove_layer. Will remove_layer free the memory dedicated to removed layer? (because in my code I repeatedly remove and add the same layers)

    Thnx again