Search code examples
fontscocos2d-iphoneopentype

OTF fonts with CCLabelTTF


I'm trying to use a .otf font with CCLabelTTF...

It doesn't work. Said font works well with native UILabel. Any workaround? Render UILabel to CCSprite-ish? Converting OTF to TTF... seems like too much trouble.

  • Edit. I wrote this. I think it works. Will see how it goes.
  • Edit 2. What's cool is if you create labels with color UIColor.whiteColor, you can then set the color with mylabel.color = ccc3(0, 0, 0); to make it black, or any other color. Then you can tint the color freely of the text using CCTintTo etc.

Spriteuifont.h:

#import "CCSprite.h"

@interface Spriteuifont : CCSprite
+(Spriteuifont*)spriteuifontWithText:(NSString*)text font:(UIFont*)font color:(UIColor*)color;
@property (nonatomic, retain) NSString* string;
@end

Spriteuifont.m:

#import "Spriteuifont.h"

#import "UIView+additions.h"

@interface Spriteuifont ()
@property (nonatomic, retain) NSString* _string;
@property (nonatomic, retain) UIFont* _font;
@property (nonatomic, retain) UIColor* _color;
@end



@implementation Spriteuifont
@synthesize _string, _font, _color; // retain
-(void)dealloc {
    self._string = nil;
    self._font = nil;
    self._color = nil;
    [super dealloc];
}
+(Spriteuifont*)spriteuifontWithText:(NSString*)text font:(UIFont*)font color:(UIColor*)color {
    Spriteuifont* s = [[[self alloc] initWithText:text font:font color:color] autorelease];
    return s;
}
-(id)initWithText:(NSString*)text font:(UIFont*)font color:(UIColor*)color {
    self = [super init];
    if (self) {
        self._font = font;
        self._color = color;
        self.string = text;
    }
    return self;
}
-(void)refresh {
    const CGSize SIZE = [self._string sizeWithFont:self._font];
    UILabel* labelTestfont = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, SIZE.width, SIZE.height)] autorelease];
    labelTestfont.font = self._font;
    labelTestfont.backgroundColor = UIColor.clearColor;
    labelTestfont.opaque = NO;
    labelTestfont.text = self._string;
    labelTestfont.textColor = self._color;
    UIImage* img = [labelTestfont image];
    // GL_ONE or GL_ONE_MINUS_SRC_ALPHA ?
    CCTexture2D* aa = [[[CCTexture2D alloc] initWithCGImage:img.CGImage resolutionType:GL_ONE] autorelease];
    [self setTexture:aa];
    [self setTextureRect:labelTestfont.frame];
}
-(void)setString:(NSString *)string {
    self._string = string;
    [self refresh];
}
-(NSString*)string {
    return self._string;
}
@end

Usage:

Spriteuifont* label = [Spriteuifont spriteuifontWithText:@"0" font:[UIFont fontWithName:@"myfontwhatever" size:16] color:UIColor.blackColor];

Btw the UIView category .m file contains this:

#import <QuartzCore/QuartzCore.h>
@implementation UIView (additions)
-(UIImage*)image {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
@end

Solution

  • There is no support for OTF fonts in Cocos2d-iphone currently. I just use native iOS support for now, rendering the contents through CCTexture2D like explained in the question. Maybe not good for performance but in my case it didn't matter much.