• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: (freely) Rotating Text
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: (freely) Rotating Text


  • Subject: Re: (freely) Rotating Text
  • From: Lindsey Spratt <email@hidden>
  • Date: Mon, 2 May 2005 12:29:22 -0400

I think you might just use an NSAffineTransform to do the rotation. I created a little test project that does this, RotateTextTest. It can be downloaded from:
http://homepage.mac.com/lspratt/FileSharing2.html


The TextView implementation is below.

Cheers,
Lindsey

@implementation TextView

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self)
{
NSAttributedString* str = [[NSAttributedString alloc] initWithString:@"Some things never change."];
_contents = [[NSTextStorage allocWithZone:[self zone]] init];
[_contents setAttributedString:str];
}
return self;
}


- (NSTextStorage*) contents
{
	return _contents;
}

static NSLayoutManager *sharedDrawingLayoutManager() {
// This method returns an NSLayoutManager that can be used to draw the contents of the TextView.
static NSLayoutManager *sharedLM = nil;
if (!sharedLM) {
NSTextContainer *tc = [[NSTextContainer allocWithZone:NULL] initWithContainerSize:NSMakeSize(1.0e6, 1.0e6)];


        sharedLM = [[NSLayoutManager allocWithZone:NULL] init];

        [tc setWidthTracksTextView:NO];
        [tc setHeightTracksTextView:NO];
        [sharedLM addTextContainer:tc];
        [tc release];
    }
    return sharedLM;
}

- (float) textWidth:(NSString*)text
/*"Determine the width of text for the default font information."*/
{
NSFont* userFont = [NSFont userFontOfSize:0.0]; // default user font @ default size.
NSDictionary* dict = [NSDictionary dictionaryWithObject:userFont forKey:NSFontAttributeName];
NSSize stringSize = [text sizeWithAttributes:dict];


// The maximum advancement is a fudge so that the text won't be wrapped to the next line.

NSSize maximumAdvancement = [userFont maximumAdvancement];

return stringSize.width + maximumAdvancement.width;
}


- (void) rotateInRect:(NSRect)bounds byDegrees:(float)angle
{
NSSize originShift = NSMakeSize(bounds.origin.x + bounds.size.width / 2.0, bounds.origin.y + bounds.size.height / 2.0);

NSAffineTransform* transform = [NSAffineTransform transform];
[transform translateXBy: originShift.width yBy: originShift.height];
[transform rotateByDegrees:angle];
[transform translateXBy: -originShift.width yBy: -originShift.height];
[transform concat]; // sets the transform in the (new) current NSGraphicsContext.
}


- (void) drawContents:(NSTextStorage*)contents inRect:(NSRect)bounds
{
NSLayoutManager *lm = sharedDrawingLayoutManager();
NSTextContainer *tc = [[lm textContainers] objectAtIndex:0];
NSRange glyphRange;

[tc setContainerSize:bounds.size];
[contents addLayoutManager:lm];
// Force layout of the text and find out how much of it fits in the container.
glyphRange = [lm glyphRangeForTextContainer:tc];

if (glyphRange.length > 0)
{
[lm drawBackgroundForGlyphRange:glyphRange atPoint:bounds.origin];
[lm drawGlyphsForGlyphRange:glyphRange atPoint:bounds.origin];
}
[contents removeLayoutManager:lm];
}


- (void)drawRect:(NSRect)rect
{
NSTextStorage *contents = [self contents];
NSRect bounds = NSMakeRect(50.0, 250.0, [self textWidth:[contents string]], 20.0);
if( NSIntersectsRect(rect, bounds) )
{
float angle = 30.0;

[self rotateInRect:bounds byDegrees:angle];

NSFrameRect(bounds);

if ([contents length] > 0)
{
[self drawContents:contents inRect:bounds];
}
}
}


@end

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: [OT] Submitting app listing to Apple's product site
  • Next by Date: Temporarily Editable NSTextField
  • Previous by thread: Re: (freely) Rotating Text
  • Next by thread: Re: (freely) Rotating Text
  • Index(es):
    • Date
    • Thread