• 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: Replacement for CGContextSelectFont ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Replacement for CGContextSelectFont ?


  • Subject: Re: Replacement for CGContextSelectFont ?
  • From: Gabriel Zachmann <email@hidden>
  • Date: Sun, 18 Sep 2016 11:24:03 +0200

Dear Graham, dear Alex, dear Will,

thanks a lot for your helpful suggestions.

So far, I have written this code as a replacement for CGContextSelectFont().
I am putting it here for the record (i.e., Google's archives).
But if anyone could suggest modifications to make it better (e.g., remove a bug, make it simpler, or more future proof),
I will greatly appreciate it.


Best regards,
Gabriel.




@implementation TextLayerDelegate
- (void) drawLayer: (CALayer *) theLayer inContext: (CGContextRef) ctxt
{
	NSMutableString * mesg;
	// fill mesg
        // create font
        NSDictionary *fontAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                                        @"Andale Mono", (NSString *) kCTFontFamilyNameAttribute,
                                                        @"Regular", (NSString *) kCTFontStyleNameAttribute,
                                                        [NSNumber numberWithFloat: 16.0],
                                                        (NSString *) kCTFontSizeAttribute,
                                                        nil ];
        // kCTFontStyleNameAttribute variants could be "Bold" "Semi-Bold" -- check in FontBook
        CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes( (CFDictionaryRef) fontAttributes );
        CTFontRef font = CTFontCreateWithFontDescriptor( descriptor, 0.0, NULL );
        CFRelease( descriptor );

        // create an attributed string
        CGColorRef textcolor = CGColorCreateGenericRGB( 0.2, 0.2, 0.2, 1.0 );      // color of the "shadow"
        CFMutableDictionaryRef attributes = CFDictionaryCreateMutable( kCFAllocatorDefault, 0,
                                                                       &kCFTypeDictionaryKeyCallBacks,
                                                                       &kCFTypeDictionaryValueCallBacks );
        CFDictionaryAddValue( attributes, kCTFontAttributeName, font );
        CFDictionaryAddValue( attributes, kCTForegroundColorAttributeName, textcolor );
        CFAttributedStringRef attrString = CFAttributedStringCreate( kCFAllocatorDefault, (CFStringRef)mesg, attributes );

        // offset from bottom left corner
        float offset = 7.0f;
        if ( theLayer.bounds.size.height > 900 )
            offset = 15.0f;

        // draw the string as one line
        CTLineRef line = CTLineCreateWithAttributedString( attrString );
        CGContextSetTextPosition( ctxt, theLayer.bounds.origin.x + offset, theLayer.bounds.origin.y + offset );
        CTLineDraw( line, ctxt );
        // draw it again on top of it, with another color
        CFRelease( textcolor );
        textcolor = CGColorCreateGenericRGB( 1.0, 1.0, 1.0, 1.0 );            // color of the foreground text
        CFDictionaryReplaceValue( attributes, kCTForegroundColorAttributeName, textcolor );
        CFRelease( attrString );
        attrString = CFAttributedStringCreate( kCFAllocatorDefault, (CFStringRef)mesg, attributes );
        CFRelease( line );
        line = CTLineCreateWithAttributedString( attrString );
        CGContextSetTextPosition( ctxt, theLayer.bounds.origin.x + offset + 3, theLayer.bounds.origin.y + offset + 3 );
        CTLineDraw( line, ctxt );

        // release everything that was constructed by a function that has "Create" or "Copy" in it
        CFRelease( attributes );
        CFRelease( line );
        CFRelease( attrString );
        CFRelease( font );
        CFRelease( textcolor );
	[mesg release];
}

// this function gets called byway of the following in the main:

	textLayer_ = [CALayer layer];
	TextLayerDelegate * textlayer_delegate = [[TextLayerDelegate alloc] initWith: self];
	// ... set up this layer
	[textLayer_ setNeedsDisplay];
	[mainLayer_ addSublayer: textLayer_ ];



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Replacement for CGContextSelectFont ?
      • From: Graham Cox <email@hidden>
References: 
 >Replacement for CGContextSelectFont ? (From: Gabriel Zachmann <email@hidden>)
 >Re: Replacement for CGContextSelectFont ? (From: Alex Kac <email@hidden>)
 >Re: Replacement for CGContextSelectFont ? (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: Strange toolbar item error
  • Next by Date: How to make Xcode launch a different app when "Running" my code?
  • Previous by thread: Re: Replacement for CGContextSelectFont ?
  • Next by thread: Re: Replacement for CGContextSelectFont ?
  • Index(es):
    • Date
    • Thread