-[NSTextField intrinsicContentSize] always has undefined width - cocoa

I'm pulling hairs here. I have an NSTextField created on a xib that has an intrinsicContentSize adjusted to it's stringValue.
If I create a textField programmatically like so:
_textfield = [[NSTextField alloc] initWithFrame:CGRectZero];
_textfield.translatesAutoresizingMaskIntoConstraints = NO;
_textfield.alignment = NSCenterTextAlignment;
_textfield.drawsBackground = NO;
[_textfield setBordered:NO];
_textfiled.stringValue = #"Test"
It's intrinsicContentSize is always (width=-1, height=16)
I've tried calling invalidateIntrinsicContentSize but to no avail…
How does one get NSTextField's intrinsicContentSize to work?? I must be doing something different than the one from the xib, I just can't figure out what.

OK, finally figured it out…
[_textfield setEditable:NO]
That's it. I guess that with an editable textfield one must have an explicit constraint for the textfield width. Which kind of makes sense, imagine editing a textfield and it would constantly grow horizontally with every keystroke... not an ideal UI.

If you do need an editable textfield, make sure the NSTextField has a width constraint set to the minimum width the view should be.

Related

How to let the width of an NSTextField be adjusted to its content?

I work with Xcode5.
I would like the width of my NSTextField to adapt to its content.
If possible, I would like to do this in InterfaceBuilder and if possible, without subclassing.
EDIT :
I am working with autolayout ON !! (which seems to prevent me from changing the width of my NSTextField)
Try like this:-
connect delegate to your textfield
-(void)controlTextDidChange:(NSNotification *)obj
{
NSString *str=[yourTextField stringValue];
NSCell *cell=[[NSCell alloc]initTextCell:str];
CGFloat sz=[cell cellSize].width;
[yourTextField setFrameSize:NSMakeSize(sz+10.0, 22.0)];
}
In the documentation for a NSTextField there is nothing that shows that a NSTextField can resize depending on its content, so with that I assume it's impossible. For the kicks and giggles, though, I checked the storyboard and there is no property that allows your NSTextField to be resized besides the auto resizing mask, which have nothing to do with the NSTextField text attribute.
Try to set the preferredMaxLayoutWidth for NSTextfield, this value will be used to update your height, I hope it is helpful.
And Reference resources:UILabel sizeToFit doesn't work with autolayout ios6

How to change the color of a NSToolbarItem's label

I'd like to change the color of all of my NSToolbarItem's labels.
I need to set it to white because it suits better to the background color of my NSWindow, but it defaults to black and I haven't found a way to change it neither in Interface Builder nor directly by code (NSToolbarItem implements setLabel, but it just sets the text string).
If possible, I'd like to avoid:
Replacing the whole NSToolbar by a custom NSView. Would feel like reinventing the wheel to me.
Having to create custom NSViews inside NSToolbarItem. It would imply having to leave blank all of its labels and adding the white-colored label inside the custom view.
In case anyone is interested, I solved it by:
Using custom views inside NSToolbarItems containing both a button and a label.
Displaying icon only instead of icon + label in NSToolbar in order to hide the default label.
I had to deal with another problem related to a bug with Interface Builder: the custom view was not showing at all. I was able to fix it thanks to this answer.
You can change it using NSMutableAttributeString.
For example:
-(void) awakeFromNib{
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:self.label];
NSRange titleRange = NSMakeRange(0, title.length);
[title addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:titleRange];
[self setLabel:title]; }
You have to subclass NSBarButtonItem and override the drawRect method for that. Otherwise it will use [NSColor controlTextColor] or [NSColor disabledControlTextColor].

how do i stop NSTextField blocking right clicks for NSTableView rows?

As the title suggest, I've enabled right clicks for my tableview with customized rows. Anywhere there is an NSTextField, it blocks the right click.
is there a userInteractionEnabled equivalent for cocoa like on the iphone?
I though I probably needed to subclass everything in my NSTableCellView subclass, but I just needed to override -(NSView*)hitTest:(NSPoint)aPoint method in my NSTableCellView subclass to return self.
I'm able to right click on text fields within my custom view based table view cell. Here is how I configure it:
NSTextField *tf = [[NSTextField alloc] initWithFrame:NSZeroRect];
self.textField = tf;
self.imageView.autoresizingMask=NSViewWidthSizable;
self.textField.editable=NO;
self.textField.selectable=NO;
self.textField.drawsBackground=NO;
self.textField.bordered=NO;
self.textField.bezeled=NO;
self.textField.target=self;
self.textField.action=#selector(textDidEndEditing:);
[self.textField.cell setLineBreakMode:NSLineBreakByTruncatingMiddle];
Also, make sure you are setting the -menu property of NSTableView and not the cell view to enable to menu. (I don't know if that will make a difference to your issue but it is how I do right clicking in a table view.)

How to disable resizing of the subview of NSSplitView in Interface Builder?

I've created in Interface Builder a NSSplitView with two subviews. I want the left-side view to have fixed width. I've tried to define autosizing rules for both subviews but the left subview still changes width on window resizing (split view fills up a window). May be that caused by NSSplitView's Autoresizes Subviews property? (I can't uncheck it). What can I do?
The best way I found to do this in Interface Builder:
Drop the NSSplitView on the window
Select the Custom View you want fixed
Go up to the Xcode menu and select Editor > Pin > Width
Adjust the Constant in the Attributes Inspector to the size that you want the panel to be fixed at
Of course, you can also add this layout constraint through code as suggested above if you're feeling adventurous.
The behavior that you want required some code that you can do on the NSSplitView's delegate. However, you can have the same result using BWToolKit.
I think it should work with a NSLayoutConstraint, I work at the moment on at :).
EDIT:
Maybe to provide more details on my answer based on the comment hayden. You can define a constraint either by code or in the the IB.
In the IB select your left subview and click on the constraint buttons in the lower right corner defining a width constraints. If you select this new constraint now you can setup the the width an say it should be equal and set the size you like.
The seconed way is to create in code a NSLayoutConstraint object, i do it like this (this is just an example, and define not a fix width).
// define for the view: Constraint and AutoresizingMask option
NSView *view = self.view;
[view setTranslatesAutoresizingMaskIntoConstraints:NO]; // disable AutoresizingMask
NSDictionary *views = NSDictionaryOfVariableBindings(view);
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"[view(>=140,<=220)]" options:0 metrics:nil views:views]];
In general you find documentation to this topic under the term Auto Layout. To use it you have to enable auto layout and this featuer replace the old autosizing functions. (therefore i disable autosizing mask in the code).
This feature is quit new and you can do complex stuff with it but i think I is worth to study.

NSTextView not refreshed properly on scrolling

I have a NSTextView with a sizeable quantity of text. Whenever I scroll however, the view isn't updated properly. There are some artifacts that remain at the top or the bottom of the view. It appears that the view doesn't refresh itself often enough. If I scroll very slowly the view updates correctly though. If I add a border to the view everything works perfectly, borderless view is the one that has a problem. Here's a link to a screenshot:
Thanks
Have you set the setDrawsBackground and copiesOnScroll propertes for either the NSScrollView or the NSClipView?
The first thing I would suggest is turning off the "draws background" property of the NSScrollView:
[myScrollView setDrawsBackground:NO];
Note that this should be set on the NSScrollView, and not on the embedded NSClipView.
The following excerpt from the documentation may be relevant:
If your NSScrollView encloses an NSClipView sending a setDrawsBackground: message with a parameter of NO to the NSScrollView has the added effect of sending the NSClipView a setCopiesOnScroll: message with a parameter of NO. The side effect of sending the setDrawsBackground: message directly to the NSClipView instead would be the appearance of “trails” (vestiges of previous drawing) in the document view as it is scrolled.
Looks like the text field isn't even in the scrolling-area... Are you sure something isnt overlapping it?
I had a similar trouble - artifacts develop when the NSTextView is embedded in another scrollview (ie. a NSTableView).
I actually turned on the setdrawsbackground, and then added a nice color to make it disappear again.
-(void)awakeFromNib{
NSScrollView *scroll = [self enclosingScrollView];
[scroll setBorderType:NSNoBorder];
[scroll setDrawsBackground:YES];
[scroll setBackgroundColor:[NSColor windowBackgroundColor]];
}
This in combination with a scrollWheel event let me use the NSTextView in a NSTableView.
-(void)scrollWheel:(NSEvent *)theEvent{
NSScrollView *scroll = [self enclosingScrollView];
[[scroll superview] scrollWheel:theEvent];
}
I had the same trouble some time ago. I don't remember how I solved it.
Try to place the NSTextView to another view if the superview is a custom view. Just to see what will happen.

Resources