Change Button and Label position during execution - xcode

I'm bit surprised, because I'd like to change a label and a textfield in dynamic during my App execution. I mean based on an image size, I need to change label and textfield position. But, it doesn't work.
I did all the creation in the main window file (ex xib).
Then when I'm clicking on my segment selector I'm doing this (and my field and label are nt moving !?):
CGRect frame = [LHeight frame];
frame.origin.x += 100; // change the location
frame.size.width += 100; // change the size
[LHeight setFrame:frame];
Even this method does't work :
LHeight.frame = CGRectMake(LHeight.frame.origin.x + 100, LHeight.frame.origin.y, LHeight.frame.size.width + 100, LHeight.frame.size.height);
is any parameters to change in the XIB file ?
thanks in advance for your support.
Regards

LHeight.frame = CGRectMake(LHeight.frame.origin.x + 100, LHeight.frame.origin.y, LHeight.frame.size.width + 100, LHeight.frame.size.height);

Related

set height of multi line label in OSX

I am trying to set the height of the multiline label in OSX but when i use sizetofit functionality it cuts the line width and doesn't set the height.What i want is to be able to grow multi line label as its size increases automatically setting the height and the width.This is the code i am working on right now.
self.solutionTextView.stringValue = String(format: "asjdasjdhsjf dasfjashd fjadhsf sfsdjfijijij dsfsd jsj aidjfajs jfsajf isdj sjdaofjaisdjf i dsfisdfjsdjfi sj jasof sjdf ijsdj fisjdsj fjas jsid isdf jsdi fsiajf isjfi jdsjf saof jisadfj isjdfisdj foajdfjsdawei difj jhjsahhjashashhjsahsahjhsahjsahjshajhsahsahjhjsahjsahjhjsahsahjsajhjhashjshjhjsahsjahjsahjsahsahjsahjshahsjahjsahjsahjshhsjahjsahjsahjsahjhjsahjsahsahhsahjsahsahhsajhjsahjsahjhsjahjsahjsahjhsjahjahjsahjshjahjsahjhjahjsahjhsjahjshjshajhjsahjsahjshjahjsahjsahjshjahjsahjshjahjsahjsahsahhjsahjsahjsahjsahjhjsahjahjsahhsahjjasjhhjsahjsahhjsahjhjsahjsajhhjsahjhjsahjsahjhjsahjshjahjsahjsahjjhashjshajhahjahjhsahhajshjashahjshhjahj%#", self.recommendation.valueForKey("solution") as String)
self.solutionTextView.sizeToFit()
self.linksTextView.stringValue = String(format: "First Link %#", self.recommendation.valueForKey("tip1") as String)
// self.linksTextView.sizeToFit()
self.proTipTextView.stringValue = String(format: "Pro Tip: Not available")
I am developing on iOS and although your variable is named 'self.solutionTextView' I assume we are talking about the equivalent of a UILabel. In this case you could measure the text and use this as an frame:
let frame = self.solutionTextView.frame
frame.size.height = NSString(string:self.solutionTextView.stringValue).sizeWithAttributes([NSFontAttributeName : self.solutionTextView.font]).height
self.solutionTextView.frame = frame
I also assume you have a set width and are more interested in scaling the label vertically in height. If not, the method above returns a size which you can use to get the width.

Creating UI element won't let change position

I tried this:
#IBAction func test(sender : AnyObject){
let height:CGFloat = 44
var tableFrame:CGRect = tableView.frame;
var fieldFrame = CGRect()
fieldFrame.origin = tableFrame.origin
fieldFrame.size.height = height
fieldFrame.size.width = tableFrame.size.width
var textField = UITextField(frame: fieldFrame)
textField.backgroundColor = UIColor(white: 0, alpha: 1)
view.addSubview(textField)
tableFrame.size.height = tableFrame.size.height - height
tableFrame.origin.y = tableFrame.origin.y + height
tableView.frame = tableFrame
}
When running, black field appears, but table won't move nor change size. Removing line
view.addSubview(textField)
allows table to change size and move, but, obviously, no field appears. What is the problem?
your textfield is actually on top of tableview. Your touch actions are probably going to the text fieldview and not to the tableview

expanding a text view when user clicks on "more..."

There are applications with a text field that shows a given amount of lines and when the user clicks more it expands and shows the rest,
I'm familiar with the code to expand my UITextView but not sure how to add the "more..." and then "less...",
option to
Fine tuning the question: I'm not looking to add a button but having the default IOS behavior when the text is larger than the size of a control it will either place ... at the end or in the middle for example: this is ... or thi...long
you can take one button and add that UIButton to view below the UITextView and when you click on that button change the button positions and title related to the UITextView.
You can implement that using the Method (sizeWithFont:constrainedToSize:lineBreakMode:)
Firstly take out the size of the text you need show into the TextField like this
NSString *textToBeShown = #"jhfsdg djsgf jdsfsdf dsf";
CGSize textSize = [textToBeShown sizeWithFont:[UIFont fontWithName:#"Arial-BoldMT" size:11]]
constrainedToSize:CGSizeMake(constrainedWidth,constrainedHeight)
lineBreakMode:NSLineBreakByWordWrapping];
Suppose you want your textview height to be 100.0 initially before pressing "ShowMore" button then you can check the textSize.height in comparison to the 100.0 , if the textHeight is less then 100.0 then assign the TextView's Heigth as textSize.height else assign it 100.0 pixel .
Now when the show more button is clicked assign the textSize.height as the height of the UITextView used to show the text .
NSString *textToBeShown = #"jhfsdg djsgf jdsfsdf dsf";
CGSize textSize = [textToBeShown sizeWithFont:[UIFont fontWithName:#"Arial-BoldMT" size:11]]
constrainedToSize:CGSizeMake(constrainedWidth,constrainedHeight)
lineBreakMode:NSLineBreakByWordWrapping];
textView.frame=CGRectMale(textView.frame.origin.x,
textView.frame.origin.y,
textSize.width,
textSize.height);

Which API can be used to _capture_ the mouse when moving OS X "Carbon" windows?

On request I have implemented support for moving an OS X window by dragging it using an area within the content part of the window, i.e replicating the drag and move functionality of the title bar but in another area.
The problem I have yet to resolve is the fact that if the user drags the mouse quickly it can leave the window area and then no more mouse move events are received.
On windows this type of problem can simply be fixed by calling the win32 method SetCapture(), what's the corresponding OSX method?
This application is a cross platform C++ application using Carbon for the OS X specific parts. (And yes, I know all about the Cocoa benefits but this is an older code base and there no time nor money for a Cocoa port at this point in time.)
I have found Carbon API methods like for example TrackMouseLocation() but can't really see how I could use them for this application. In listing 2-7 here http://developer.apple.com/legacy/mac/library/documentation/Carbon/Conceptual/Carbon_Event_Manager/Tasks/CarbonEventsTasks.html
the mouse is captured but the problem is that TrackMouseLocation() blocks waiting for input. Blocking is something this application can not do since it also host a flash player that must be called many times per second.
The protototype I have assembled when trying to figure this out basically looks like this:
switch(GetEventKind(inEvent))
{
case kEventMouseDown:
// A silly test to make parts of the window border "draggable"
dragging = local_loc.v < 25 || local_loc.h < 25;
last_screen_loc = screen_loc;
break;
case kEventMouseUp:
dragging = false;
break;
case kEventMouseMoved:
break;
case kEventMouseDragged:
if (dragging) {
Rect rect;
GetWindowBounds (windowRef, kWindowContentRgn, &rect);
int dx = screen_loc.h - last_screen_loc.h;
int dy = screen_loc.v - last_screen_loc.v;
rect.left += dx;
rect.right += dx;
rect.top += dy;
rect.bottom += dy;
SetWindowBounds (windowRef, kWindowContentRgn, &rect);
}
last_screen_loc = screen_loc;
break;
Any ideas appreciated?
I feel you should track mouse in Window as well as out of window. Following code should solve your problem,
EventHandlerRef m_ApplicationMouseDragEventHandlerRef;
EventHandlerRef m_MonitorMouseDragEventHandlerRef;
{
OSStatus ErrStatus;
static const EventTypeSpec kMouseDragEvents[] =
{
{ kEventClassMouse, kEventMouseDragged }
};
ErrStatus = InstallEventHandler(GetEventMonitorTarget(), NewEventHandlerUPP(MouseHasDragged), GetEventTypeCount(kMouseDragEvents), kMouseDragEvents, this, &m_MonitorMouseDragEventHandlerRef);
ErrStatus = InstallApplicationEventHandler(NewEventHandlerUPP(MouseHasDragged), GetEventTypeCount(kMouseDragEvents), kMouseDragEvents, this, &m_ApplicationMouseDragEventHandlerRef);
return true;
}
//implement these functions
OSStatus MouseHasDragged(EventHandlerCallRef inCaller, EventRef inEvent, void *pUserData){}
Hope it helps!!
I Hope It Help´s you too:
// Get Mouse Position --> WAY 1
printf("Get Mouse Position Way 1\n");
HICoordinateSpace space = 2;
HIGetMousePosition(space, NULL, &point);
printf("Mouse Position: %.2f %.2f \n", point.x, point.y);
// Get Mouse Position --> WAY 2
printf("Get Mouse Position Way 2\n");
CGEventRef ourEvent = CGEventCreate(NULL);
point = CGEventGetLocation(ourEvent);
printf("Mouse Position: %.2f, y = %.2f \n", (float)point.x, (float)point.y);
I´m looking for the way to get a WindowPart Reference at a certain location (over all windows of all aplications)
Certain methods in Carbon doesn´t work, always return 0 as a windowRef... Any ideas?
You could also try just calling DragWindow in response to a click in your window's content area. I don't think you need to implement the dragging yourself.

How to draw a window to a memory device context?

In my app the user double-clicks on a table row to open a dialog window. The problem is that the window is instantly displayed - I want to make the window appear to 'jump out' of the table row (in the same way that windows 'jump out' from the task bar). To do this I need to draw the dialog window to a memory device context - how can I do this without first drawing it to the screen?
Thanks
------------------ Edit ----------------------
#bubbafat:
Thanks - yes I will need to use CreateCompatibleDC, then set the size of the memory DC to the size of the dialog window. But then I need to draw a 'picture' of the window to the memory device pixels. This will then allow me to draw each frame of the 'pop-up' animation - ie:
Frame 1: Copy the image from the
buffer to the screen at 10% normal
size and 10% opacity
Frame 2: Copy
the image from the buffer to the
screen at 20% normal size and 20%
opacity
etc
If you're trying to draw the window into a memory DC, consider sending it the WM_PRINT message. Assuming your window procedure doesn't do anything especially strange in the normal case, it should render everything into your DC. The animation can be taken from there.
The DrawAnimatedRects function is what does that "jumping out" for the taskbar - there's no need to render the window animation yourself.
Edit: Except that doesn't work on Vista. Here's some equivalent code:
// DrawAnimatedRects(wnd->GetSafeHwnd(), IDANI_CAPTION, animateFrom, &rect);
const DWORD MILLIs = 500;
DWORD startTime = GetTickCount();
DWORD now = startTime;
CRect offset(rect.left - animateFrom->left, rect.top - animateFrom->top,
rect.right - animateFrom->right, rect.bottom - animateFrom->bottom);
wnd->Invalidate();
while (now - MILLIs < startTime)
{
int fraction100 = (int) (((now - startTime) * 100) / MILLIs);
CRect step(animateFrom->left + (fraction100 * offset.left) / 100,
animateFrom->top + (fraction100 * offset.top) / 100,
animateFrom->right + (fraction100 * offset.right) / 100,
animateFrom->bottom + (fraction100 * offset.bottom) / 100);
wnd->SetWindowPos(0, step.left, step.top,
step.right - step.left, step.bottom - step.top,
SWP_NOZORDER);
wnd->ShowWindow(SW_SHOWNORMAL);
Sleep(5);
now = GetTickCount();
}
It sounds like your question is "how do I create a memory device context that is compatible with my screen so that I can draw to it?" In that case the answer is to use CreateCompatibleDC. If that is not your question then please provide additional information so that it is clearer where exactly in the process you are having problems.

Resources