简体   繁体   English

在Objective-C iPhone中将文字写在图像上,模拟器的字体大小与iPhone本身不同

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

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? 顺便说一句,我正在使用iOS6的SDK,那里有东西吗? Is it something with the retina display? 视网膜显示器有问题吗? How to fix that? 如何解决?

When you call UIGraphicsBeginImageContext , you should be using the extended call to pass in screen scale, like this: 调用UIGraphicsBeginImageContext ,应使用扩展调用传递屏幕比例,如下所示:

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. 请注意,第二个参数(这里为YES )是您是否需要使图像不透明。 If there's transparency in your image, set this to NO . 如果图像中有透明度,请将其设置为NO

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. 我选择默认字体大小(在我的情况下为48),然后根据一些引用大小(在我的情况下为640x480)检查图像大小,我用sqrtf(area(a)/ area(ref))进行了此操作,然后将其乘以字体大小的结果。 That's the only way it works for me. 那是对我有用的唯一方法。

Hope that would help others... 希望对别人有帮助...

//  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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM