我正在使用下面提到的代码向图像添加文本。但面临的问题是颜色和字体大小没有变化。我试图改变字体大小,没有发生,也不知道如何改变颜色。
-(UIImage *)imageFromText:(NSString *)text
{
// set the font type and size
//UIFont *font = [UIFont systemFontOfSize:txtView.font];
//CGSize size = [text sizeWithFont:[UIFont fontWithName:@"Arial" size:13.0f]]; // label or textview
NSDictionary *attributes = @{
NSFontAttributeName: [UIFont fontWithName:@"Arial" size:14]
};
CGSize size = [text sizeWithAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:14]
}];
// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
else
// iOS is < 4.0
UIGraphicsBeginImageContext(size);
//[text drawInRect:CGRectMake(0,0,size.width,size.height) withFont:[UIFont fontWithName:@"Arial" size:15.0f]];
CGRect drawRect = CGRectMake(0.0, 0.0, size.width,size.height);
NSStringDrawingContext *drawingContext = [[NSStringDrawingContext alloc] init];
drawingContext.minimumScaleFactor = 1.5;
[text drawWithRect:drawRect options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:drawingContext];
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}
请指导,为什么字体大小不变,代码中有什么问题nad如何更改此文本的颜色。
答案 0 :(得分:1)
最后我得到了自己。
[theText drawAtPoint:CGPointMake(x, y)
withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:8], NSForegroundColorAttributeName:[UIColor whiteColor] }];