How to draw text of known width and height inside onDraw(Canvas canvas) method - android-custom-view

Here I am trying to make a Custom View containing text, I want to measure the text width and height so that I can also draw a background which fits the text of specific Measurment

I'm just taking a guess here, use wrap_content in layout_width and layout_height parameters in XML

Related

Adding Text to Objects During Zooming

I am evaluating fabricjs and wondering if you can dynamically add text to shapes while you are zooming. So for example while zoomed out, the shapes are too small to show the text but as you zoom and they get to a certain size, the text can be shown. Or is this part of the base control? In other words if I add text to an object but it cannot be shown because it is too small it automatically hides?

JavaFx image resizing

I have a borderPane with a menu top,a grid left and an image in the center.I want the image to have the same size as the center of the border because now the image goes over my grid.
I tried this:
imageView.fitWidthProperty().bind(box.widthProperty());
where imageView is the name for my image and box is the BorderPane object.Thank you.
See JavaFX Feature Request RT-21337 Add ImageViewPane and MediaViewPane controls which contains a code attachment for a sample ImageViewPane implementation which which will resize the ImageView it contains to the area available to the region. To get the behaviour required you might also need to implement computeMinWidth and computeMinHeight on the ImageViewPane so that they return zero rather than the minimum size of the Image.
now the image goes over my grid
This is because the minimum size of your image is currently larger than the available space of the center of your BorderPane:
BorderPane does not clip its content by default, so it is possible that childrens' bounds may extend outside its own bounds if a child's min size prevents it from being fit within it space.
Some potential alternatives to prevent the center content overflowing the border:
Manually set a clip on the center node to prevent it overflowing the borders.
Dynamically resize the ImageView as in the ImageViewPane sample linked above.
Place the ImageView in a ScrollPane.
Use the css -fx-background-image attributes to display and dynamcially resize your image rather than using an ImageView.
Set the center of the borderpane first before setting the border content, that way it will be rendered underneath the border content.
Use a different construct from a borderpane (e.g. a HBoxes, VBoxes, etc.) which don't overlap their nodes when the nodes are larger than the available display area.
I tried this: imageView.fitWidthProperty().bind(box.widthProperty());
My guess from the code provided is that this didn't work because the enclosing box of the image is a dynamically resizable pane of some sort, so the minimum width of the box is being determined by the width of the image and not vice versa.

How can I resize a NSTextView automatically based on the size of the contents?

I am implementing an iChat-like app, using NSTextView to display chat records. The problem is how to change the width of NSTextView automatically based on characters in it. For example, the width of NSTextView is only one character width if there's only one character in it. More than 100 characters will automatically increase the height of NSTextView.
You can use the string addition [-NSString boundingRectWithSize:options:attributes:] to get a bounding rectangle. You might also want to look into the Layout Manager.

Setting font size based on line height in NSTextField

I have an NSTextField where the font used can be changed by the user.
Although the font's point size remains the same, the actual height of the font is much bigger for some fonts. This means if the user changes the font to 'Zapfino' for example, most of the text is cropped. I would like it so the text in the box always looks roughly the same size.
Also the line height seems to change depending on which font is used meaning they don't line up well and sometimes get pushed down and the bottom gets cropped off.
How can I keep the text size and line height looking the same?
You need NSTextField which is using single line. You can do it in Attributes inspector by checking Uses Single Line Mode.
How to do it:
And now Your text will be like this:
Do programatically:
To change NSTextField height programatically by font size or scale the text to fit the bounds example here.

Height of NSTextView with one line?

I want to programatically create an NSTextView. How can I determine the correct frame height so that the view displays one line of text in the current default font?
The NSFont class has a method that can give you the size of a rectangle that would enclose a specific attributed string. Get the font used by your text view, create a string that serves as a reasonable example of what will be in the text view, and use that to inform your frame height. (The frame height will need to be some number of points larger than the actual rectangle the string would be displayed in.)
Alternately, you can get the various metrics from the font and attempt to calculate a reasonable frame from that. That might or might not work; for example, a font like Apple Chancery has a huge amount of variation depending on the glyphs that are being rendered, where they are in a word, and so on; I don't know that you can calculate what the needed size would be in advance without knowing exactly what you were going to render.
It would be more normal to be using an NSTextField than an NSTextView for a single line of text.
With NSTextField, just do the following:
[textField setFont:myFont];
[textField sizeToFit];
Oh, and there is no built-in 'current default font'. If an application has such a concept, it needs to track it itself. The font panel doesn't read or write to anything global, it's used to operate on specific text objects.

Resources