首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在iOS上绘制带文本环绕的图像?

如何在iOS上绘制带文本环绕的图像?
EN

Stack Overflow用户
提问于 2011-03-13 02:28:32
回答 3查看 10.2K关注 0票数 18

我想像下面的样式一样绘制文本--图像总是在右上角,文本在图像的周围。

有人能告诉我如何在iOS上实现它吗?我需要使用核心文本吗?

提前感谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-13 03:02:41

是的,CoreText是实现这一目标的最佳方式。完成这项工作的代码在这里有点长。文章"Clipping a CGRect to a CGPath."中的一些代码可能会为你指明正确的方向,如果这样做的过程仍然令人困惑,那么ping我和我终于写下了我一直想要写的关于这个主题的博客文章。

票数 12
EN

Stack Overflow用户

发布于 2013-07-01 17:18:10

第1步:创建从UIView继承的对象

步骤2:覆盖-(空)drawRect:(CGRect)rect方法

步骤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);

第四步:使用方法如下;

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

 TextWrappingImage.backgroundColor=[UIColor clearColor];

 [cell addSubview:TextWrappingImage]; //Add as subview where you want
票数 1
EN

Stack Overflow用户

发布于 2014-06-22 09:23:57

类似的问题。我解决这个问题的方法是:使用标准标记标签在HTML语言中创建包含图形的文本文档,将HTML和pngs添加到我的项目中,并使用UIWebView显示它。:-)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5284516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档