0

I want to make a UIimage with DrawInRect with alignment text, multiple strings but each string with the alignment that i want.

i made this code but the final image doesn't have the alignment, i been searching and testing, and modifing the code, but i can't find the solution, please help me.

Thank You.

- (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;
}
  • Could you show what you expect, and what you have? – Larme May 09 '14 at 10:01
  • @Larme Ok, Thank you. Here is the image that i want to create [IMAGE LINK](https://www.dropbox.com/s/0k4halhqjbgxndx/photo.jpg) it has diferent alignment. The image that returns my code is this [IMAGE LINK](https://www.dropbox.com/s/oxjx2psqe4q621m/Result.png). Maybe the 'NSDictionary' with the alignment doesn't works in the 'DrawInRect' method. thanks. – Jose Garfias Lopez Parkinson May 09 '14 at 21:39
  • Check if this could help you: http://stackoverflow.com/questions/16737503/nsmutableattributedstring-add-different-alignments – Larme May 10 '14 at 12:06

0 Answers0