我想用UIimage
和DrawInRect
制作对齐文字,多个字符串,但每个字符串都有我想要的对齐方式。
我制作了这段代码,但最终的图片没有对齐,我一直在搜索和测试,并修改了代码,但我找不到解决方案,请帮助我。
谢谢。
- (IBAction)DrawImage:(id)sender
{
UIImage *Imagen=[[UIImage alloc]init]; //imagen a retornar:
int TamanoFuente = 30;
//Estilo de fuente y tamaño:
UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:TamanoFuente];
NSString *fontString=[NSString stringWithFormat:@"Helvetica-Bold"];
//size of image:
CGSize size = CGSizeMake(875, 10000);
//Dictionarys with formats:
NSMutableParagraphStyle * EstiloCentrado = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[EstiloCentrado setAlignment:NSTextAlignmentCenter];
[EstiloCentrado setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary * Centrado = [[NSDictionary alloc] initWithObjectsAndKeys:font, NSFontAttributeName,EstiloCentrado,NSParagraphStyleAttributeName,nil];
NSMutableParagraphStyle * EstiloIzquierda = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[EstiloIzquierda setAlignment:NSTextAlignmentLeft];
[EstiloIzquierda setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary * Izquierda = [[NSDictionary alloc] initWithObjectsAndKeys:font, NSFontAttributeName,EstiloIzquierda,NSParagraphStyleAttributeName,nil];
NSMutableParagraphStyle * EstiloDerecha = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[EstiloCentrado setAlignment:NSTextAlignmentRight];
[EstiloCentrado setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary * Derecha = [[NSDictionary alloc] initWithObjectsAndKeys:font, NSFontAttributeName,EstiloDerecha,NSParagraphStyleAttributeName,nil];
//all text:
NSMutableString *CadenaTotal = [[NSMutableString alloc]init];
NSString *Encabezado = @"Lavautos En Un 2x3";
NSString *TicketNumber = [NSString stringWithFormat:@"Numero de Ticket: 5"];
NSString *FechaTicket = [NSString stringWithFormat:@"Fecha: %@\n",[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle]];
[CadenaTotal appendString:Encabezado];
[CadenaTotal appendString:TicketNumber];
[CadenaTotal appendString:FechaTicket];
//total size:
CGRect total = [CadenaTotal boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{
NSFontAttributeName:[UIFont fontWithName:fontString size:TamanoFuente],
}
context:nil];
CGSize messuredtotal = total.size;
//sizes:
CGSize EncaSize = [self CreaTam:Encabezado FontSize:TamanoFuente FontType:fontString Tamano:size];
CGSize TicketNSize = [self CreaTam:TicketNumber FontSize:TamanoFuente FontType:fontString Tamano:size];
CGSize FechaTSize = [self CreaTam:FechaTicket FontSize:TamanoFuente FontType:fontString Tamano:size];
//start:
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
if ([[UIScreen mainScreen] scale] == 2.0) { //Retina
UIGraphicsBeginImageContextWithOptions(messuredtotal, NO, 1.0);
} else { //Non Retina
UIGraphicsBeginImageContext(messuredtotal);
}
} else {
UIGraphicsBeginImageContext(messuredtotal);
}
//Draw:
CGRect RectoEnca = CGRectMake(0, 0, EncaSize.width, EncaSize.height);
[Encabezado drawInRect:RectoEnca withAttributes:Centrado];
CGRect TicketNRect = CGRectMake(0, EncaSize.height, TicketNSize.width, TicketNSize.height );
[TicketNumber drawInRect:TicketNRect withAttributes:Centrado];
CGRect FechaTRect = CGRectMake(0,EncaSize.height + TicketNSize.height, FechaTSize.width, FechaTSize.height);
[FechaTicket drawInRect:FechaTRect withAttributes:Derecha];
//Save image
Imagen = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//return IMAGEN:
[ImagenView setImage:Imagen];
}
-(CGSize)CreaTam:(NSString *)Cadena FontSize:(int)Fontsize FontType:(NSString*)font Tamano:(CGSize)Tam
{
CGRect RECT = [Cadena boundingRectWithSize:Tam
options:NSStringDrawingUsesDeviceMetrics
attributes:@{
NSFontAttributeName:[UIFont fontWithName:font size:Fontsize],
}
context:nil];
return RECT.size;
}