I want align text to center on elm_label widget. I can't find any functions about text align as evas object text function or elm_label function. Also, I can't find a function that can get the text size (absolute pixel or relative size) on a label.
I have tried elm_object_style_set with marker, it was showed bold text on center of label, but font size was too small (smaller than default style). (and I don't want bold on text.)
How I can align text to the center of a label?
You can use HTML like markup to customize the way you want to the display the text in elm_label.
To align the text to the center, you can use <align> markup tag with value center .
std::string text = "<align = center> TEXT TO DISPLAY </align>";
Evas_Object *label = elm_label_add(parent);
elm_object_text_set(label,text.c_str());
evas_object_show(label);
You can also use other tags like font_weight, font_style to customize your display text. List of all possible tags can be found here
You can set the alignment of the label through the evas_object API. Likely what you are looking for is:
evas_object_size_hint_align_set(label, 0.5, EVAS_HINT_FILL);
evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
The 0.5 is center, you could try 0.0 for left align and 1.0 for right.
Related
please see the image below for two examples of what is to be achived
the alignment should be on the Center Y of the first lines of each UILabels and should work regardless of font size or font. currently we have implemented this with different constraints to the top of the super view for different font and font size combinations.
the constraint to align the center of the two UILabels does not work since the text of the second UILabel is not fixed and can have several lines.
also the text is dynamic, so it is not known where the text will wrap to create the first line, thus it cannot be shown in an one line UILabel with the rest of the text in another one below.
currently this is implemented using UIKit, but if there is an easy solution in SwiftUI we can put these two labels in a SwiftUI component. so a SwiftUI solution would also be welcomed.
Your comments said "it should be on the glyphs" ... but, without additional information, my guess is that "real world" usage would not really need that level of precision.
For example:
While the glyphs are not perfectly center-Y aligned, it seems unlikely you'd run into a case where the first line of the "rightLabel" is " ' " ' " or . , . , ..
This layout can be easily done with only a few constraints - no need to do any calculations:
The "Positioning" label would, of course, be set .hidden = true so it would never be seen.
If you really, really want glyph-precision, you'll need to calculate
the Glyph bounding box for the left-label
the Glyph bounding box for first line of the right-label
calculate the "character box" offsets to align the Glyph Y-centers
and then position the two labels accordingly, or use Core Text to draw the text (instead of using UILabel).
Probably more work than necessary -- unless your actual use-case demands it.
That's an interesting problem! You can try using the centerYAnchor for the label on the left, and the firstBaselineAnchor for the label on the right... that will align the center Y with the text baseline, which isn't quite what you want.
To find the correct offset to apply, you can use the information from UIFont about the size of the characters. I'd probably start with capHeight * 0.5 and see if that looks or feels right. Something like:
leftLabel.centerYAnchor.constraint(equalTo: rightLabel.firstBaseLineAnchor, constant: rightFont.capHeight * 0.5)
This is a more difficult problem in SwiftUI, I think, because resolved font metrics aren't directly available to you.
I have a custom renderer that is attempting to create a white square with rounded corners containing a single font image at the top and text under it. I have been able to do something similar with the ImageRenderer, but am looking to support font images with this version.
In LayoutSubviews I set the frame to the requested size, set the corner radius, and set Clip to Bounds to true. At theis point in time Bounds are still (0,0,0,0) but setting there here has no effect.
Control.Layer.CornerRadius = 10;
Control.Layer.MasksToBounds = false;
Control.Layer.Frame = new CGRect(0, 0, element.WidthRequest, element.HeightRequest);
Control.Layer.BackgroundColor = HelperUtilities.GetColor(element.Item.BackgroundColor, Color.White).ToCGColor();
Control.ClipsToBounds = true;
I have also checked the value of Bounds in Draw both before and after calling the base code, and they are as expected - (0,0,126,126). Yet when the screen is drawn the white background is too short - it's a rectangle the proper width, but the height of the font icon, not the given height. I can change the font icon's placement within the square area by changing the VerticalTextAlignment from Start to End which moves it from the top to the bottom of the square.
Next, I add the UIView for the additional text and anchor it to the BottomAnchor of the Control, it anchors to the bottom of the white area rather than the bottom of the square which tells me the Control's Bounds have been changed so the height is no longer 126 but is the actual height of the Font and FontSize being used.
What do I need to set and/or override to get the control to have the given height instead of the font height?
This is what is displayed:
This is what I want it to display:
I was able to solve this by adding padding to the bottom of the object that inherits from Label in my forms app to account for the expanded area. I only add the padding if iOS as Android didn't have the issue. This was a much easier solution than adding it into the custom renderer.
When creating a treemap structure with boxes, boxes with a text that is larger than the box itself are not drawn.
Below the same treemap is show twice: once with text, once without text.
treemap with text
image without text
How do I prevent this from happening?
I'm pretty sure this can't be prevented. The reason is that the box sizes have hard constraints on their sizes (the size is as meaningful as the height of a bar in a bar chart), so they can't grow with the size of the enclosed text.
To fix this you could generate shorter labels (removing all the vowls and truncating them) and showing the full label as a tooltip? Or you could have a box with a label in it on the side which contains the currently hovered box?
I would like to align the start of the text of two UILabels. I aligned the two UILabels (with the yellow and grey background) and used sizeToFit: to shrink the UILabels to the content but the text is not perfectly left aligned. There is a gap on the left. The gap is bigger or smaller depending on the first character. I would like to align the red lines in the following picture. There is even a small gap with the small font in the grey UILabel but it's barely visible.
With the Z character the gap is smaller but still visible by the yellow area left to the Z
A simple UILabel alignment does not help for my specific problem, because the text content is dynamic and not static. So there could be any combination depending on the data I get from the backend. Therefore I was hoping for a UIFont or UILabel attribute that could return the size of the gap based on the current rendering of the text.
I know that there are great UIFont related attributes like baseline, capHeight and ascender one can access to align text but there seems to be no attribute that would return the value of this gap on the left.
If this doesn’t need to be two UILabels, you could have one with attributed text. Then both lines would be subject to the same layout.
I haven't tried the GUI which people have been posting Screen Shots of, however... the behaviour is consistent with the very nature of typography.
The font sizes are completely different, therefore the width of an em is different, subsequently the letter-spacing is also different.
See this: http://www.w3.org/WAI/GL/css2em.htm for more information.
I know this answer doesn't give you what you have asked for, however it should explain why this is happening.
For customisation such as this (if HTML and CSS) I would use a negative / positive margin on the sub text, however seeing as you are using backgrounds use padding (or the equivalent in your GUI).
Use attributed text of UILabel
set indentation as per your requirement
ref screen shot
Select both the labels and add a constraint called align leading edges.
I have 2 UILabels, one which is multi-line and other is of 2 lines. I always have an image besides these labels and its becoming difficult for me to align the image with the text. Eg:
If the 2 line label just has some small text on it, the text is displayed on the middle of the label and the image is aligned with the first line. If it has exactly 2 lines on text on it, then the image is aligned to the first line of text which is exactly what I want.
Is there a way I can get the position or co-ordinates (x,y) of the first line on UILabel so that I can adjust the image constraints accordingly then?
This will give you the co-ordinates and the dimensions:
label.frame.origin.x
label.frame.origin.y
label.frame.size.width
label.frame.size.height