简体   繁体   English

drawRect:rect如何调用此方法,我正在绘制一个字符串

[英]drawRect:rect how to call this method , I am using to draw a string

-(void)drawRect:(CGRect)rect
{   
    CGContextRef  context = UIGraphicsGetCurrentContext();
    CGContextSelectFont(context, "Arial", 24, kCGEncodingFontSpecific);
    CGContextSetTextPosition(context,80,80);
    CGContextShowText(context, "hello", 6);
    //not even this works
    CGContextShowTextAtPoint(context, 1,1, "hello", 6);
}

I want to know how to call the above method, I have pasted the above code into the implementation file, but don't know why it's not called when I execute the project. 我想知道如何调用上面的方法,我已经将上面的代码粘贴到实现文件中,但是不知道为什么在执行项目时为什么不调用它。

Does anyone know why this could be? 有谁知道为什么会这样吗?

setNeedsDisplay should do that for you, here is a link link text setNeedsDisplay应该为您做到这一点,这是一个链接链接文本

The methods is also called when the view first displays... 当视图首次显示时,也会调用这些方法。

Finally I got the anser, just take a new class and inherit the class with UIView instead of NSobject, 最后,我得到了分析器,只需选择一个新类并使用UIView而不是NSobject继承该类,

This class will give us UIView object and it will also contain the drawRect method inbuilt. 此类将为我们提供UIView对象,还将包含内置的drawRect方法。

so this is How i done. 所以这就是我的做法。 it :) 它:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM