2013-01-21 6 views
5

मैं आईओएस ऐप में एक पीडीएफ उत्पन्न करता हूं, ड्रॉप्ड फ़ंक्शन का उपयोग करते हुए, nsobject क्लास में ड्रॉटेक्स्ट फ़ंक्शन को कॉल करते समय यह टेक्स्ट को स्पष्ट रूप से फ्रेम और स्ट्रिंग के अनुसार खींचता है जो मैंने निर्दिष्ट किया है।आईओएस: पीडीएफ पीढ़ी में पाठ खींचते समय फ़ॉन्ट कैसे सेट करें?

मेरे कोड

+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect 
{ 

    CFStringRef stringRef = (__bridge CFStringRef)textToDraw; 
// Prepare the text using a Core Text Framesetter 
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL); 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); 


    CGMutablePathRef framePath = CGPathCreateMutable(); 
    CGPathAddRect(framePath, NULL, frameRect); 

// Get the frame that will do the rendering. 
    CFRange currentRange = CFRangeMake(0, 0); 
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); 
    CGPathRelease(framePath); 

// Get the graphics context. 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

// Put the text matrix into a known state. This ensures 
// that no old scaling factors are left in place. 
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 


// Core Text draws from the bottom-left corner up, so flip 
// the current transform prior to drawing. 
    CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 
// Draw the frame. 
    CTFrameDraw(frameRef, currentContext); 

    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2); 


    CFRelease(frameRef); 
    CFRelease(stringRef); 
    CFRelease(framesetter); 
} 

है लेकिन मैं पाठ fontsize बड़ा और bolder सेट करना होगा।

मैं

CGContextSelectFont(currentContext, "Helvetica", 20, kCGEncodingMacRoman); 

और

CGContextSetFontSize (currentContext, 20); 

ऊपर समारोह में mr.texian द्वारा प्रदान की जवाब के संदर्भ में, लेकिन कोई बदलाव नहीं इस्तेमाल किया

मैं में उत्पन्न पीडीएफ दिखा रहा UIWebView।

मुझे यह कोड नेट से मिला है। मुझे नहीं पता था कि फ़ॉन्ट सेटिंग के लिए कोड को कहां संपादित करना है। किसी भी मदद की सराहना की जाएगी।

उत्तर

6

कृपया कोरटेक्स्ट, एक बहुत ही उपयोगी नींव वर्ग आज़माएं।नमूना कोड निम्नलिखित है:

CTFontRef font = CTFontCreateWithName((CFStringRef)@"Arial", 16.0f, nil); 
CFAttributedStringSetAttribute(textString,CFRangeMake(0, strLength-1), kCTFontAttributeName, font); 

इसे CFAttributtedStringRef पॉइंटर में जोड़ें।

यह काम करना चाहिए।

+0

ऊपर दिए गए दो कोड लाइनों को CFAttributtedStringRef सूचक में कैसे जोड़ना है? –

2

मैं इन कर रहे हैं लगता है कि आप

void CGContextSelectFont (
    CGContextRef c, 
    const char *name, 
    CGFloat size, 
    CGTextEncoding textEncoding 
); 

और

void CGContextSetFontSize (
    CGContextRef c, 
    CGFloat size 
); 

कुछ की तरह ...

CGContextSelectFont(currentContext, "Helvetica", 20, kCGEncodingMacRoman); 

या

CGContextSetFontSize (currentContext, 20); 
के लिए क्या देख रहे

आशा है कि मदद करता है, यहाँ और अधिक है ... Quartz 2D Programming Guide - Text

+0

त्वरित जवाब है, जहां इस कोड – NAZIK

+0

मैं उर कोड का इस्तेमाल किया, लेकिन उत्पादन एक ही रहता है – NAZIK

+0

यह आवश्यक शून्य CGContextSetFontSize उपयोग करने के लिए ( CGContextRef ग, CGFloat आकार ) है लिखने के लिए +1; – NAZIK

2

बनाएँ CTFontCTFontCreateWithNameAndOptions का उपयोग कर (जाँच: CTFont Reference)

एक परिवर्तनशील जिम्मेदार ठहराया स्ट्रिंग बनाएँ (CFMutableAttributedString Reference) आप इस परिवर्तनशील स्ट्रिंग को संपादित करने और किसी भी कर सकते हैं आप चाहते हैं परिवर्तन अपने मामले में, इसे फ़ॉन्ट सेट करें।

इस स्ट्रिंग के साथ अपना CTFrameSetter बनाएं और फ़ॉन्ट प्रकट होना चाहिए।

आप पूरी तरह से करना चाहते हैं और पर्याप्त समय है, तो String programming Guide

पढ़ आप CFMutableAttributedString प्रयोग करने के लिए एक त्वरित कोड नमूना चाहते हैं, stackoverflow पर CFMutableAttributedString खोज और सबसे जवाब तुम ऐसा कैसे उपयोग करने के लिए की एक विचार दे देंगे । उदाहरण के लिए this question

1

आप आईफोन प्रोजेक्ट में पीडीएफ उत्पन्न करने के लिए "libHaru" आज़मा सकते हैं।

https://github.com/akisute/iPhonePDF

यह मदद करता है उम्मीद है कि !!!

1

CGContextSetFont के बारे में क्या?

NSString *fontName = @"Helvetica"; 
CGFontRef fontRef = CGFontCreateWithFontName((__bridge CFStringRef)fontName); 
CGContextSetFont(context, fontRef); 
CGContextSetFontSize(context, 20); 

मत भूलना इसे जारी करने:

CGFontRelease(fontRef); 
0

एक बहुत खोज के बाद, fnd कि itsdamslife के जवाब सही है, लेकिन यह मेरी कोड के लिए समर्थन नहीं किया।

लेकिन, मैंने इसे सस्ता हल किया। यानी बोल्ड फ़ॉन्ट प्राप्त करने के लिए drawText: फ़ंक्शन को कई बार बुलाया जाता है।

for(int i=0;i<5;i++) 
{ 
[self drawtext]; 
} 
संबंधित मुद्दे