auto adjusting the positions and height width of UILabel in iOS - objective-c

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

Related

ngx-charts-bar-vertical-2d minimum bar width

I would like to set the width of the bars to be a minimum of 12px. I would actually prefer to have a fixed width on the bars period.
As an example, when I am displaying 10 days of data, the graph looks fine, but for 30 days, it does not look good at all. Any thoughts on making this look better?
I added a horizontal scroll, but the width of the bars is automatically calculated based on the number of items in the graph, irrespective of the graph width. Is there any way to alter this behavior?
Screen capture: https://share.getcloudapp.com/yAubDN8X
Try adding [barPadding]="1" to ngx-charts-bar-vertical-2d element
<ngx-charts-bar-vertical-2d [barPadding]="1" ...>...</ngx-charts-bar-vertical-2d>
Try adding [groupPadding]="1" to ngx-charts-bar-vertical-2d element
<ngx-charts-bar-vertical-2d [groupPadding]="1" ...>...</ngx-charts-bar-vertical-2d>
as long as you minimize the padding between groups, the barWidth with grow automatically !

NSTableRowView "Empty Rows"

I've overridden NSTableRowView, which so far is working a treat. The problem I'm facing is drawing the separator for the empty rows.
drawSeparatorInRect:dirtyRect
only seems to override the rows that have content, all the rest seems to be filled with system colours.
At the moment I have:
-(void)drawSeparatorInRect:(NSRect)dirtyRect
{
NSRect sepRect = self.bounds;
sepRect.origin.y = NSMaxY(sepRect) - 1;
sepRect.size.height = 1;
sepRect = NSIntersectionRect(sepRect, dirtyRect);
if (!NSIsEmptyRect(sepRect))
{
[[NSColor gridColor] set];
NSRectFill(sepRect);
}
}
Which, as I said works well. I originally picked this up from some apple sample.
When I change the separator color to "red", you can see what happens:
The bottom-half of the last populate row is filled, but none of the others there after are.
Has anyone got an idea what I'm missing?
Cheers,
A
There are only row views for rows which have content. Therefore, a custom row view can not affect the drawing of separators for rows past the end of your table's content.
To customize the drawing of those separators, you need to override -[NSTableView drawGridInClipRect:]. You compute the position of the separator for where rows past the end of the content would be and draw the same way as your custom row view does.
This is explained, with example code, in the video for WWDC 2011 Session 120 – View Based NSTableView Basic to Advanced. It's also demonstrated in Apple's HoverTableDemo sample project.

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.

Adding hardcoded value to contentSize

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);

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.