在需要重绘的时候,我们不要直接调用drawRect方法,要调用setNeedsDisplay方法
先上界面效果图
- (void)drawRect:(CGRect)rect
{
[self.txt drawInRect:self.bounds withFont:[UIFont systemFontOfSize:self.fontSize] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
}
-(void) setFontSize:(float)fontSize{
_fontSize=fontSize;
[self setNeedsDisplay];
}
-(void) setTxt:(NSString *)txt{
_txt=txt;
[self setNeedsDisplay];
}