IBOutlet UILabel do not get resized - cocoa-touch

HI all,
I have designed a view in Interface Builder and connected correctly with its controller.
There some UILabel in it that are going to be filled with lot of text.
But, even if I have declared "word wrap" and set line to 0 in IB, I can display only first line. I notice that if I increase the height of the UILabel in IB, all the text display properly in multi line, but I cannot do that because text may vary.
Shouldn't the UILabel resize itself to fit all the text ?
Also, if I built the UILabel programmatically I can succesfully accomplish a word wrap, why cannot do the same with IB ?
What am I doing wrong ?
thanks
Leonardo

The only solution I found is to call sizeToFit programmatically on UILabel.
I wonder if there's a setting in IB to emulate this command.

Related

Replacing NSTextField with NSScrollView to get a scrollbar

My OS X app written in Objective-C needs to display a varying NSString* variable theString which can get quite large.
I use Xcode IB to build a nib file which displays theString in a NSTextField* object panel declared inside AppDelegate.h like this:
#property (weak) IBOutlet NSTextField *panel;
Now I can set the contents of panel inside AppDelegate.m like this:
self.panel.stringValue = theString;
This all works fine. But I now want to give my text field a scrollbar. So in place of a "Text Field" I choose a "Text View" from the Object Library, and get its blue line to generate me a new declaration of panel which now looks like this:
#property (weak) IBOutlet NSScrollView *panel;
This now no longer works:
self.panel.stringValue = theString;
raising the error: (!) Property 'stringValue' not found on object of type 'NSScrollView*'
How do I need to fixup this statement?
(Might I just say I find the extensive Apple documentation on the topic byzantine and opaque. Why am I being naive to expect a simple answer to this simple question, as it all seems to imply? I must be missing something obvious -- what is it?)
What you got was a NSTextView wrapped inside a NSScrollView. The scrollView is the thing that makes the scrollbars you want. It basically holds a potentially much larger view inside its viewport and shows only small part of it, that you can shift around with the scroll bars. You need to get (another) reference from your code to the NSTextView inside the scrollView. You can find the NSTextView in the hierarchy in IB and attach to that.
This is in the direction of what you want but I think not quite what you need. The textView is a far more advanced control than a simple textField and probably more than you need. You could instead use a custom view by taking a NSScrollView that comes with a default NSView wrapped inside. Then instead of the NSTextView place your NSTextField on the NSView. The issue with this is that then you need to add some code to auto-resize the NSTextField and NSView based on the content of the textField. Once you got that sorted the scrollView will automatically arrange for the scrollbars that you need.

How to always stick button and label together

I want to have a button and label who always stick together. I want it to be static and I found a solution which uses the UICollectionView with cells. I got the "Connection "" cannot have a prototype object as its destination" and found a solution here: Strange error when adding items to prototype cells in storyboard-IB but the answer given there is talking about a dynamic one. What is the right way to do this? I don't need to have a #property from the cell, I only need to have a #property from the button and label and I just want the label and button to stick together. So actually something like this:
You should make a UIView with a UIButton and a UILabel as its subviews.
If you want to use UITableView or UICollectionView, this parent UIView will actually be the UITableViewCell or UICollectionViewCell.

UILabel UITextField UITextView

What's the fundamental difference between them?
Is a UITextField that's not editable is effectively a UILabel?
Are those essentially the same?
UILabel: "The UILabel class implements a read-only text view."
UITextField: "A UITextField object is a control that displays editable text and sends an action message to a target object when the user presses the return button."
UITextView: "The UITextView class implements the behavior for a scrollable, multiline text region."
So:
labels are read-only
textfields are editable, and provide horizontal character seeking (not really scrolling) when the text is too long to display all at once. Generally used to input short text.
textviews are also editable, but provide vertical scrolling when the text is too long to display all at one.
In addition to the above answers, UITextView has selectable text. This means that you can copy it or get the device to speak the text.
UILabel - used for static text
rendering,
UITextField - is an input
field,
UITextView - is a multiline
input field
Finally Apple have covered this part in this amazing talk:
https://developer.apple.com/videos/play/wwdc2018/221/
The key takeways is as followed:
Also in addition to this: Only with UILabel you can get the Auto shrink feature, otherwise you should implement that (if needed)

iOS Text View Not Updating

I've got a UITextView defined by
#property (nonatomic, retain) IBOutlet UITextView *quote;
in my view controller header, and I can set the value by using
quote.text = #"some text";
but the view doens't want to update the value, what can I do
Setting the text should immediately cause the UITextView to render your text under normal conditions.
Are you sure your that:
The UITextView is placed appropriately in your Nib and is visible?
The UITextView is appropriately linked with your outlet in your file owner (aka view controller)?
A quick test to verify the visibility of your UITextView - just place some sample text in it in the nib and verify that it appears on launch. If so, then you know that at least your view is displaying appropriately. At that point, it would have to be related to #2.
Make sure you connected quote to your UITextField in your XIB. Also, make sure that you #synthesize quote; in your .m.
I just bumped into this too and the problem went away as soon as I specified enough height for the content. In Xcode it may still look all right, but AutoLayout decided to do without the TextView if there was no height-constraint on it.
This was probably not your problem back when you asked the question, but it still turned up fairly early in my google search, therefore I post this answer anyway.
Btw: Xcode is still acting a little skiddish when you edit the constraint. It will update the view (and save) if you hit 'Enter' in the Constant-Field, but it will not do so if it loses focus in some other way.
This just to show us how difficult it is to get user interfaces right all the time.

Wrap Text in UITextField?

Does anyone know how to wrap text in a UITextField? I am using Cocoa/Objective-C in Xcode for my iPhone project, and I can not seem to find a way to do this...
UITextField is meant for single-line text only. If you want multiple lines of text, then you'll have to use the UITextView class instead.
It's worth noting that UITextView inherits from UIScrollView, so if you don't want scrolling, you may want to stick with the UITextField and put up with the text being on one line... I once tried to subclass UITextView to make it like a multiple-line UITextField, but the UIScrollView made the task a nightmare—in the end I just went back to using a simple UITextField.