Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:06:25+00:00 2026-06-12T07:06:25+00:00

I am following the 23th chapter of Aaron’s Cocoa programming for Mac OS X.

  • 0

I am following the 23th chapter of Aaron’s Cocoa programming for Mac OS X.
I have to drag a letter from a view and copy it in another application like text edit.
This is the portion of code with which I find problems:

- (void) mouseDragged:(NSEvent *)theEvent
{
    NSPasteboard* pb=[NSPasteboard pasteboardWithName: NSDragPboard];
    NSPoint down=[mouseDownEvent locationInWindow];
    NSPoint drag=[theEvent locationInWindow],p;
    NSSize size=[string sizeWithAttributes: attributes];
    NSRect imageBounds;
    NSImage* image=[NSImage alloc];
    float distance;
    distance= hypot(down.x-drag.x, down.y-drag.y);
    if(distance<3 || [string length]==0)
    return;
    image=[image initWithSize: size];
    imageBounds.origin=NSZeroPoint;
    imageBounds.size=size;
    [image lockFocus];
    [self drawStringCenteredIn: imageBounds];
    [image unlockFocus];
    p=[self convertPoint: down fromView: nil];
    p.x= p.x - size.width/2;
    p.y= p.y - size.height/2;
    [self writeToPasteboard: pb];
    [self dragImage: image at: p offset: NSZeroSize event: mouseDownEvent pasteboard: pb source: self slideBack: YES];
}

Where:
– mouseDownEvent is an event previously saved, when the user has clicked on the lecter (mouseDown event);
– string is a NSMutableAttributesString, a string of max 1 lected that contains the lecter to be displayed into the view;

When the event occurs there is already a letter displayed on the view (so the string has length 1).If I forgot some important informations please ask.

The problem: the drag and drop operation works fine, but the problem is that while I drag the image, I don’t see the image displacing from it’s original position.
This is what I see while I drag the letter:

enter image description here

This is what I should see instead:

enter image description here

So the letter should displace, but this doesn’t happen.I don’t recognize the cause of this problem, I think that the method drawImageCenteredIn should work fine.That’s the code of this method:

- (void) drawStringCenteredIn: (NSRect) rect
{
    NSSize size=[string sizeWithAttributes: attributes];
    NSPoint origin;
    origin.x= rect.origin.x+ (rect.size.width+size.width)/2;
    origin.y=rect.origin.y + (rect.size.height+size.height)/2;
    [string drawAtPoint: origin withAttributes: attributes];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:06:26+00:00Added an answer on June 12, 2026 at 7:06 am

    It’s simple. The sign is incorrect in these two lines:

      origin.x= rect.origin.x+ (rect.size.width-size.width)/2;
      origin.y= rect.origin.y + (rect.size.height-size.height)/2;
    

    Here is the corrected method:

    - (void) drawStringCenteredIn: (NSRect) rect
    {
        NSSize size=[string sizeWithAttributes: attributes];
        NSPoint origin;
        origin.x= rect.origin.x+ (rect.size.width-size.width)/2;
        origin.y= rect.origin.y + (rect.size.height-size.height)/2;
        [string drawAtPoint: origin withAttributes: attributes];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from another question I have asked I have created a new class
Following is my xml from where I have to get attribute value: <R a=1
Following on from my previous question: Simple inner join in linq How would I
following code is used to find url from a string with php. Here is
Following on from an earlier question I'm trying to get multiple (widgets or in
Following example code from the libpcap documentation yields the following code which should report
following problem: I have a simple paragraph with a lot of text where I
Following the instructions here: http://www.padrinorb.com/guides/padrino-mailer I have the delivery method added on the app.rb
Following this example on another thread, I am able to tweet either the URL:
Following on from this question: Can't I use JQuery inside my FancyZoom popup? The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.