Jump to content

Centering text in Text2D


royibernthal
 Share

Recommended Posts

How do I make the Text2D text centered? As in centering the actual text according to the Text2D origin, not centering the Text2D within its parent.

Do I pass the desired origin in options in the Text2D constructor? I tried doing that and I didn't see any change, maybe I did it wrong?

Link to comment
Share on other sites

I'm not seeing anything in the docs for what I think you are asking for. 

 

I'm assuming

what you want is something

like this.

All this text would be

in one Text2D object. 

Right?

 

You can create a feature request here:

https://trello.com/b/eKMKhCFc/canvas2d

 

If you can't wait, you could write a function that parses your text and adds line breaks and extra spaces before adding it to the Text2D object.

centerText(text:string: width:number){

....

}

I might be able to help you with that, but I'd like to make sure someone else doesn't have a better solution.

Link to comment
Share on other sites

11 hours ago, Nabroski said:

If you not happy how text is centered(im happy with it) why you dont use all the other methods to get text align ?

By default the text is not centered. What text align methods are you talking about? It'd help if you could create a PG of a centered text if the solution for it exists.

 

@adam Yeah that's what I mean. I was thinking about changing the origin of the textfield to x: 0.5, that much seems to be covered in the docs. I can't seem to pull it off though, I suspect I'm not setting the origin right, or otherwise it doesn't behave the way I think it does. If you think it might be a direction, care to give it a try?

Regarding creating my own solution, yeah that's a possibility. Is there an event dispatched whenever the text is changed? If so, an optimized solution would be pretty easy to do.

 

@chicagobob123 Seems like a bit of an overkill don't you think?

Link to comment
Share on other sites

I was looking for an already built in solution, but since there is none, I believe this to be the simplest solution: textField.x = -textField.width / 2

I created a class extending Group2D containing Text2D and added align options, with centering being a simple textField.x = -textField.width / 2 that is executed in a text setter.

Link to comment
Share on other sites

I dislike static solutions (i.e. typing spaces), they'll usually require rewrite often, e.g. whenever you change design / font size / etc... even if you do know the text ahead of time.

Regarding your solution, while it's nice it's not very accurate (and also would probably require constant rewrite and attention as well in the long run).

If I'd have tried my hand at creating a centered multiline textfield, I'd probably create a Group2D containing an array of Text2D children according to the number of lines, spread them around according to each line's height and center them according to each line's width. It'd generate a very accurate result, though I'm not sure how efficient it'd be performance-wise.

Link to comment
Share on other sites

2 minutes ago, royibernthal said:

Regarding your solution, while it's nice it's not very accurate (and also would probably require constant rewrite and attention as well in the long run).

Yeah, that's why I said:

54 minutes ago, adam said:

I really need to use the width of each char though instead of counting the them.

 

Link to comment
Share on other sites

Did you consider creating a for loop that goes over all possible characters (e.g. a-z, 0-9) and test each char size by setting the char to a textfield's text and saving the textfield's size with only that char in it?

That'd give you each char's size (width x height), but even if you have that info, do you have a way to apply it in your solution or are you limited to pushing a rough number of spaces according to the actual space required?

 

Regarding hello world example - literally, it's a centered text, but it's not what I need - I tried to explain what I need in detail in my previous posts, problem solved though so I guess it doesn't matter anymore.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...