Interface Builder Autolayout and Resizing - objective-c

So I have two questions about Interface Builder for Xcode:
I'm trying to build an interface with a label on the left and a text field on the right, which have constant spacing between them, but I want the text field to expand horizontally when I resize the window horizontally to keep the spacing. I've added a restraint that keeps the spacing between them equal, but when I move the window, it resizes the label box rather than the text field. I tried pinning the width of the label, but then it stops me from resizing the window.
Is there any way to resize multiple items at the same time? Like if I have 8 labels vertically and I want to size them all to each be an 8th of the window space, how can I do that without just eyeballing it? It would be easy if you could highlight all of them and drag one corner to resize them all, but it wont let me do that.

For your first problem you should uncheck AutoLayout from Utilities Panel -> File Inspector and see what happens. Concerning the second you can create how many labels you want with your desired size directly from code as follows :
Example for 10 labels:
for (int i=0; i<30; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:anyFrame];
[yourView addSubView:label];
[label release]; //if not using ARC
}

Related

UILabel gets hidden behind UIImageView

I have made a subclass for UITableViewCell and I am implementing Subtitle TableViewCell with a thumbnail image.
Following are the UITableViewCell contents:
The issue I am facing is when the data loads in TableViewCell, the subtitleLabel text gets hidden upto the height of the imageView. But when I select any Cell, it shows subtitleLabelText completely.
I have added the screenshot of the same for complete reference:
The UIImageView has frame = CGRectMake(0,0,40,40);
Try to give a clearColor background color for the cell title label -
cell.textLabel.backgroundColor = [UIColor clearColor];
It turns out I was using TableViewCell style as subtitle instead of custom. The style settings in subtitle was making the other labels to hide below them. What a silly miss!
In your nib or storyboard file, make sure that the label is below the image view in the list of subview components (it is in the left of the screen). The first subview in that list will be at the lowest level (behind every other subview, if they overlap).
write one line of code
Titlelabel.backgroundcolor = [UIColor ClearColor];
because your label has white background..and Titlelabel height is too large so label is colliding.
Let me know working or not!!!
Happy Coding!!!
What is the frame of Title Label? if its height is more, then also it may possible that it hides your subtitle Label
Here's a great tutorial which helped me when I was trying to do something like you want :
http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/
You can adapt the size of the different components (ImageView, TitleLabel, Subtitle,...)

Resized UIToolbar refuses to keep its UIBarButtonItem aligned correctly

I have a UIToolbar at the top of my view, and it needs to be resized in an animation. The toolbar contains:
A UIBarButtonItem using system item UIBarButtonSystemItemAdd (the '+' button)
A UIBarButtonItem using a custom view (the title)
A UIBarButtonItem using the style UIBarButtonItemStyleBordered (the 'Edit' button)
I am adding these buttons using a spacer between each, which keeps the title in the center:
[toolbar setItems: #[addButton, spacer, titleButton, spacer, editButton] animated:NO];
The toolbar resizes just fine, and the two buttons keep their locations pinned to the outside edges. However, the title button does not stay in the center of the toolbar. Instead, it seems to pin its right edge to the same location, creating a space on its left side. During the animation, this give the impression that it is sliding right.
To be clear, I do want the title bar to keep its same width - I do not want it to expand as the toolbar grows. But I need the title to stay in the center of the toolbar.
Since a UIBarButtonItemis not a UIView, I can't (?) use the autoresizingmask functionality.
How do I keep the title in the center of the toolbar?
Additional Info
This might be because the Add and Edit buttons have different widths - when I add only the title (with a spacer either side), the behavior is correct.
It turns out that I was leaving out a crucial detail. I am manually setting the width of the edit button to 50.
If this is removed, the resizing works as expected. However, I am then unable to control the size of the Edit button, which I like to set to 50 so it matches the one on the navigation bar.
I found a workable solution. Keep the width of the Edit button at 50. Add a fixed separator immediately after the '+' button, and set its width to 18 (50 - the width of the '+' button). This balances the items either side of the title.
I know it's too bad, still you can use custom button as bar button item and use images of Add/Edit button in it. So that you can set the button frames, at any places. So your button won't placed to right most place.
UIButton * addButton = [[UIButton alloc] init];
[addButton setBackgroundImage:<AddimageName> forState:UIControlStateNormal];
UIBarButtonItem * addItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];

Expand a UITextView when clicked?

How would I go about animating open a textviewto expand when clicked? So if I have a UITextView that is of height 30 but when it gains focus have it expand to 100. Then on losing focus have it go back to 30 even if the text is of height 100.
TIA
Look into the UITextViewDelegate methods -textViewDidBeginEditing: and -textViewDidEndEditing:
Interesting you ask this, because I literally just figured out how to do the same thing. Call this method whenever you want to resize your textview:
-(IBAction)resizeFrame {
textView.frame = CGRectMake(x, y, width, height);
[self.view bringSubviewToFront:questions];
}
Line 1 sets the frame of the textView as a rectangle starting at point x,y with whatever width and height you specify.
Line 2 brings this view to the front so that you can actually see it!
If you want to size it back down, call another method with a different name that sets the CGRect back to the textView's original size.

Getting NSTextView to perfectly fit its contents

I have a view that contains a button and a textview. When the button is clicked, the textview's hidden status will change and be shown on the view. Springs and struts have been configured so the textview expands vertically with the view. All this is done in IB
I then insert text into the textview programmatically, but I need the textview to show all its contents without the user needing to scroll.
This is the code I use to calculate the height of the text in the textview:
- (float) getTextViewHeight {
//based on http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html%23//apple_ref/doc/uid/20001809-CJBGBIBB
[textview.textContainer setLineFragmentPadding:0.0];
[textview.layoutManager glyphRangeForTextContainer:textview.textContainer];
return [textview.layoutManager usedRectForTextContainer:self.interactionData.textContainer].size.height;
}
With or without that call to -sizeToFit on the textview, it will either be too big or too small (depending on its contents).
I need to get the height of the textview with all the contents showing so I can adjust the view's size.
I know I could probably use a NSTextField as a label, but I need a NSTextView for its added functionality (specifically using the enclosing scrollview's rulerview).
Does anybody have any suggestions?
NSTextView generally will resize itself if its string over-runs the container width. I think this is because the contained cell has a default behavior for text over-run, called "Line Wrap" or something. My gut feeling is you could just ask the TextView for it's height after it's been loaded and adjust the containing view accordingly, all without needing a layout manager. And obviously make sure the auto-resizing mask is set (oh, you're doing this in IB so no worries there). I could be wrong, and I didn't do any tests... but yeah, you could try it! :P
Her's how I do it and it works well:
// Help text.
NSBundle* mainBundle = [NSBundle mainBundle];
NSString* path = [mainBundle pathForResource: #"category-analysis-help" ofType: #"rtf"];
NSAttributedString* text = [[NSAttributedString alloc] initWithPath: path documentAttributes: NULL];
[helpText setAttributedStringValue: text];
NSRect bounds = [text boundingRectWithSize: NSMakeSize(helpText.bounds.size.width, 0) options: NSStringDrawingUsesLineFragmentOrigin];
helpContentView.frame = NSMakeRect(0, 0, helpText.bounds.size.width + 20, bounds.size.height + 20);
helpContentView is just a container for helpText to add some marging around the text. helpText resizes with its container.
It should be obvious that for the correct height a fixed width is necessary, since the height depends on what fits on the lines.
If you want to omit the scroll view entirely (e.g., make a text view that is attached to another superview and sizes itself to fit its text), you might take a look at NSText. It is, AFAICT, basically a NSTextView without the superview (scroll view parts), and can automagically resize itself.

Resizable UILabel which scrolls across the screen

Right, I'm trying to get a label with text in it (done already obviously), which scrolls across the screen.
The text that is input into the label is done by a UITextField and a UIButton. This updates fine.
But I'm trying to get the UILabel to resize accordingly to the amount of text input, so that the WHOLE lot of text scrolls across the screen.
This is the code I have at the moment for the scrolling label:
[lblMessage setText: txtEnter.text];
CABasicAnimation *scrollText;
scrollText=[CABasicAnimation animationWithKeyPath:#"position.x"];
scrollText.duration = 3.0;
scrollText.repeatCount = 10000;
scrollText.autoreverses = NO;
scrollText.fromValue = [NSNumber numberWithFloat:500];
scrollText.toValue = [NSNumber numberWithFloat:-120.0];
[[lblMessage layer] addAnimation:scrollText forKey:#"scrollTextKey"];
The problem is, sometimes is starts scrolling in the middle of the screen, and sometimes vanishes before it has fully gone acrosss.
It also cuts of text due to the label being one size.. I don't know how to change this.
Thanks in advance.
Dom
I believe something similar to the solution for this question would work for this case.
You could embed the UILabel in a UIScrollView, with the UIScrollView set to the max size of the label that you want to display on the screen at one time. The UIScrollView would need to have its scroll indicators turned off using its showsHorizontalScrollIndicator and showsVerticalScrollIndicator properties. On a change of text, you could do the following:
[lblMessage setText: txtEnter.text];
[lblMessage sizeToFit];
scrollView.contentSize = lblMessage.frame.size;
followed by the panning animation code as described in the above-linked question, with the frame to be panned to being the far right of the UILabel. This would cause the text to scroll at a constant rate across the label. If you want the label to scroll back to the beginning, you could use the UIView setAnimationRepeatAutoreverses: and setAnimationRepeatCount: methods in the beginning of your animation block.
The label resize worked great thanks!
Now, i've put the label into the scrollview and i've got that showing up.. but I'm now not sure of the exact animations to add to that. I tried some of the ones on the link you gave me, but they're not working.
EDIT: Nevermind, I've got it all working now.
CGPointMake(x, x) is what i needed for the contentOffset.