NSSplitView programmatically Change Size - cocoa

I was wondering if there's a way you can programatically change the position of the splitter.

You can change the position of the divider using setPosition:ofDividerAtIndex:

Thanks for the quick reply...
And for interest sake, I found this on the way (might be useful for others)
Thanks...And for interest sake, I found this on the way (might be useful for others) which I found here: on Github
- (CGFloat)positionOfDividerAtIndex:(NSInteger)dividerIndex {
while (dividerIndex >= 0 && [self isSubviewCollapsed:[[self subviews] objectAtIndex:dividerIndex]])
dividerIndex--;
if (dividerIndex < 0)
return 0.0f;
NSRect priorViewFrame = [[[self subviews] objectAtIndex:dividerIndex] frame];
return [self isVertical] ? NSMaxX(priorViewFrame) : NSMaxY(priorViewFrame);
}
I personally used -(float), so use whichever "floats" your boat :D

Related

Dragging caret in NSTextView doesn't match result of characterIndexForPoint

I'm dragging onto an NSTextView (from a table in another window) and as I drag over the text in the NSTextView a caret is displayed showing the insertion point. There appears to be no way to get the location of this caret and I'm using:
NSPoint mouseLocation = [sender draggingLocation];
NSPoint localMouseLocation = [self convertPoint:mouseLocation fromView:nil];
CGFloat fraction = 0.4;
NSUInteger dropLocation = [[self layoutManager] characterIndexForPoint:localMouseLocation inTextContainer:[self textContainer] fractionOfDistanceBetweenInsertionPoints:&fraction];
The problem is that sometimes the inserted text is being inserted one character to the left of the displayed caret. I've tried different values for 'fractionOfDistanceBetweenInsertionPoints' without success.
Q1. Is there a way to get the position of the dragging caret?
Q2. What 'fractionOfDistanceBetweenInsertionPoints' value is Apple using?
Q3. Should I be using an alternative approach (drawInsertionPointInRect perhaps)?
Thanks.
Ok, the solution is to use characterIndexForInsertionAtPoint instead of characterIndexForPoint. Thusly,
NSPoint mouseLocation = [sender draggingLocation];
NSUInteger dropLocation = [self characterIndexForInsertionAtPoint:[self convertPoint:mouseLocation fromView:nil]];

Is it possible to animate a NSTextField

I´m trying a pretty simple thing for testing and can´t get it to work, sorry.
What I want to achieve is that the changes in the textfield label are seen on the screen as well as the color changes.
I already tried several things without success and I´m pretty sure I haven´t got the clue yet.
Any ideas what I´m doing wrong?
I have this right now:
- (IBAction) writeTextToLabel:(id)sender
{
NSAnimation *animation;
[animation setDuration:10];
int n = 100;
NSString *string1 = #"";
[animation startAnimation];
for (int i = 1 ; n >= i ; i++) {
[label setTextColor:[self ccRandom:1.0 :0.0]];
string1 = [NSString stringWithFormat:#"Hello World %.3i\n",i];
[label setStringValue:string1];
}
[animation stopAnimation];
}
You can use AnimTextFieldUsingNSAnimationContext by ipmcc
This code makes fadeIn/fadeOut effect of changing text.
But you can add changing color code after [self setStringValue: aString]; to change your color.
Don't forget to include Quartz.framework to your project.

How to move to the right the label Core Plot?

I use the following method to display the labels for my plot:
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index{
...
CPTTextLayer *label=[[CPTTextLayer alloc] initWithText:stringValue style:textStyle];
}
which for every index should return the label
I know that it's possible to move label up or down using:
plot.labelOffset=10;
The question is: how can i move the label a bit to the right?
I tried to use
label.paddingLeft=50.0f;
but it doesn't work.
Adding padding as in your example does work, but maybe not in the way you expect. Scatter and bar plots will center the label above each data point (with a positive offset). The padding makes the whole label wider so when centered, the test appears off to the side. It's hard to control, especially if the label texts are different lengths.
There is an outstanding issue to address this (issue 266). No guarantees when it will be fixed, but it is something we're looking at.
I ran into the same problem and came up with a different solution.
What I decided to do was to create the label using the CPTAxisLabel method initWithContentLayer:
CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:labelStr style:axisTextStyle];
CGSize textSize = [textLayer sizeThatFits];
// Calculate the padding needed to center the label under the plot record.
textLayer.paddingLeft = barCenterLeftOffset - textSize.width/2.0;
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithContentLayer:textLayer];
Here barCenterLeftOffset is the offset of the center of the plot record.
I wrote an article about this:
http://finalize.com/2014/09/18/horizontal-label-positioning-in-core-plot-and-other-miscellaneous-topics/
A demo project I created that uses this solution can be found at:
https://github.com/scottcarter/algorithms
You can subclass CPTTextLayer and include an offset.
#interface WPTextLayer : CPTTextLayer
#property (nonatomic) CGPoint offset;
#end
#implementation WPTextLayer
-(void)setPosition:(CGPoint)position
{
CGPoint p = CGPointMake(position.x + self.offset.x, position.y + self.offset.y);
[super setPosition:p];
}
Then Use:
WPTextLayer *tLayer = [[WPTextLayer alloc] initWithText:#"blah" style:textStyle];
tLayer.offset = CGPointMake(3, -3);
return tLayer;
There may be consequences of this that I'm not aware of, but it seems to be working so far.

Flipped NSView mouse coordinates

I have a subclass of NSView that re-implements a number of the mouse event functions. For instance in mouseDown to get the point from the NSEvent I use:
NSEvent *theEvent; // <- argument to function
NSPoint p = [theEvent locationInWindow];
p = [self convertPoint:p fromView:nil];
However the coordinates seem to be flipped, (0, 0) is in the bottom left of the window?
EDIT: I have already overridden the isFlipped method to return TRUE, but it has only affected drawing. Sorry, can't believe I forgot to put that straight away.
What do you mean by flipped? Mac uses a LLO (lower-left-origin) coordinate system for everything.
EDIT I can't reproduce this with a simple project. I created a single NSView implemented like this:
#implementation FlipView
- (BOOL)isFlipped {
return YES;
}
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint p = [theEvent locationInWindow];
p = [self convertPoint:p fromView:nil];
NSLog(#"%#", NSStringFromPoint(p));
}
#end
I received the coordinates I would expect. Removing the isFlipped switched the orientation as expected. Do you have a simple project that demonstrates your problmem?
I found this so obnoxious - until one day I just sat down and refused to get up until I had something that worked perfectly . Here it is.. called via...
-(void) mouseDown:(NSEvent *)click{
NSPoint mD = [NSScreen wtfIsTheMouse:click
relativeToView:self];
}
invokes a Category on NSScreen....
#implementation NSScreen (FlippingOut)
+ (NSPoint)wtfIsTheMouse:(NSEvent*)anyEevent
relativeToView:(NSView *)view {
NSScreen *now = [NSScreen currentScreenForMouseLocation];
return [now flipPoint:[now convertToScreenFromLocalPoint:event.locationInWindow relativeToView:view]];
}
- (NSPoint)flipPoint:(NSPoint)aPoint {
return (NSPoint) { aPoint.x,
self.frame.size.height - aPoint.y };
}
- (NSPoint)convertToScreenFromLocalPoint:(NSPoint)point
relativeToView:(NSView *)view {
NSPoint winP, scrnP, flipScrnP;
if(self) {
winP = [view convertPoint:point toView:nil];
scrnP = [[view window] convertBaseToScreen:winP];
flipScrnP = [self flipPoint:scrnP];
flipScrnP.y += [self frame].origin.y;
return flipScrnP;
} return NSZeroPoint;
}
#end
Hope this can prevent just one minor freakout.. somewhere, someday. For the children, damnit. I beg of you.. for the children.
This code worked for me:
NSPoint location = [self convertPoint:theEvent.locationInWindow fromView:nil];
location.y = self.frame.size.height - location.y;
This isn't "flipped", necessarily, that's just how Quartz does coordinates. An excerpt from the documentation on Quartz 2D:
A point in user space is represented by a coordinate pair (x,y), where x represents the location along the horizontal axis (left and right) and y represents the vertical axis (up and down). The origin of the user coordinate space is the point (0,0). The origin is located at the lower-left corner of the page, as shown in Figure 1-4. In the default coordinate system for Quartz, the x-axis increases as it moves from the left toward the right of the page. The y-axis increases in value as it moves from the bottom toward the top of the page.
I'm not sure what your question is, though. Are you looking for a way to get the "flipped" coordinates? If so, you can subclass your NSView, overriding the -(BOOL)isFlipped method to return YES.

Problem with setContentBorderThickness:forEdge: not actually setting the value

I'm trying to use setContentBorderThickness:forEdge: to create a bottom bar in a Cocoa application.
mipadi was on to something, but in testing it out, I think maybe this is a slightly different problem:
-(void) adjustContentBorderBasedOnArrayControllerSelection{
if(([[self.resultsArrayController selectionIndexes] count] == 0)){
[[self window] setContentBorderThickness:40.0f forEdge:CGRectMinYEdge];
NSLog(#"%f", [[self window] contentBorderThicknessForEdge:CGRectMinYEdge]);
} else {
[[self window] setContentBorderThickness:60.0f forEdge:CGRectMinYEdge];
NSLog(#"%f", [[self window] contentBorderThicknessForEdge:CGRectMinYEdge]);
}
}
Both of those NSLog() messages show the thickness value is 0.0 even after I explicitly set it. Anyone know what's up with that?
You can use CGRectMinYEdge. (On 64-bit systems, NSMinYEdge is #define'd as CGRectMinYEdge anyway).
By any chance have you checked to make sure [self window] isn't nil? Do you have your outlet setup right? I get that behavior if the outlet to the window isn't set.

Resources