Pack text into a JFrame? - jframe

I'm wondering if there is a way to make text wrap around when it reaches the end of a frame? When using the drawString method, if a line of text exceeds the length of the frame, it seems to just continue on and not wrap around. Does anyone know a way around this?

JTextArea would probably suit. It displays text and you can set setLineWrap(true).
Wrapping text within a frame is a more complicated behavior than can/should be developed as a single method. For example, when you exceed the pre-assigned vertical area, what should happen? And what size of frame is required for layout?
For all these reasons, "display text wrapping in a frame" should be (and is) a component. That component is JTextArea.

Related

What's the proper way to align a strechable Text and an image to two ends in Unity UI?

I have an UI element that has a Text and an Image as its children. It has a HorizontalLayoutGroup component that enables Control Child Size and Child Force Expand.
I want the Image has a fixed size, and Text has strechable size controlled by the HorizontalLayoutGroup. So when the Image is set to inactive, the Text fills the whole space and when the Image is active, the Text shrinks a little bit in order to give space to the Image. Right now this part works good.
My second goal is to align them to both ends: the Text in the left and the Image on the right with space in between. But changing Child Alignment can't achieve this.
I tried the following solution:
Add LayoutElement both to Image and Text. On Text, enable Flexible Width and set a a value, on Image, enable Min Width and set to a value. Manually adjust the two values until it seems right.
This solution seems to work, but I don't know why. Is anyone familiar with this?What's the recommended way to do it? Thanks!
I worked it out. On the LayoutElement, treat Preferred Width or Preferred Height as Max Width or Max Height. Enable them, set the value the same with min value. One the other objects that you want to stretch, enable Flexible values. Then all worked as we want.

How to stop label in long text?

I'm trying to layout my UI. I want to do something like this:
I want the label gonna stop if it meet another view like heart button in this case
Is there any way or trick to do this ? Thank you so much.
If i understand you correctly what you need to do is:
1: This to calculate the width of the text lines.
2: Set a line height and line spacing
3: Get label top frame position add each row of text (height + spacing) and see if it intercepts the UIButton's frame and in that case truncate the line if line is greater than label.minX position - button.minX position
I have never done this myself. And as i'm not actually giving you a solution this is not the correct answer. But this is the approach i would have.
Good luck.
Edit:
As #Maddy mentioned, the exclusionPaths might work. But if you strive for truncating it i don't know if that is the correct approach.

Text component displaying lines in reverse order

Alright, I do not know how to fix this and only ran into this problem after trying to put in some longer text on a UI Text component. I have tried both pasting a value into its Text attribute through the Unity editor, and setting its value programmatically like this:
t.GetComponent<Text> ().text = "This is where meat, fish, and [...] (long text)"
Because Horizontal wrap is on, the text wraps when it reaches the edge of the available space.
However, the text displays backwards. Meaning, the start of the paragraph is at the bottom, and bottom at the top. Taking off wrap fixes this, but then the font size has to be really small (or it won't all be visible), and it can't form a normal paragraph because it has to... you know... wrap.
Is this a bug in Unity?
This is what happens - as you can see, it is displayed backwards:
The negative Line Spacing value is what is causing the issue here:
When the value of this field is less than 0, the lines will actually be arranged backwards, leading to the behaviour you're currently encountering. Just change the value to a positive number to have the text display correctly.

When is a GWT label's layout calculated?

I want to create a few labels, add them to an AbsolutePanel, and then calculate what positions they should go in. In order to do that calculation, I need to know how wide the browser has made the labels. When I call label.getOffsetWidth(), I get zero.
Is there anything I can do to force a layout calculation, or do I have to use something like scheduleDeferred() to set my positions after the layout has happened?
All widgets have a height and width of zero until the browser finishes rendering them. So you do have to use scheduleDeferred to wait until it happens.

Spacing between characters on the iPhone

I have a label and I wish to increase the spacing between characters.
I tried adding a space between each character, but this was too much
Perhaps there is a font with large spacing between the letters?
If all else fails, I am considering putting each character (only a size character code), into its own textbox.
Any ideas on how to achieve this?
There is a way to insert a half space, but I don't recall the exact command (option-spacebar?). Wikipedia has a complete list of spaces you can use.
Another approach would be a UIWebView with the letter-spacing CSS attribute set.
You're better off creating a custom view and using your drawRect routine to draw the text manually. You can use CFAttributedString to hold your text along with kerning information.
Update: sounds like you can't actually use CFAttributedString to draw text on the iPhone. You can still use your drawRect to draw the customized text, but it will take some more work to actually get your custom kerning to work.