2013-04-17 4 views
8

J'ai un exemple d'application simple où je crée un CATextLayer et définissez sa propriété string sur NSAttributedString. J'ajoute ensuite ce CATextLayer à une vue.Affichage d'une chaîne affectée dans une CATextLayer

#import <CoreText/CoreText.h> 
#import <QuartzCore/QuartzCore.h> 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    CTFontRef fontFace = CTFontCreateWithName((__bridge CFStringRef)(@"HelfaSemiBold"), 24.0, NULL); 
    NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init]; 
    [attributes setObject:(__bridge id)fontFace forKey:(NSString*)kCTFontAttributeName]; 
    [attributes setObject:[UIColor blueColor] forKey:(NSString*)kCTForegroundColorAttributeName]; 
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@"Lorem Ipsum" attributes:attributes]; 

    CATextLayer *textLayer = [CATextLayer layer]; 
    textLayer.backgroundColor = [UIColor whiteColor].CGColor; 
    textLayer.frame = CGRectMake(20, 20, 200, 100); 
    textLayer.contentsScale = [[UIScreen mainScreen] scale]; 
    textLayer.string = attrStr; 

    [self.view.layer addSublayer:textLayer]; 
} 

Cela fonctionne très bien dans le simulateur, sauf que le texte est noir au lieu de bleu. Lors de l'exécution sur un périphérique, tout s'affiche comme sur le simulateur mais génère les deux erreurs suivantes dans la console.

<Error>: CGContextSetFillColorWithColor: invalid context 0x0 
<Error>: CGContextSetStrokeColorWithColor: invalid context 0x0 

Dois-je définir un CGContext quelque part? Est-ce que je mets mes attributs mal? Suis-je complètement sur la mauvaise piste. Remarque, ceci pour une application iOS 5.x et je veux utiliser un CATextLayer pour des raisons de performances. La vraie application va avoir beaucoupCATextLayer s.

Répondre

12

Vous devez utiliser CGColor au lieu de UIColor pour kCTForegroundColorAttributeName:

[attributes setObject:(__bridge id)[UIColor blueColor].CGColor 
       forKey:(NSString *)kCTForegroundColorAttributeName]; 
+0

I * toujours * oublier d'utiliser .CGColor lorsque vous travaillez au niveau CoreGraphics. Merci beaucoup! –

0

Je converti ce à Swift 3 et le mettre à l'intérieur SpriteKit ainsi:

import QuartzCore 
import SpriteKit 

class GameScene: SKScene { 

    var textLayer = CATextLayer() 

    func helloWord() { 
    let fontFace = CTFontCreateWithName((("HelfaSemiBold") as CFString), 
             24.0, 
             nil) 
    var attributes: [AnyHashable: Any]? = [:] 
    attributes?[(kCTFontAttributeName as String)] = fontFace 
    attributes?[(kCTForegroundColorAttributeName as String)] = UIColor.blue.cgColor 

    let attrStr = NSAttributedString(string: "Hello Attributed Word!", 
            attributes: attributes as! [String : Any]?) 

    textLayer.backgroundColor = UIColor.white.cgColor 
    textLayer.frame = CGRect(x: CGFloat(50), y: CGFloat(200), 
          width: CGFloat(300), height: CGFloat(100)) 
    textLayer.contentsScale = UIScreen.main.scale 
    textLayer.string = attrStr 
    self.view?.layer.addSublayer(textLayer) 
    } 

    override func didMove(to view: SKView) { 
    helloWord() 
    } 
} 
Questions connexes