2010-07-19 7 views
8

An image clarifying thingsकोर टेक्स्ट के साथ लंबवत संरेखित?

मैं CTFramesetter फ्रेम में टेक्स्ट के लंबवत संरेखण को कैसे बदलूं? मैं चाहता हूं कि मेरा टेक्स्ट शीर्ष पर रहने के बजाय मध्य में हो। मैं कोर टेक्स्ट फ्रेमवर्क का उपयोग कर रहा हूं। क्षैतिज संरेखण को बदलने के लिए अनुच्छेद की एक सेटिंग है लेकिन लंबवत नहीं है।

उत्तर

7

अंत में यह समझ से बाहर ...

CGRect boundingBox = CTFontGetBoundingBox(font); 

//Get the position on the y axis 
float midHeight = self.frame.size.height/2; 
midHeight -= boundingBox.size.height/2; 

CGPathAddRect(path, NULL, CGRectMake(0, midHeight, self.frame.size.width, boundingBox.size.height)); 
+0

यह कुछ फोंट के लिए काम नहीं करता। सिर्फ अकादमीएंग्रावेडलेप्लेप्लेन फ़ॉन्ट का उपयोग करके परीक्षण किया गया है, और फ़ॉन्ट – Coolant

+0

नहीं खींचा गया है, लेकिन मैंने रेक्ट आकार की ऊंचाई को मूल सीमा ऊंचाई के रूप में सेट करके इसे काम करने में कामयाब रहा है, बाउंडिंग नहीं है बॉक्स ऊंचाई – Coolant

+0

और क्या यह कई लाइनों के लिए काम करता है ??? – MatterGoal

4

धन्यवाद निक, यह एक बड़ा टुकड़ा था।

बस उस पर विस्तार, यदि आपका एक enum के साथ शीर्ष, मध्य और नीचे संरेखण कर रही है, उदाहरण के लिए आप यह इतना की तरह कर सकता है:

if (VerticalAlignmentTop == currentTextAlignment) { 
    CGPathAddRect(path, NULL, rect); // Draw normally (top) 
} 
else if (VerticalAlignmentMiddle == currentTextAlignment) { 
    CGRect boundingBox = CTFontGetBoundingBox(fontRef); 

    //Get the position on the y axis (middle) 
    float midHeight = rect.size.height/2; 
    midHeight -= boundingBox.size.height/2; 

    CGPathAddRect(path, NULL, CGRectMake(0, midHeight, rect.size.width, boundingBox.size.height)); 
} 
else { 
    CGRect boundingBox = CTFontGetBoundingBox(fontRef); 

    CGPathAddRect(path, NULL, CGRectMake(0, 0, rect.size.width, boundingBox.size.height)); 
} 
1

इस तथ्य के लिए खातों कि एकाधिक फ़ॉन्ट प्रकार और शैलियों कर सकते हैं एक फ्रेम में प्रयोग किया जाता है (दोनों ऊंचाई और पाठ की चौड़ाई की गणना करता है, if(index == lastLineIndex) ब्लॉक में देखने के लिए जहां ऊंचाई गणना की जाती है को देखने के लिए) किया:

- (CGSize) measureFrame: (CTFrameRef) frame forContext: (CGContext *) cgContext 
{ 
    CGPathRef framePath = CTFrameGetPath(frame); 
    CGRect frameRect = CGPathGetBoundingBox(framePath); 

    CFArrayRef lines = CTFrameGetLines(frame); 
    CFIndex numLines = CFArrayGetCount(lines); 

    CGFloat maxWidth = 0; 
    CGFloat textHeight = 0; 

    // Now run through each line determining the maximum width of all the lines. 
    // We special case the last line of text. While we've got it's descent handy, 
    // we'll use it to calculate the typographic height of the text as well. 

    CFIndex lastLineIndex = numLines - 1; 
    for(CFIndex index = 0; index < numLines; index++) 
    { 
     CGFloat ascent, descent, leading, width; 
     CTLineRef line = (CTLineRef) CFArrayGetValueAtIndex(lines, index); 
     width = CTLineGetTypographicBounds(line, &ascent,  &descent, &leading); 

     if(width > maxWidth) 
     { 
      maxWidth = width; 
     } 

     if(index == lastLineIndex) 
     { 
      // Get the origin of the last line. We add the descent to this 
      // (below) to get the bottom edge of the last line of text. 

      CGPoint lastLineOrigin; 
      CTFrameGetLineOrigins(frame, CFRangeMake(lastLineIndex, 1), &lastLineOrigin); 

      // The height needed to draw the text is from the bottom of the last line 
      // to the top of the frame. 

      textHeight =  CGRectGetMaxY(frameRect) - lastLineOrigin.y + descent; 
     } 
    } 

    // For some text the exact typographic bounds is a fraction of a point too 
    // small to fit the text when it is put into a context. We go ahead and round 
    // the returned drawing area up to the nearest point.  This takes care of the 
    // discrepencies. 

    return CGSizeMake(ceil(maxWidth), ceil(textHeight)); 
} 

संदर्भ: स्कॉट थॉम्पसन (http://lists.apple.com/archives/quartz-dev/2008/Mar/msg00079.html)

3

आप अपने स्ट्रिंग के बाउंडिंग बॉक्स के आयत को प्राप्त करने के लिए [NSString boundingRectWithSize: विकल्प: विशेषताएँ: संदर्भ:] का उपयोग कर सकते हैं, जो मल्टी-लाइन टेक्स्ट को भी अनुमति देता है। अपने ड्रा पाठ विधि में, निम्न (RECT आयत जहां आप पाठ आकर्षित करने के लिए करना चाहते हैं) कार्य करें:

// get the graphics context 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSaveGState(context); 

// flip the context coordinate 
CGContextTranslateCTM(context, 0.0f, 2*RECT.origin.y+RECT.size.height); 
CGContextScaleCTM(context, 1.0f, -1.0f); 

// Set the text matrix. 
CGContextSetTextMatrix(context, CGAffineTransformIdentity); 

// set text horizontal alignment 
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
paragraphStyle.alignment = NSTextAlignmentCenter; 

NSDictionary *attributes = @{NSParagraphStyleAttributeName:paragraphStyle, NSFontAttributeName:YOUR_FONT, NSForegroundColorAttributeName:TEXT_COLOR}; 
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:YOUR_TEXT attributes:attributes]; 

CGMutablePathRef path = CGPathCreateMutable(); 

// set text vertical alignment 
CGSize textSize = [text boundingRectWithSize:RECT.size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; 
CGPathAddRect(path, NULL, CGRectMake(RECT.origin.x, RECT.origin.y-(RECT.size.height-textSize.height)/2.0f, RECT.size.width, RECT.size.height)); 

CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString); 
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, attrString.length), path, NULL); 
CTFrameDraw(frame, context); 

CFRelease(frame); 
CFRelease(path); 
CFRelease(frameSetter); 

[attrString release]; 
[paragraphStyle release]; 

CGContextRestoreGState(context);