我现在感觉非常愚蠢。iOS如何在UIViewController中绘制文字
我想绘制文本到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',让它处理你的文字。 –
是的,试图学习 - 我已经遇到了UILabels将字体下降到字体上的麻烦,所以我想我会看看是否可以自己绘制文本并消除问题。 – wayneh