在iphone中使用coregraphics绘制文本

时间:2010-05-28 11:35:00

标签: iphone text core-graphics

如何使用iphone中的coregraphics在imageview上绘制文字

2 个答案:

答案 0 :(得分:25)

使用UILabel覆盖imageView或使用

在当前图形上下文中绘制字符串
- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(UILineBreakMode)lineBreakMode

答案 1 :(得分:1)

TextImageView.h

@interface TextImageView : UIView

@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) NSString *text;

@end

TextImageView.m

@implementation TextImageView

- (void)drawRect:(CGRect)rect
{
    [self.image drawInRect:self.bounds];
    [self.text drawInRect:self.bounds
           withAttributes:@{}];
}

@end

已弃用接受答案中的方法。您想要使用的方法是- [NSString drawInRect:withAttributes:]

您应该在drawRect:子类的UIView方法中调用此方法,但如果您继承UIImageView,则永远不会调用drawRect:。 (From the docs:“UIImageView不调用其子类的drawRect:方法。”)

所以,你真的无法在UIImageView上绘制文字,但是你可以将UIView子类化,并让它在上面提出一个带有文字的图像。

相关问题