【发布时间】:2014-05-28 23:57:13
【问题描述】:
我现在感觉很愚蠢。
我正在尝试在 UIViewController 上绘制文本。
我正在使用 Xcode 5 并从一个简单的单页项目开始,除了将这段代码添加到 ViewController.m 之外什么也没做:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGRect myRect=CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
UIFont *myFont=[UIFont fontWithName:@"Helvetica" size:50];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
paragraphStyle.alignment = NSTextAlignmentRight;
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:myFont forKey:NSFontAttributeName];
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[attributes setObject:[NSNumber numberWithFloat:4] forKey:NSStrokeWidthAttributeName];
[attributes setObject:[UIColor whiteColor] forKey:NSStrokeColorAttributeName];
[@"test" drawInRect:myRect withAttributes:attributes];
// draw fill
[attributes removeObjectForKey:NSStrokeWidthAttributeName];
[attributes removeObjectForKey:NSStrokeColorAttributeName];
[attributes setObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
[@"test" drawInRect:myRect withAttributes:attributes];
}
它运行没有错误,但不产生任何文本 - 为什么?
【问题讨论】:
-
您是否只是将其作为学习练习(如果是这样对您有好处)?否则,您可以在视图中添加
UILabel并让它负责绘制您的文本。 -
是的,正在努力学习 - 我在使用 UILabel 裁剪 Descenders 字体时遇到了麻烦,所以我想看看我是否可以自己绘制文本并消除这些问题。
标签: ios objective-c uivewcontroller