में एक विशेषता स्ट्रिंग प्रदर्शित करना मेरे पास एक साधारण उदाहरण ऐप है जहां मैं 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
एस होने जा रहा है।
मैं * हमेशा * .CGColor उपयोग करने के लिए भूल जाते हैं CoreGraphics स्तर पर काम करते समय। बहुत बहुत धन्यवाद! –