Replacing NSTextField with NSScrollView to get a scrollbar - objective-c

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.

Related

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.

Setting the position/width of an NSSlider inside an NSMenuItem

I've got an NSMenu attached to an NSStatusItem, and inside that NSMenu I have a NSMenuItem for which I've set the view property to an NSSlider object. That works (it got me what I was after) almost.
The result looks like this:
NSSlider in an NSMenu as an NSMenuItem.view http://dl.dropbox.com/u/91596/Screenshots/k8tl.png
The problem is that I'd like the slider to line up with the menu items above it, so it doesn't look so friggin terrible.
I can't find anything in the apple documentation that says anything about being able to set the position of an NSSlider so I assume I'm going to have to wing it by drawing it myself.
Is that a correct assumption? If so, what are some class references/suggested reading links for achieving that result? Or, am I going about this wrong?
While I'm at it - I had to drag the slider in the UI builder to the desired width I wanted - is there any way to programmatically set the width of the slider so it fills up the desired space inside the menu?
Thanks, sorry of these are noob questions - I'm pretty new to ObjC programming.
First make your own NSView. Next, put your NSSlider in that view. If you need special spacing, adjust the slider in the container view. Or, maybe, twiddle in Interface Builder.

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.

Add a second image to a custom UIButton

I've got an Custom UIButton. It's got
a "static" background image
a variable text (the Title) which gets set in the code
Now I would like to add an icon (UIImage/UIImageView) inside the button on the left of the text. (I use the indent to move the text slightly to the right). Is there an easy way of adding that icon (and referencing it from code, so I can change it) or would you recommend creating a completely new button e.g. based on a UIView? (e.g. a view, that responds to touches)?
I'm just trying to get a feel for what the best approach would be for this. Any experience?
Two ways:
I prefer doing this by subclassing a UIView and (as you mention) implement the UITouch-responder methods. You're also able to override drawRect: what I really like because it makes your app soooooo much faster!
Since UIButton is a subclass of UIView, you can add a UIImageView to your button by using addSubview:. If you save it in your main-class (#property (...) UIButton *button) you can always access it by calling [self button]. You can also subclass your UIButton and add this #property. That's up to you.
It's totally up to you. However I prefer the first way!

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.