我現在感覺非常愚蠢。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