-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to vertically center text? #1015
Comments
First of all: VERY NICE !!! Since RichTextFX basically just uses a JavaFX TextFlow control for the layout of nodes (in your case Text & Canvas) try experimenting with a plain TextFlow to get the alignment you want (in various scenarios if need be). Once to you get that right, then you should be able to apply the same technique to RichTextFX. If you get what you want with TextFlow but can't seem to get the same results with RichTextFX then post your TextFlow findings here and I'll try and help if I can. |
Also try looking here in the Wiki, maybe this helps .... |
Thanks @Jugen! Looking into this, I found this JDK bug report. According to that, the best way to solve this issue seems to be by overriding From looking around the code I've learned that RichTextFX subclasses Therefore, subclassing |
Hi @mgroth0, ummm .... I think you misread the solution the JDK bug report offers ? It's not the TextFlow's baseline offset that gets overridden it's your Apparently something like the following (according to Christian) will roughly center the canvas on the line: public double getBaselineOffset()
{
return getHeight() * 0.75;
} Note that according to Felipe:
|
Excellent @Jugen, you are completely right! This was from the I understand if I need even more precision (for example to try to line up "sum of squares" with the equals sign on the second example) I can try Felipe's approach. However, I'm not sure of the best way to get |
Hey, that's great :-) Okay, so I have two suggestions on how to go about this:
public double getBaselineOffset()
{
// Hopefully the canvas is a child of TextFlow at this point ?
// Change, if you may want to find a more specific Text node ....
Optional<Text> text = getParent().getChildrenUnmodifiable().stream()
.filter( n -> n instanceof Text )
.map( n -> (Text) n )
.findFirst();
if ( text.isPresent() )
{
double txtBase = text.get().getBaselineOffset();
double txtHeight = text.get().getLayoutBounds().getHeight();
// This is my own untested calculation and probably isn't right ?
return txtBase + (txtHeight - txtBase)/2 - getHeight() / 2;
}
return getHeight() * 0.75;
}
|
@mgroth0 hope you came right with this .... please reopen if not. |
First of all to the creators/contributors, thanks so much for a great library.
I have create a
GenericStyledArea
that incorporates both text (using the defaultTextExt
) and custom nodes. For example, one custom node renders some math:Problem is they are not vertically aligned.
The rendered math is a
Canvas
. If it were translated up or down, this introduces possible clipping. So I think theTextExt
needs to be vertically centered, possible with an additional offset as well.However, using the
translateYProperty
for theTextExt
would also possibly cause clipping, unless it were bound to the height of theParagraphBox
. Even if it were bound to the height of theParagraphBox
, it would also need to consider how many line-wraps there are. This is way more complicated than setting thealignmentProperty
of anHBox
, for example.Also, the vertical alighment method needs to be segment by segment, and generalize to multi-line cases.
I couldn't find any vertical alignment property and a search for "vertical alignment" here comes up empty, so maybe not thinking about this correctly or maybe I'm searching the wrong terms.
Any ideas?
Here's a more extreme case:
The text was updated successfully, but these errors were encountered: