Adding hardcoded value to contentSize - objective-c

I've been trying to make my scrollview correctly fit it's content which includes a label, UIImageView, and textview. The textview sizes dynamically to different text, so I've made the scrollview's content size the same as the textview's:
sview.contentSize = (tview.contentSize);
That worked as expected, so my scrollview is close to the size I want, but I need to just add a static 180 pixels or so to make up for the label and image, which don't change size. I would guess it's an easy 1 or 2 lines of code but I can't figure it out. I've tried:
sview.contentSize += 180;
and
CGRect extra;
extra.size.height = 180;
sview.contentSize += extra.size;
and several other combinations to try to make it work but I keep getting errors like Lvalue required as left operand of assignment, or invalid operands to binary +. I'm sure I'm missing an easy solution, thanks for any help.

sview.contentSize = CGSizeMake(sview.contentSize.width, sview.contentSize.height+180);

Related

Why does NSTextField with usesSingleLineMode set to YES has intrinsic content size of multiline text?

I want to draw single line textfield with default border.
So I entered some multiline text into it(just for test for future cases) and set the flags
Swift:
textfield.usesSingleLineMode = true
textfield.maximumNumberOfLines = 1
Objective-C:
self.textfield.usesSingleLineMode = YES;
self.textfield.maximumNumberOfLines = 1;
But I got in console:
(lldb) po self.textfield.intrinsicContentSize
(width = 511.5, height = 174)
I've tested all lineBreakModes, including NSLineBreakByTruncatingTail.
Here is a field in IB:
Of course I can:
Overload intrinsicContentSize method
Filter strings keeping only first line
But it looks like a crutch =( Do you have any ideas how to fix it without crutches? Do you think it is an AppKit bug or my mistake?
PS In any case, thanks for attention.
This is not a bug. By default the NSTextField is trying to display as much text possible. I made a sample application, that contains a long line of text in the title of the NSTextField. Set it to "Uses Single Line Mode" and set line break to "Truncate Tail". I put leading, top and trailing layout constraints on the view. Now when i input the text into the text field, it will make the horizontal width of the window very wide. This is Autolayout in action.
To fix this you need to select the NSTextField, choose the Size inspector and set the Horizontal Content Compression Resistance Priority to a lower number. In the sample application I changed it from 750 to low (250).
Resize content view to an acceptable size, and you are off to the races.

Two NSTextFields with interdependent widths in autolayout

I’m trying to put together what seems to be a simple case of two NSTextFields with dynamic width and fixed spacing in between. I cannot figure out an effective way to do so though.
I’m looking to get something like this:
The blue boxes are the NSTextFields. When more text is entered into one, it should grow and thus make the other one shrink, maintaining the lead space, trailing space and the spacing in between the fields. The first one should take the priority if both of the fields have too much text. Each field will also clearly have a maximum and a minimum possible width it can reach.
How would I go around handling this, preferably utilising IB autolayout as much as possible?
It seems to me that all of constraints you mentioned directly translate into interface builder --
First view has width >= something.
First view has width <= something
Same for Second view.
Space between views is fixed.
Second view wants to be as small as possible (have its width at 0) but this has lower lower priority than the previous constraints and lower priority than inner content size constraints.
The code I had to add to my view controller, after applying the constraints as per the ilya’s answer:
In controlTextDidChange (_controlWidthConstraint refers to the fixed width constraint of the input; it’s probably 0 by default for the second input):
// Get the new width that fits
float oldWidth = textControl.frame.size.width;
[input sizeToFit];
float controlWidth = textControl.frame.size.width;
// Don’t let the sizeToFit method modify the frame though
NSRect controlRect = textControl.frame;
controlRect.size.width = oldWidth;
textControl.frame = controlRect;
_controlWidthConstraint.constant = controlWidth;
The key lies in invalidating the intrinsicContentSize for the text field when text is input.
You can check a sample project here, to get you on the right track.

Proper way of assigning frame

I am developing an app, in which when user clicks on button a view will display a view below that, view height depend upon some criteria.It will be some x height or y height, no other height.Now my question is, what is the correct way of assigning height to that view, should I hard code it depend upon criteria or every-time I should calculate height based on the inside view placed.If I calculate height every-time then for now it is useless because we know what should be the height but in future it may happen that the UI changes, so the height may be changed.
I want to know what you guys think, which the correct way?
Thanks in advance!
I would use hardcoded values as long as the views layout does not change. Even if if will change later I would first use the hardcoded values and then when it is really needed I would change the implementation to a dynamic one. The reason behind my logic is that until it is really needed conditions can change very quickly and therefore even if you implement the a dynamic implementation now you can not be sure that you do not need to change it again when it is really needed because of a specification change until then e.g.
I do a mixture of hard coded and calculated:
To calculate the position of a view i define hard coded insets:
CGFloat topInset = 5;
CGFloat bottomInset = 5;
CGFloat leftInset = 5;
CGFloat interButtonDistance = 10;
Then I calulate the positions of sub view frames by:
CGFloat startX = 0;
CGFloat startY = 0;
CGFloat buttonSize = 50;
CGFloat buttonX = leftInset + column * (buttonSize + interButtonDistance);
Further I do not use a hard coded screen size, I read them
CGFloat height = self.view.frame.size.height; // or similar
Then I could position a button on the bottom of the view by
buttonY = height - buttonHeight - bottomInset;
Using this technic you are a bit more indepenedent from iphone / ipad screen height.
There still will be the need to adapt values when using in an devicxe with dieffernet screen size (iphone 5), but this is then easier, hopefully yus simply have to adapt topInset and boittomInset, and the rest works out of the box (hopefully)

auto adjusting the positions and height width of UILabel in iOS

I have designed my help page for my application,it contains some FAQs..so I have used UILabels for question and answer,I have adjusted,x,y,width and height exactly in the xib,leaving a gap between a question and answer and answer and next question.so totally I have some 10 question and answers,I have put them inside a scroll view.
But now, suppose the text changes dynamically, how can I adjust the positions and width and height,is a problem which I am not able to figure out, by using the below code,it adjusts the height and width of a particular label,if the text increases
NSString * test=#" It may come as a rude shock, but Facebook users should not feel surprised if tomorrow they come across their photos existing in the web world despite having deleted them personally long ago.";
m_testLabel.text = test;
m_testLabel.numberOfLines = 0; //will wrap text in new line
[m_testLabel sizeToFit];
but then, my next label will clash with it..is their any way so that all the labels get adjusted according if the width of any label increases or decreases?
Hope you all have understood my question..
Did you look into the below function, pass the font you have used and the size to which u want to constraint the label into.
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size

Zedgraph textobj X location depends on text length?

I have a Zedgraph textobj which I want to place always in the same x, y position (ASP.NET image). I noticed that the text doesn't always show in the same starting x position. It shifts depending on the text's length. I tried to have the text to have the same length by padding it with spaces. It helped a little but the result is not always consistent. I am using PaneFraction for coordType.
What's the proper method to have a piece of text to always show in the same x position. I am using textobj as a title because the native title property always shows up centered and I need my title be left aligned to the graph.
No, it does not depend on text lenght, however...
It depends on various other things:
Horizontal and vertical align of the text box (see: Location )
Current size of the pane. The font size is scaled dynamically to fit the changing size of the chart.
Counting proper positions to have TextObj (or any other object) always at the same place is quite hard. So you need avoid as much as you can any numbers/fractions in your location coordinates. ZedGraph sometimes calculates the true position in quite odd way then.
You haven't provided any code, so it's hard to tell if and where you made the mistake (if any). But, if I were you, I would do something like that:
TextObj fakeTitle = new TextObj("some title\n ", 0.0, 0.0); // I'm using \n to have additional line - this would give me some space, margin.
fakeTitle.Location.CoordinateFrame = CoordType.ChartFraction;
fakeTitle.Location.AlignH = AlignH.Left; // Left align - that's what you need
fakeTitle.Location.AlignV = AlignV.Bottom; // Bottom - it means, that left bottom corner of your object would be located at the left top corner of the chart (point (0,0))
fakeTitle.FontSpec.Border.IsVisible = false; // Disable the border
fakeTitle.FontSpec.Fill.IsVisible = false; // ... and the fill. You don't need it.
zg1.MasterPane[0].GraphObjList.Add(fakeTitle);
I'm using ChartFraction coordinates instead of PaneFraction (as drharris suggests) coordinates to have the title nicely aligned with the left border of the chart. Otherwise it would be flushed totally to the left side (no margin etc...) - it looks better this way.
But make sure you didn't set too big font size - it could be clipped at the top
Are you using this constructor?
TextObj(text, x, y, coordType, alignH, alignV)
If not, then be sure you're setting alignH to AlignH.Left and alignV to AlignV.Top. Then X and Y should be 0, 0. PaneFraction for the coordType should be the correct option here, unless I'm missing your intent.
Alternatively, you can simply download Zedgraph code, edit it to Left-align the title (or even better, provide an option for this, which should have been done originally), and then use it in production. Beauty of open source.