我想像下面的样式一样绘制文本--图像总是在右上角,文本在图像的周围。
有人能告诉我如何在iOS上实现它吗?我需要使用核心文本吗?
提前感谢!
发布于 2011-03-13 03:02:41
是的,CoreText是实现这一目标的最佳方式。完成这项工作的代码在这里有点长。文章"Clipping a CGRect to a CGPath."中的一些代码可能会为你指明正确的方向,如果这样做的过程仍然令人困惑,那么ping我和我终于写下了我一直想要写的关于这个主题的博客文章。
发布于 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
发布于 2014-06-22 09:23:57
类似的问题。我解决这个问题的方法是:使用标准标记标签在HTML语言中创建包含图形的文本文档,将HTML和pngs添加到我的项目中,并使用UIWebView显示它。:-)
https://stackoverflow.com/questions/5284516
复制相似问题