如何从视图控制器中获取自绘制视图的文本



我有一个self - draw view with text:

NSString* text = @"text";
UIFont* font = [UIFont systemFontOfSize:14.f];
NSShadow* shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(1, 1);
shadow.shadowColor = [UIColor whiteColor];
shadow.shadowBlurRadius = 0.5;
NSDictionary* attirbutes =
[NSDictionary dictionaryWithObjectsAndKeys:
 [UIColor grayColor],   NSForegroundColorAttributeName,
 font,                  NSFontAttributeName,
 shadow,                NSShadowAttributeName, nil];
CGSize textSize = [text sizeWithAttributes:attirbutes];
CGRect textRect = CGRectMake(0,
                             0,
                             textSize.width, textSize.height);
textRect = CGRectIntegral(textRect);
[text drawInRect:textRect withAttributes:attirbutes];

在我的ViewController中,我想从另一个类中获取它并在我的selfDrawnView中绘制它。我该如何执行?

我不太清楚你到底在问什么。如果你用你的新视图类子类化UIView并且重写了drawRect方法你需要添加一个属性来保存你正在绘制的文本。

然后在你的视图控制器中,你只需将文本传递给该属性并调用[myCustomView setNeedDisplay]来强制重绘。

最新更新