2013-04-17 29 views
8

Mam prostą przykładową aplikację, w której utworzę CATextLayer i ustawię jej właściwość string na NSAttributedString. Następnie dodam, że CATextLayer do widoku.Wyświetlanie przypisanego ciągu w 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]; 
} 

Działa to doskonale w symulatorze wyjątkiem tekstu jest czarny zamiast niebieskiego. Podczas działania na urządzeniu wszystko wyświetla się tak jak na symulatorze, ale generuje następujące dwa błędy w konsoli.

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

Czy powinienem ustawić gdzieś CGContext? Czy źle ustawiam swoje atrybuty? Czy jestem całkowicie na niewłaściwym torze? Zauważ, że jest to aplikacja na iOS 5.x i chcę używać jej z powodów związanych z wydajnością: CATextLayer. Prawdziwa aplikacja będzie mieć wieluCATextLayer s.

Odpowiedz

12

Musisz użyć zamiast UIColorCGColor dla kCTForegroundColorAttributeName:

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

I * zawsze * zapomnij użyć .CGColor podczas pracy na poziomie CoreGraphics. Dzięki wielkie! –

0

konwertowane to Swift 3 i umieścić go wewnątrz SpriteKit także:

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() 
    } 
} 
Powiązane problemy