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];
文字显示在右边而不是中心,很奇怪
答案 0 :(得分: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 :(得分:1)
NSMutableParagraphStyle *styleCenter = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
styleCenter.alignment = NSCenterTextAlignment;
NSDictionary *attributes = @{NSFontAttributeName:txtFont,NSForegroundColorAttributeName:txtColor,NSParagraphStyleAttributeName:styleCenter};
[txt drawInRect:rect withAttributes:attributes];
答案 2 :(得分:0)
NSString
上的此类别将使用以下给定的字体在rect中绘制居中文本:
@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];
}
答案 3 :(得分: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];
}