如何将文本居中设置在UIImage上?

3
 UIGraphicsBeginImageContext(targetSize);
NSString *txt = @"my String";
UIColor *txtColor = [UIColor whiteColor];
UIFont *txtFont = [UIFont systemFontOfSize:30];
NSDictionary *attributes = @{NSFontAttributeName:txtFont,NSForegroundColorAttributeName:txtColor};
[txt drawAtPoint:CGPointMake(0, 0) withAttributes:attributes];

UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我不知道如何修复它以使文本居中。

//编辑

我添加了您的设备,但没有成功。

  NSMutableParagraphStyle *styleCenter = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
styleCenter.alignment = kCTCenterTextAlignment;
NSDictionary *attributes = @{NSFontAttributeName:txtFont,NSForegroundColorAttributeName:txtColor,NSParagraphStyleAttributeName:styleCenter};
[txt drawInRect:CGRectMake(0, 0, targetSize.width, targetSize.height) withAttributes:attributes];

右侧显示的文本不居中,感觉很怪异

4个回答

2

试试这个:

UIGraphicsBeginImageContext(targetSize);
NSString *txt = @"my String";
UIColor *txtColor = [UIColor whiteColor];
UIFont *txtFont = [UIFont systemFontOfSize:30];
NSDictionary *attributes = @{NSFontAttributeName:txtFont, NSForegroundColorAttributeName:txtColor};

CGRect txtRect = [txt boundingRectWithSize:CGSizeZero
                                   options:NSStringDrawingUsesLineFragmentOrigin
                                attributes:attributes
                                   context:nil];

[txt drawAtPoint:CGPointMake(targetSize.width/2 - txtRect.size.width/2, targetSize.height/2 - txtRect.size.height/2) withAttributes:attributes];
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

1
NSMutableParagraphStyle *styleCenter = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
styleCenter.alignment = NSCenterTextAlignment;
NSDictionary *attributes = @{NSFontAttributeName:txtFont,NSForegroundColorAttributeName:txtColor,NSParagraphStyleAttributeName:styleCenter};
[txt drawInRect:rect withAttributes:attributes];

0

这个关于NSString的类别将使用下面给定的字体在矩形中绘制居中文本:

@implementation NSString (Helpers)

- (void)drawCentredInRect:(CGRect)rect withFont:(nullable UIFont *)font andForegroundColor:(nullable UIColor *)foregroundColor {
    NSMutableDictionary *attributes = [NSMutableDictionary new];

    if (font) {
      attributes[NSFontAttributeName] = font;
    }

    if (foregroundColor) {
      attributes[NSForegroundColorAttributeName] = foregroundColor;
    }

    CGSize textSize = [self sizeWithAttributes:attributes];
    CGPoint drawPoint = CGPointMake(
            CGRectGetMidX(rect) - (int) (textSize.width / 2),
            CGRectGetMidY(rect) - (int) (textSize.height / 2)
    );

    [self drawAtPoint:drawPoint withAttributes:attributes];
}

0

我尝试了你聪明问题的答案,现在我找到了解决方案。

+(UIImage*) drawText:(NSString*)text  inImage:(UIImage*)image  atPoint:(CGPoint)point
{

  UIGraphicsBeginImageContextWithOptions(image.size, YES, 0.0f);
  [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
  CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
  [[UIColor whiteColor] set];
  if([text respondsToSelector:@selector(drawInRect:withAttributes:)])
  {
    //iOS 7 or above iOS 7
    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    style.alignment = NSTextAlignmentCenter;
    NSDictionary *attr = [NSDictionary dictionaryWithObject:style forKey:NSParagraphStyleAttributeName];
    [text drawInRect:rect withAttributes:attr];
  }
  UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return newImage;
}

请在选择图片的位置调用上述方法,如下所示。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   UIImage *imagePicked = info[UIImagePickerControllerEditedImage];
   picker.delegate = self;
   self.imageViewTextCenter.image = [ViewController drawText:@"WELCOME"
                                              inImage:imagePicked
                                              atPoint:CGPointMake(0, 0)];;
   [picker dismissViewControllerAnimated:YES completion:nil];

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接