2011-03-12 8 views
17

मैं निम्नलिखित शैली की तरह टेक्स्ट खींचना चाहता हूं - छवि हमेशा शीर्ष दाएं कोने पर है, और पाठ छवि के चारों ओर है।आईओएस पर टेक्स्ट रैपिंग के साथ मैं छवि कैसे आकर्षित कर सकता हूं?

enter image description here

किसी को भी मुझे बता सकते हैं कि कैसे iOS पर इस लागू करने के लिए? क्या मुझे कोर टेक्स्ट का उपयोग करने की ज़रूरत है?

अग्रिम धन्यवाद!

उत्तर

12

हां, कोरटेक्स्ट ऐसा करने का सबसे अच्छा तरीका है। ऐसा करने के लिए कोड यहां पोस्ट करने में थोड़ा लंबा है। कुछ कोड जो आपको सही दिशा में इंगित कर सकते हैं लेख "Clipping a CGRect to a CGPath." में है यदि ऐसा करने की प्रक्रिया अभी भी भ्रमित है, तो मुझे पिंग करें और अंत में मैं ब्लॉग पोस्ट लिखता हूं जिसका अर्थ है कि मैं इस विषय पर लिखना चाहता हूं।

+0

मुझे लगता है कि मुझे आपकी मदद चाहिए। मैं आपके ब्लॉग पोस्ट में कोड को काफी समझ नहीं पा रहा हूं। क्या आप इस विषय पर एक लेख लिख सकते हैं? धन्यवाद! – nonamelive

+0

इस बारे में भूल गए नहीं है; इसे एक साथ रखने के लिए बस थोड़ा सा लिया गया है। –

+7

यह थोड़ा मोटा है, लेकिन उम्मीद है कि यह आपको सही दिशा में ले जायेगा। http://robnapier.net/blog/wrapping-text-around-shape-with-coretext-540 –

1

चरण 1: एक वस्तु UIView

से

विरासत में मिला बनाएं चरण 2: ओवरराइड - (शून्य) drawRect: (CGRect) रेक्ट विधि

चरण 3: इस विधि

/* Define some defaults */ 
float padding = 10.0f; 

/* Get the graphics context for drawing */ 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 

/* Core Text Coordinate System is OSX style */ 
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity); 
CGContextTranslateCTM(ctx, 0, self.bounds.size.height); 
CGContextScaleCTM(ctx, 1.0, -1.0); 
CGRect textRect = CGRectMake(padding, padding, self.frame.size.width - padding*2, self.frame.size.height - padding*2); 

/* Create a path to draw in and add our text path */ 
CGMutablePathRef pathToRenderIn = CGPathCreateMutable(); 
CGPathAddRect(pathToRenderIn, NULL, textRect); 

/* Add a image path to clip around, region where you want to place image */ 
CGRect clipRect = CGRectMake(padding, self.frame.size.height-50, 50, 40); 
CGPathAddRect(pathToRenderIn, NULL, clipRect); 

/* Build up an attributed string with the correct font */ 
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.Text]; 

//setFont 
CTFontRef font = CTFontCreateWithName((CFStringRef) [UIFont systemFontOfSize:10].fontName, [UIFont systemFontOfSize:10].lineHeight, NULL); 
CFAttributedStringSetAttribute((CFMutableAttributedStringRef) attrString, CFRangeMake(0, attrString.length), kCTFontAttributeName,font); 

//set text color 
CGColorRef _white=[UIColor whiteColor].CGColor; 
CFAttributedStringSetAttribute((CFMutableAttributedStringRef)(attrString), CFRangeMake(0, attrString.length),kCTForegroundColorAttributeName, _white); 

/* Get a framesetter to draw the actual text */ 
CTFramesetterRef fs = CTFramesetterCreateWithAttributedString((CFAttributedStringRef) attrString); 
CTFrameRef frame = CTFramesetterCreateFrame(fs, CFRangeMake(0, attrString.length), pathToRenderIn, NULL); 

/* Draw the text */ 
CTFrameDraw(frame, ctx); 

/* Release the stuff we used */ 
CFRelease(frame); 
CFRelease(pathToRenderIn); 
CFRelease(fs); 
लिए निम्न कोड जोड़ें

चरण 4: निम्नानुसार उपयोग करें;

TextLayoutView *TextWrappingImage=[[TextLayoutView alloc] init...your own constructor...]; 

TextWrappingImage.backgroundColor=[UIColor clearColor]; 

[cell addSubview:TextWrappingImage]; //Add as subview where you want 
0

इसी तरह की समस्या। मैंने मानक मार्कअप टैग का उपयोग करके HTML में अपना टेक्स्ट-ग्राफिक्स दस्तावेज़ बनाकर हल किया, मेरे प्रोजेक्ट में एचटीएमएल और पीएनजी जोड़ा, और इसे UIWebView का उपयोग करके प्रदर्शित किया। :-)

+0

+1। यह सबसे सुरुचिपूर्ण समाधान नहीं हो सकता है, हालांकि मैंने इस विधि का उपयोग किया था और यह इसे संभालने के लिए कोड की एक पंक्ति है और एक div तत्व पर बस कुछ सरल सीएसएस .. 'width: 95px; ऊँचाई: 95px; मार्जिन: 5 पीएक्स 5 पीएक्स 5 पीएक्स 0 पीएक्स; सीमा: 1 पीएक्स ठोस #eee; बाईंओर तैरना; पृष्ठभूमि: यूआरएल (% @) केंद्र केंद्र; पृष्ठभूमि दोहराने: नहीं दोहराने; पृष्ठभूमि आकार: इसमें शामिल है; 'मेरी राय में लागू करने के लिए बहुत छोटा और आसान है। धन्यवाद – thefoyer

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

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