我正在尝试使用该drawAtPoint:point withAttributes:attrs
方法将字符串附加到视图中,但是在视图内的任何地方都看不到该字符串,或者至少我看不到它(可能是颜色与视图相同,白色)。
以下代码是我在viewDidLoad
视图控制器中使用的代码:
NSMutableDictionary *stringAttributes = [[NSMutableDictionary alloc] init];
[stringAttributes setObject:[UIColor redColor]forKey: NSForegroundColorAttributeName];
NSString *someString = [NSString stringWithFormat:@"%@", @"S"];
[someString drawAtPoint:CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2) withAttributes: stringAttributes];
我做错了什么?
编辑:在遵循@rokjarc 的建议之后,我创建了一个视图控制器,其drawRect
方法是将字符串添加到当前上下文中:
#import <Foundation/Foundation.h>
@interface XYZSomeView : UIView
@end
#import "XYZSomeView.h"
#import "NSString+NSStringAdditions.h"
@implementation XYZSomeView
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
NSMutableDictionary *stringAttributes = [[NSMutableDictionary alloc] init];
[stringAttributes setObject:[UIColor redColor]forKey: NSForegroundColorAttributeName];
NSString *someString = [NSString stringWithFormat:@"%@", @"S"];
[someString drawAtPoint:CGPointMake(rect.origin.x, rect.origin.y) withAttributes: stringAttributes];
NSLog(@"%@", someString);
CGContextRestoreGState(context);
}
@end
在我的根视图控制器中,我初始化了XYZSomeView
:
#import "XYZRootViewController.h"
#import "XYZSomeView.h"
@implementation XYZRootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
XYZSomeView *someView = [[XYZSomeView alloc] init];
[self.view addSubview:someView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
NSLog(@"%@", @"XYZRootViewController reveived memory warning");
}
@end
问题是 mydrawRect
没有被调用,是因为我必须自己调用它吗?我认为应该在初始化时调用此方法,而不必调用它。