Pixel-length of a string?

First of all, let me say that I’m rather new to Codea and Lua
I’m also new to this forum so hey guys (and girls)

So yeah, this is my question, how can I get the pixel-length of a string? I would use this to be able to make a textbox, where the cursor place can be changed with the touch of your finger (kinda like the normal apple textboxes)

Or do you guys suggest another method for doing this?

(Sorry if my English isn’t 100% correct, my native language is dutch)

Welcome to the forums!

You can get the size of a string like this:

local w, h = textSize("String")

w is the width, h is the height. It also varies depending on the current fontSize(), textWrapWidth(), and font().

well, for the textbox, that wouldn’t be a problem with fontSize and font because the cursor would divide the string in 2 seperate strings and thus it would evaluate each chars length etc etc (i’ll post it here if it’s done)

But what you’re actually saying is that the textSize actually is a vec2() ?
nope it isn’t a vec2, so i got to extract both values to get to it

But that doesn’t matter because eventually i’ll use the height var as well for some kind of multi-line textbox

Thx anyway :slight_smile:

I’m not sure what you mean. fontSize() changes the size of text, and font changes what the text looks like. I wasn’t saying it was a problem, I was saying it was helpful, because if you changed the size of your text or something it would still return an accurate size.

textSize() isn’t a vec2, it’s a function. You call it with. The parameter being the string you want to get the size of. It returns two variables, the width and height, so you have to get it in the way I previously mentioned.

ooh ok, thx :3

and yes i know, but sometimes i’m like talking/thinking to myself but keep typing it xd

Btw, i’ve got another small question, but it’dd be to silly to open a new Discussion about it

What would be the best way for a button to react…?

  1. When you press a button, execute function imediatly
  2. when you press button, and go off button while still keeping pressed, keep the button selected, and execute function when releasing touch (doesn’t matter on/off button since it’s selected)
  3. Press button but deselect when you go out of it’s boundaries and thus only execute function when releasing the button withing its boundaries

I always like buttons to have a dark tint while I’m pressing them and execute when I release my finger. And if I drag my finger off the button it won’t execute when I lift my finger, but I can always drag it back on the button again and when I lift my finger it executes. So if you missed the button you can drag your finger on to it, and if you didn’t mean to press it you can drag it away and be fine.

ok, so option 4 xD

well ok, i just need to play around with the touch.state a little, i already now how to do it tho, and i just need to put the color in a temp variable, so like, i got 2 colors

  1. self.colornormal
  2. self.colorpressed

and then
self.currentcolor = colornormal

in setup

so when drawing, just use the fill(self.currentcolor) and draw button

and when touching, just do self.currentcolor = self.colorpressed

or is there a more efficient way to do that? i’m here to learn :wink: