uitableviewcell dynamic label width with VFL - ios

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

Related

How to check if an image contains horizontal or vertical text region

I have images with text written vertically in some cases and horizontally in others. I need to process horizontal and vertical text images differently. Hence, for that I need to first check if the image contains a vertical text or not.If someone can suggest some way of finding if an image contains vertical text or not, it will be helpful. For example see the following imageshttps://i.stack.imgur.com/A5txz.jpg and https://i.stack.imgur.com/7LU28.jpg. The first one contains a vertical text and the second one contains a horizontal text.

Label not hugging text properly

I'm struggling with a problem. Some labels are not being calculated correctly in my list.
As you can see in the image below, when the text is too large an ellipsis is added to the end of the label and there is an extra space on the top and bottom of the label. But when the text has only 3 lines, the label size hugs the text properly.
Anyone knows how to fix this?

How to compress the text in a line to fit it?

I have a whole paragraph and I want to fit it in a label and be justified.Also, I want every line to have a specific range of words. I know that there is "Autoshrink" in Xcode, but it does not seem to work because I am trying it in a multiline text.
Here is an example of what I am trying to do http://imgur.com/Kxbs64Q. You can see how the text is fit into the label and is justified. However, not every line has the same number of characters. Is there a way to compress the text inside the label in swift?.
Use a minimumFontSize for your label. It will allow you to fit all of your text in the label. Also, set your label's numberOfLines to 0 if you want label to automatically create multiple lines. Use this link for help.

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.

NSAttributedString alignment to particular text

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"

Resources