核心文本 - 在目标C中的自定义视图上绘制简单文本

时间:2014-11-09 04:09:36

标签: objective-c

我制作了一个自定义视图,在这个自定义视图中,我想绘制一个简单的文本。我一直在寻找几个小时,没有找到运气。我在Apple文档中找到的那个很难理解。任何人都可以获得教程的示例或链接吗?感谢。

1 个答案:

答案 0 :(得分:3)

这是一个非常简单的例子。您只需要在自定义视图的drawRect:中的字符串上调用drawAtPoint:withAttributes:。

@implementation CustomView {
    NSDictionary *attributes;
    NSString *theText;
}

-(void)awakeFromNib {
    attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:24], NSForegroundColorAttributeName:[UIColor redColor]};
    theText = @"This is my text";
}


- (void)drawRect:(CGRect)rect {
    [theText drawAtPoint:CGPointMake(5, 5) withAttributes:attributes];
}