Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)drawRect:(NSRect)dirtyRect
- {
- NSFont *font = [NSFont fontWithName:@"Zapfino" size:40.0];
- NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"flapfino\ntest" attributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]];
- NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:string];
- NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
- NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
- [layoutManager addTextContainer:textContainer];
- [textStorage addLayoutManager:layoutManager];
- NSUInteger numGlyphs = [layoutManager numberOfGlyphs];
- NSRange glyphRange = NSMakeRange(0, numGlyphs);
- NSRect visibleBounds = NSZeroRect;
- for (NSUInteger i = glyphRange.location; i < glyphRange.location + glyphRange.length; ++i) {
- NSPoint offsetForGlyph = [layoutManager locationForGlyphAtIndex:i];
- offsetForGlyph.y += NSMinY([layoutManager lineFragmentRectForGlyphAtIndex:i effectiveRange:NULL]);
- NSRect boundingRectForGlyph = [[textStorage font] boundingRectForGlyph:[layoutManager glyphAtIndex:i]];
- if ([self isFlipped])
- boundingRectForGlyph.origin.y = - NSMinY(boundingRectForGlyph) - NSHeight(boundingRectForGlyph);
- visibleBounds = NSUnionRect(visibleBounds, NSOffsetRect(boundingRectForGlyph, offsetForGlyph.x, offsetForGlyph.y));
- }
- NSRect viewBounds = [self bounds];
- NSPoint offsetForCentering = NSMakePoint(NSWidth(viewBounds) / 2 - NSWidth(visibleBounds) / 2 - NSMinX(visibleBounds), NSHeight(viewBounds) / 2 - NSHeight(visibleBounds) / 2 - NSMinY(visibleBounds));
- [layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:offsetForCentering];
- NSRect typographicBounds = [layoutManager usedRectForTextContainer:textContainer];
- [[NSBezierPath bezierPathWithRect:NSOffsetRect(typographicBounds, offsetForCentering.x, offsetForCentering.y)] stroke];
- [[NSBezierPath bezierPathWithRect:NSOffsetRect(visibleBounds, offsetForCentering.x, offsetForCentering.y)] stroke];
- }
- - (BOOL)isFlipped
- {
- return YES;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement