Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Draw Image with Text Wrapping on iOS?

I want to draw text just like the following style -- the image is always on the top right corner, and the text is around the image.

enter image description here

Could anyone tell me how to implement this on iOS? Do I need to use Core Text?

Thanks in advance!

like image 780
nonamelive Avatar asked Mar 12 '11 18:03

nonamelive


People also ask

How can you wrap and image with text?

Go to Picture Format or Shape Format and select Arrange > Wrap Text. If the window is wide enough, Word displays Wrap Text directly on the Picture Format tab. Choose the wrapping options that you want to apply. For example, In Line with Text, Top and Bottom, and Behind Text.

How do you wrap text in a drawing?

Update: Right-click on shape > Edit Style. Check Word wrap text in shape, then press OK.

What command should I use if I want to make my text wrapped in my image?

To wrap text around an image using the Wrap Text command: Select the image by clicking on it and then, on the Picture Tools Format tab in the Arrange group, click Wrap Text . Hover over the Wrap Text options and then click on one of them to select it.


2 Answers

Yes, CoreText is the best way to do this. The code to do it is a little long to post here. Some code that may point you in the right direction is in the article "Clipping a CGRect to a CGPath." If the process for doing it is still confusing, ping me and I finally write the blog post I've been meaning to write on the subject.

like image 131
Rob Napier Avatar answered Oct 23 '22 18:10

Rob Napier


Step 1 : Create an object inherited from UIView

Step 2 : Override - (void)drawRect:(CGRect)rect method

Step 3 : Add Following code to this method

/* 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);

Step 4 : Use as follows;

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

 TextWrappingImage.backgroundColor=[UIColor clearColor];

 [cell addSubview:TextWrappingImage]; //Add as subview where you want
like image 44
asetil Avatar answered Oct 23 '22 17:10

asetil