Write text on image in objective-c iPhone, Font size of simulator different from on iPhone itself

巧了我就是萌 提交于 2019-12-23 04:57:26

问题


I'm having something really weird going, I'm adding caption to an image and everything works fine on simulator but on the device itself the font is really small...

I have a problem when using this code :

-(UIImage*) drawTextOnPic:(NSString*) bottomText
         inImage:(UIImage*)  image
{

UIFont *font = [UIFont boldSystemFontOfSize:48];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];

CGRect = CGRectMake(10.0f,10.0f,image.size.width-20.0f, image.size.height);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadow(context, CGSizeMake(2.5f, 2.5f), 5.0f);


[[UIColor whiteColor] set];
[bottomText drawInRect:CGRectIntegral(rect) withFont:font lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

Does someone know why it works only on simulator as I expected? BTW, I'm using the SDK for iOS6, Can it be something there? Is it something with the retina display? How to fix that?


回答1:


When you call UIGraphicsBeginImageContext, you should be using the extended call to pass in screen scale, like this:

UIGraphicsBeginImageContextWithOptions(image.size, YES, [[UIScreen mainScreen] scale]);

That will create the image context at the scale of your screen. Note that the second argument, here YES, is whether or not you need the image to be opaque. If there's transparency in your image, set this to NO.




回答2:


I choose the default font size (on my case it's 48) and then checked the image size according to some ref size (in my case 640x480), I did that by sqrtf(area(a)/area(ref)) then multiply this result on font size. That's the only way it works for me.

Hope that would help others...




回答3:


//  Drawing text on image
+ (UIImage*) drawText:(NSString*)text withColor:(UIColor *) textColor inImage:(UIImage*)image atPoint:(CGPoint)point {

    UIFont *font = [UIFont boldSystemFontOfSize:12];
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
    [textColor set];
    [text drawInRect:CGRectIntegral(rect) withFont:font];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}


来源:https://stackoverflow.com/questions/11298266/write-text-on-image-in-objective-c-iphone-font-size-of-simulator-different-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!