NSAttributedString alignment to particular text - ios

I m trying to align the attributed text of UILabel with multiple lines as below.
The alignment should be like all the days in chars left align and timings right align. How can i do this .Please suggest.
Thanks

I would break the string into 2 and align the day to right and the time to left. Otherwise you need to calculate each letters width (and the spacing between the letter before and/or after.) Fonts are complex as the glyph itself and its spacing to neighbours are different from letter to letter.

I hope there is someone who found an elegant way to do this.
I also had very similar task and here is how I did it.
I achieved this by breaking the line into two pieces.(what should be aligned left and what should be aligned right)
Then on your view create two UILable with the equal widths.
After that display left side in Keys Label and right side in Values label
Do not forget set Lines to 0

Left align your text.
textAlignment = NSTextalignmentLeft
Separate each line with "\n" to force a new line
#"Tuesday 9:20\nWednesday 8:40"

Related

How to align two UILabels on the Y axis of the center of the first line of text

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.

uitableviewcell dynamic label width with VFL

I need to fit a label and an image in a table view cell in such a way that the image should be placed immediately next to the label but within the cell visibility. ie., they should look like below,
When the text is smaller or within a line, then the image could be immediate next to the label. Eg -
Smaller text (image)
When the text goes beyond a line, then image could be at the edge of the cell because the label length had already been extended to the length. Eg -
Multiline text multi-line text multi-line
text multi-line text multi-line text multi (image)
multi-line text multi-line text
I am trying to achieve this using VFL and no storyboard or nib.
What I tried out :
1. "H:|-[label]-[image(==20)]-|" , "V:|-[label]-|"
This makes the image to always attached to the edge thus the case 1 fails which needs the image near the label. Eg -
case 1 :
Smaller text (image) (not ok)
case 2 :
Multiline text multi-line text multi-line
text multi-line text multi-line text multi (image) (ok)
multi-line text multi-line text
2. "H:|-[label]-[image(==20)]" , "V:|-[label]-|"
This makes the image near the text thus okay for case 1 whereas that fails for case 2 which does not wrap the text to multiple lines then and the text is extending beyond the cell width. Eg -
case 1 :
Smaller text (image) (ok)
case 2 :
Multiline text multi-line text multi-line text multi...
(not ok - text not wrapped to multiple lines, remaining text and image hidden)
How to resolve this? Setting priorities to some constraints would help ?
I don't have much experience with VFL but , I know Autolayout so I can give you Idea how to resolve
I have seen your both tries
1. "H:|-[label]-[image(==20)]-|" , "V:|-[label]-|"
2. "H:|-[label]-[image(==20)]" , "V:|-[label]-|"
In first try you have fixed image on trailing
on second try you have fixed image with label
What would be combine solution ?
Your clear requirement says that you need your image attached with label trailing also take care about tailing to superview
So clear solution for that problem is
you have to give your imageView Trailing may be this code [image(==20)]-| should be changed with Greater than or equal to relation with 0 Constant
it will tells autolayout engine to keep Min 0 Distance from trailing of superview
Sorry I couldn't give you exact code as I don't have much experience with VFL
Hope it will help to you

How adjust label alignment using autolayout?

I have some labels on my screen.4 Labels can have dynamic value it can display integer number value as well as it can also display some word as START.I have given the following constraints:
Leading to image
Top to image
Now as the text increase there is enough space on left so that label can expand it's width.but as i have given leading to top image so it will not move more to left.So please tell how can i move it to left when text increase.I know i can do it with the help of making outlet of leading constraint and change its value.
But is there any easy way of doing it ?
edit the leading Constraints to "less than equal to" '<=' and trailing fixed '=', whenever the label will have lengthier text its size will increase in the left side and be fixed on the right side

Align two UILabel texts

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.

UILabel Autoshrink Mal-use?

I Need a label in my UI to hold up to 32 characters without distorting, so I enabled auto shrink and changed the number of lines to 0:
heres my label on the IB:
now when I run the application it runs off the screen:
how can I make it fit accordingly?
You don't want the number of lines to be 0, because that will tell the label to use any number of lines that it needs. You also have to constrain the label's right edge to its superview's right edge, because without that, it'll run off the right side, as you can see.
remove constraints first
give the width 0 and apply width constraints . Click on the constraints change width constraint = to >= after which side you want (left or right) apply top space and (trailing space or leading space) apply two constraints.
clear the lable text.
try it and let me know after this wat is th problem.
You are on right direction. Set minimum font size and maximum font size of the label and not need to truncate characters, not need to set number of lines. Autoshrink definitely works.

Resources