2010-09-22 16 views
9

मैं एनएसएट्रिब्यूटेड स्ट्रिंग को कैसे ले सकता हूं और आईपैड पर कोर टेक्स्ट में इसका उपयोग करने का प्रयास कर रहा हूं। मैं WWDC वीडियो में से एक देखा (110) जो स्लाइड्स (लेकिन कोई स्रोत कोड) है और यह बताता है कि किसी NSAttributedString बनाने का तरीका है, तो बस एक CTFramesetterRef में डाल दिया:क्या मैं आईओएस में कोर टेक्स्ट में एनएसएट्रिब्यूटेड स्ट्रिंग का उपयोग कर सकता हूं?

CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 24.0, NULL); 

NSString* unicodeString = NSLocalizedString(@"TitleString", @"Window Title"); 
CGColorRef color = [UIColor blueColor].CGColor; NSNumber* underline = [NSNumber numberWithInt:kCTUnderlineStyleSingle|kCTUnderlinePatternDot]; 
NSDictionary* attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:helveticaBold, (NSString*)kCTFontAttributeName, color, (NSString*)kCTForegroundColorAttributeName, underline, (NSString*)kCTUnderlineStyleAttributeName, nil]; 
NSAttributedString* stringToDraw = [[NSAttributedString alloc] initWithString:unicodeString attributes:attributesDict]; 

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(stringToDraw); 

CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); 
CFRelease(framesetter); 
CTFrameDraw(frame, context); 
CFRelease(frame); 

लेकिन जब मैं यह कोशिश है, यह

Passing argument 1 of 'CTFramesetterCreateWithAttributedString' from incompatible pointer type

CFAttributedString रहे हैं और NSAttributedString 'टोल फ्री पाटने': त्रुटि फेंकता है? मैंने कहीं पढ़ा है कि यह ओएसएक्स पर ही सच है, लेकिन इस तरह ऐप्पल अपने डेमो में ऐसा करता है ...

धन्यवाद!

: -Joe

+0

आप दिखाने के लिए बिंदु वाले रेखांकन प्राप्त करने में सक्षम थे? मुझे केवल पाठ के नीचे सादा एकल रेखांकन मिलता है। – learner2010

उत्तर

7

आप WWDC10 स्रोत कोड here डाउनलोड कर सकते हैं। इसके अलावा, टोल-फ्री ब्रिजिंग का उपयोग करें और स्पष्ट रूप से stringToDraw को CFAttributedStringRef पर डालें।

+0

धन्यवाद, लेकिन आपके द्वारा दिया गया लिंक कहता रहता है कि मेरा सत्र समाप्त हो गया है (यहां तक ​​कि जब मैं फिर से लॉगिन करने का प्रयास करता हूं)। मैंने पहले डब्ल्यूडब्ल्यूडीसी 10 नमूना कोड डाउनलोड किया है, लेकिन 110 इसमें नहीं था :( – jowie

+1

इसके अलावा, आपका समाधान काम करता है, धन्यवाद! :-) – jowie

+1

क्या आप अंतिम कार्य कोड पोस्ट कर सकते हैं। – user836026

3

आप लगभग वहां हैं। जब आप इसे विशेषताएँ शब्दकोश में जोड़ते हैं तो बस CTFontRef को id प्रकार पर डालें।

CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 24.0, NULL); 

NSString* unicodeString = NSLocalizedString(@"TitleString", @"Window Title"); 

CGColorRef color = [UIColor blueColor].CGColor; 
NSNumber* underline = [NSNumber numberWithInt: 
    kCTUnderlineStyleSingle|kCTUnderlinePatternDot]; 

NSDictionary* attributesDict = [NSDictionary dictionaryWithObjectsAndKeys: 
        (id)helveticaBold, (NSString*)kCTFontAttributeName, 
        color, (NSString*)kCTForegroundColorAttributeName, 
        underline, (NSString*)kCTUnderlineStyleAttributeName, nil]; 

NSAttributedString* stringToDraw = [[NSAttributedString alloc] 
          initWithString:unicodeString 
          attributes:attributesDict]; 
संबंधित मुद्दे

 संबंधित मुद्दे