How doesn't UILabel implementation intrinsicContentSize? - ios

As we known, UILabel has intrinsicContentSize, my puzzle is when we pin a label to it's superview with leading and trailing offset 10 for example, and set the numberOfLines to 0, assume the superview is a viewcontroller's view that has a width 320, and how label knows it's width is 300.
Another, when there are mutiple constraint that the label is compressed, and how the label know it's compressed and recalculate it's size?

The intrinsicContentSize is independent of any context, including any constraint applied to the label. This is, actually, the meaning of intrinsic.
So when you ask for the intrinsicContentSize of a label, it does not know that its width is gonna be 300, and indeed it returns any width that fits its content. Go on, check it: ask the the intrinsicContentSize of a label which contains "foo", or "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum vitae sapien nec elit tongue incident." You won't get the same result.
Know you may wonder: if the intrinsicContentSize is (52,21) or (633,21), why on earth does it end at (300, 21)?
Well the answer is simple: the intrinsicContentSize generates implicit constraints whose priorities are controlled by the contentHuggingPriority and contentCompressionResistancePriority of the label. Those are generally less than 1000. On the opposite, the priority of the leading and trailing offsets are 1000, the maximum. So the offset constraints win in the final layout, and the label ends with a 300 width.
Now try to set contentHuggingPriority and contentCompressionResistancePriority to 1000: you will see a conflict. Do you see why?

Related

Graphviz: aligning a graph wrt its label

By default, Graphviz centers the graph relatively to its caption (label):
graph {
label="lorem ipsum dolor sit amet consectetur adipiscing elit"
A -- B
}
How can I align the graph to the left, i.e., make A, B and the initial of "lorem" vertically aligned?
The attributes labeljust and labelloc have other purposes, and I cannot see anything in the reference to achieve this result.
(it helps it include your source as text, not png)
No direct way, but (if needed) use invisible nodes and/or node width to get the graph wide enough to meet your needs. Then use the labeljust attribute to to justify the label to the left (or right) of the entire graph.
graph {
// does not seem to work size="8!"
label="abcdefghijklmnopqrstuvblahblah" labeljust=r
{rank=same a node[style=invis width="2."] x1 x2 edge[style=invis] x1--a--x2}
a--b
}
giving:

Horizontal UIStackView with two label (one multiline label, one one-line)

I have a horizontal UIStackView which has two UILabel in it. First UILabel is multiline (two line) and other one is one line only. They both have default content compression and resistance priorities. My problem is that even there is a gap between labels, "best" word in first text goes second line. I noticed that first label doesn't goes beyond half of total width.
What I want is that second label should always show itself and first label should size It self for remaining space. If It can't fit to one line It should be two line. However, If second label is too short and first label is a long one but both of them can fit, first label should go beyond half of the width.
P.S I need to use UIStackView in this scenario because there are other cases. I know putting two label inside UIView may solve the problem.
UIStackView:
- Distribution: Horizontal
- Alignment: Center
- Spacing: 0
UILabel:
- Number of line: 2
- Line break: Word wrap
UILabel:
- Number of line: 1
View hierarchy:
Desired Result:
OR
EDIT: I calculate the width of second label and give width constraint. I think It solved my problem, I'll test a bit.
//Give specific width to second label to make first one calculate right number of lines.
if let font = UIFont(name: "Metropolis-ExtraBold", size: 15) {
let fontAttributes = [NSAttributedString.Key.font: font]
let size = (secondLabelText as NSString).size(withAttributes: fontAttributes)
secondLabel.widthAnchor.constraint(equalToConstant: size.width).isActive = true
}
To try and simplify...
Forget calculating any widths... what matters is the horizontal Content Hugging and Content Compression Resistance
Leave the left (blue) label at the defaults:
Content Hugging Priority
Horizontal: 251
Content Compression Resistance Priority:
Horizontal: 750
But set the right (orange) label to:
Content Hugging Priority
Horizontal: 1000
Content Compression Resistance Priority:
Horizontal: 1000
Results:
The only other issue would be if the text in the right-side label exceeds the full width of the view -- but you haven't indicated that you might need that much text.
You should set different horizontal content compression resistants for each of them:
For this case, the blue one(multiline) should have something less than the orange one(Singleline).
Assistive note: Multiline: 250, Singleline: 750
Orange Settings:
Blue Settings:
Note that I have set the line limit of the orange one to 0. You can set it to anything you like. But if you like to make it selfsize to anyhight taller view should have higher vertical compression resistant than 250.
First embed your multiline label inside a view and constraint the label to this view (top, leading, trailing, bottom). After that add your view with your multiline label and single line label to your stackView.
Then you can use .fillProportionally for the distribution of your UIStackView and the spacing value to specify the space between your two labels.
Make also sure to use Required (1000) for your horizontal content compression resistance priority on your right number label.
View-hierarchy:
Result:
With spacing between your labels:

Autolayout Constraints Priority

I have 2 UILabel labelA and labelB and a width of 320 px. I created constraints
H:|-[labelA]-(>=4)-[labelB]-|
Unfortunately, labelA and labelB can sometime be big and would not fit without truncation. I would like to make sure that no matter what, labelB will not get truncated. (labelB will not go over 160 px). However, I would like to use the available space for labelA as much as possible so fixing labelB to half is just a waste of space.
Question: How do I put a priority such that labelB always retain its intrinsic size? Where as labelA can get truncated.
You set the content compression resistance to high on label B:
[labelB setContentCompressionResistancePriority: UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
This means that B will try not to be compressed below the intrinsic size given by the text. To make it also be as small as possible, you may also want to set the contentHuggingPriority- this will make it try to match the size of the text.

iOS UILabel text of different sizes dynamically loading?

For example I have fixed width label with 4 word text with a fix font size. but when 4 (small )word sentence "this is my car" and other 4 (large)words sentence "elephant most beautiful animal" with same size font.
so small 4 words sentence will be adjusted in fixed label width but bigger word sentence "elephant most beautiful animal" will cause the problem in that UILabel text
How can I dynamically calculate the size of sentence words and adjust font size of text to be fixed in UIlabel.
please guide...
Probably you're looking for the adjustsFontSizeToFitWidth property.
in interface builder click on the label and set the minimum font size. if it is not in interface builder, do this
[Label setAdjustsLetterSpacingToFitWidth:YES];
[Label setMinimumScaleFactor:2];
[Label setAdjustsFontSizeToFitWidth:YES];
these shrink the font size depending on the length of the sentence. The first tightens the letter spacing and the second scales the font down by 2. The 3rd adjusts the font size.

LaTeX book class: Twosided document with wrong margins

I am trying to write my thesis in latex... Cannot get the layout straight though :?
I'm using the following document class:
\documentclass[11pt,a4paper,twoside,openright]{book}
My problem is: on the odd numbered pages there is a big margin right, and a small margin left - it should be the other way round... (for binding & stuff)
I am a little puzzled by this -- am I just to stupid to see the obvious? The odd page numbers appear on the 'right' page of a bound document, so there needs to be a larger gutter margin on the left for binding -- and vice versa. Right?
Why does LaTeX not behave like this?
Here is the full code to produce a small Tex file that shows my problem:
\documentclass[11pt,a4paper,twoside,openright]{book}
\begin{document}
\chapter{blah}
Lorem ipsum ius et accumsan tractatos, aliquip deterruisset cu usu. Ea soleat eirmod nostrud eum, est ceteros similique ad, at mea tempor petentium. At decore neglegentur quo, ea ius doming dictas facilis, duo ut porro nostrum suavitate.
\end{document}
Edit:
I know about a lot of ways to manually specify the page margins, like
\setlength{\oddsidemargin}{53pt}
or ...
\usepackage[lmargin=1cm,rmargin=2.5cm,tmargin=2.5cm,bmargin=2.5cm]{geometry}
I just wanted to use the default settings and don't understand why they do not behave as expected.
I had the same problem. Anyhow, here the "simple" solution (put it in your preamble):
\let\tmp\oddsidemargin
\let\oddsidemargin\evensidemargin
\let\evensidemargin\tmp
\reversemarginpar
Cheers,
Chris
Edit: Contrary to the previous solutions, the provided code does not change the style of the document :)
No, that's right. The odd pages should have a bigger margin. If you take a twosided book, you have 3 margings: On the left, in the middle, on the right. Every margin should be the same size. The one in the middle is build by two margins from each side. So it has to be smaller.
If you want to add a bindingoffset, you need to use the package geometry:
\usepackage{geometry}
\geometry{bindingoffset=1cm}
or use another class, like the KOMA-classes:
\documentclass[11pt,a4paper,twoside,openright,BCOR=1cm]{scrbook}
The extra space is for the margin notes. In general, to see what's going on with your layout, you can put \usepackage{layout} in your preamble, and then stick \layout in your document to get a diagram and listing of geometry settings.
In your case, as I say, what's going in is the extra space for margin notes. If you don't want it, use \setlength{\marginparwidth}{0pt}.
If you have the time and inclination to learn more about this, and many other things about page layout and document design in general, I believe that studying the memoir package will give you an excellent return on your investment.
In particular, memdesign.pdf gives the underlying principles a thorough going-over, and memman.pdf is a comprehensive (though large!) reference.
In the case of the memoir class.
The margins are correct. There is more space to the right of the odd pages because of margin notes. As #etaoin pointed out, you can see the page layout with \usepackage{layout} and putting \layout within the document environment.
I don't use margin notes, therefore my solution is to reduce the \marginparwidth to 0 or set it to a small value.
You can do that directly by setting the length
\setlength{\marginparwidth}{0pt}
or using the command \setmarginnotes described in the documentation memman.pdf.
\setmarginnotes{0pt}{0pt}{0pt}
However, that didn't worked out for me immediately because the text width remained the same.
My final solution is to set a desired width to the margin note, then whatever width you gained you should add it to the text width. On the odd pages this works fine because the text expands to the right. However, on the even pages the text grows to the right but your free space is to the left.
You have to correct this by decreasing the even side margin as well.
Here's the complete solution:
% the new margin note width will be a fraction of the old one
\newlength{\notewidth}
\setlength{\notewidth}{0.2\marginparwidth}
% deltanotewidth is the space that we gain
\newlength{\deltanotewidth}
\setlength{\deltanotewidth}{\marginparwidth}
\addtolength{\deltanotewidth}{-\notewidth}
% update the note width
\setlength{\marginparwidth}{\notewidth}
% the text width grows
\addtolength{\textwidth}{\deltanotewidth}
% the even side margin reduces to fit the bigger text
\addtolength{\evensidemargin}{-\deltanotewidth}

Resources