< IOS开发 >使用CGContextRef绘制文字时的设置
NSString *str = @"hello"; //字体 UIFont *font = [UIFont systemFontOfSize:16.0]; //文本风格,设置居中 NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; [paragraphStyle setAlignment:NSTextAlignmentCenter]; //文本位置 CGRect iRect = CGRectMake(0, 0, 100, 20); //文本颜色 UIColor *color = [UIColor whiteColor]; //打印文本 [str drawInRect:rect withAttributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:color}];
注意drawInRect:withAttributes函数在IOS7之后才能使用