2013-04-17 4 views
8

में एक विशेषता स्ट्रिंग प्रदर्शित करना मेरे पास एक साधारण उदाहरण ऐप है जहां मैं CATextLayer बनाता हूं और string प्रॉपर्टी को NSAttributedString पर सेट करता हूं। मैं फिर उस दृश्य में CATextLayer जोड़ता हूं।एक 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]; 
} 

यह सिम्युलेटर में बहुत अच्छा काम करता है सिवाय इसके कि पाठ नीले रंग के बजाय काला है। डिवाइस पर चलते समय सब कुछ सिम्युलेटर पर प्रदर्शित होता है लेकिन यह कंसोल में निम्न दो त्रुटियों को उत्पन्न करता है।

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

क्या मुझे कहीं CGContext सेट करना चाहिए? क्या मैं अपने गुण गलत कर रहा हूं? क्या मैं पूरी तरह गलत ट्रैक पर हूं। नोट, यह आईओएस 5.x ऐप के लिए है और मैं प्रदर्शन कारणों से CATextLayer का उपयोग करना चाहता हूं। असली ऐप कईCATextLayer एस होने जा रहा है।

उत्तर

12

आप kCTForegroundColorAttributeName के लिए CGColor बजाय UIColor का उपयोग करना चाहिए:

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

मैं * हमेशा * .CGColor उपयोग करने के लिए भूल जाते हैं CoreGraphics स्तर पर काम करते समय। बहुत बहुत धन्यवाद! –

0

मैं स्विफ्ट 3 को यह बदला है और SpriteKit अंदर डाल दिया और साथ ही:

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() 
    } 
} 
संबंधित मुद्दे