Wrap Text in UITextField? - objective-c

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.

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.

UILabel with more... text

I have a text with multiple lines but at the end I want to add the phrase "more..." if the user press the more... word than the system it suppose to display the rest of the paragraph.
How can I do that?
one way is: use a UILabel to display a short text
put a UIButton under that with "show more" make settings to button to look like a UILabel
catch the action of the button, and remove the button and display a multines text and change the size of the UILabel.
This isn't the right approach for novice programmers, I hope I have helped with an idea.
You could use a UITextView that contains the limited text and add a UITapGestureRecognizer that when tapped, removes the gesture and adds text to the textview.
There is no particularly easy way, built-in way to do it. If you want to implement it yourself, your best bet is to use a UITextView instead of a UILabel. UITextView conforms to the UITextInput protocol, so when you detect a tap on the text view, you can use closestPositionToPoint: and related messages to figure out which part of the string was tapped.
Instead of doing it yourself, you could use TTTAttributedLabel or OHAttributedLabel. Both of these have built-in support for detecting taps on links. I haven't used either, so I can't advise you on which is better. There are probably other free solutions available, but those were easy to find.

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)

How can I programmatically add a text field with a decimal pad keyboard in Xcode 4?

I needed to add a UIKeyboardTypeDecimalPad keyboard to a decimal field, which can only be done programatically. I need the full code (I'm pretty new to Objective-C) to make a UIKeyboardTypeDecimalPad pop up when someone touches inside the text box. I'd also like to add the text field programmatically.
Basically, I need to add a text field with a UIKeyboardTypeDecimalPad keypad. Or, if possible, just the keypad to a text field made in Interface Builder.
Does anyone know a good tutorial for this or can anyone give me the full code themselves, and tell me where I need to put it (the .m or .h or what?)?
Thanks!
Update: As mentioned in the comment below, could I see example code for an app that does this?
To set the keyboard type to UIKeyboardTypeDecimalPad, just assign that value to the text field's keyboardType property.
textField.keyboardType = UIKeyboardTypeDecimalPad;
Adding a text field programmatically is only slightly more work: UITextField *textField = [[[UITextField alloc] initWithFrame:frame] autorelease]; to create it, then set any properties you need to set, and add it to the appropriate view with addSubview:.
In your view controller's viewDidLoad method is a good place for this sort of thing. Or if you're loading a view without a controller from the nib, use the view's awakeFromNib method.

IBOutlet UILabel do not get resized

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.