2010-04-16 15 views
5

Tengo que dibujar texto subrayado-multilínea con todos los tipos de alineación de texto. He buscado en los foros y consiguió algunos resultados como:Dibujar texto subrayado/tachado (MULTILINE STRING)?

http://davidjhinson.wordpress.com/2009/11/26/underline-text-on-the-iphone/

http://forums.macrumors.com/showthread.php?t=561572

Pero todo dibujar texto por sólo una sola línea. mientras tengo texto de varias líneas. La situación incluso empeora cuando la alineación del texto está centrada. Busqué y encontré que en iphone-sdk-3.2 hay algunos atributos de texto central para subrayar un texto, pero no tengo idea de cómo usarlo. Además, si los uso, mi problema no se resolverá por completo.

Como tengo que dibujar texto tachado también.

Cualquiera que tenga una idea sobre esto, por favor ayuda.

Respuesta

2

¿Qué tal esto. Esto funciona bien para mí, ¿qué piensas, si esto requiere alguna optimización?

CGContextSetFillColorWithColor(c, [[UIColor blackColor] CGColor]);   //THE TEXT COLOR OF THE STRING TO BE DRAWN. 

      UIFont *fontName = [UIFont fontWithStyle:@"Arial-BoldMT" andFontSize:13]; 

      if(FontOverLayUnderLine || FontOverLayStrikeThrough){ 

       NSArray *stringsArray = [textString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; 
       CGContextSetStrokeColorWithColor(c, [appDel.textDisplayStyle.fillColor CGColor]); 
       CGContextSetLineWidth(c, 1.0); 
       CGSize stringSize; 
       NSString *stringToDraw; 
       for (int i = 0 ; i < [stringsArray count]; i++) { 

        stringToDraw = [stringsArray objectAtIndex:i]; 

        if(![[stringToDraw stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""]){ 

         stringSize = [stringToDraw sizeWithFont:fontName forWidth:rect.size.width lineBreakMode:UILineBreakModeCharacterWrap]; 

         float x = rect.origin.x + (rect.size.width-stringSize.width)/2 + 7; 
         float y = 4 + stringSize.height*i; 

         [stringToDraw drawAtPoint:CGPointMake(x, y) forWidth:stringSize.width withFont:fontName lineBreakMode:UILineBreakModeCharacterWrap]; 

         if(FontOverLayUnderLine) 
          y += (1.05) * stringSize.height*i +1; 
         else 
          y += (stringSize.height/2)+1; 

         CGContextMoveToPoint(c, x, y); 
         CGContextAddLineToPoint(c, x+stringSize.width, y); 
         CGContextStrokePath(c); 
        } 
       } 

      } 

Espero que esto funcione bien para todos.

Gracias,

Madhup

+0

Hola Madhup, esto no funciona para mí. Por favor, ¿pueden ayudarme con el subrayado de texto de líneas múltiples en un UILabel? Sería genial si puedes hacerlo. –

+0

¿Qué se puede hacer? Por favor ayudame con esto. –

+0

@Parth \ Bhatt el código de arriba es para texto escrito. Debe tratar de cambiar el marco de la línea un poco para obtener el texto subrayado. Por favor vea el primer enlace en mi pregunta. –

1

Usar texto principal.

He buscado y he encontrado que en iphone-sdk-3.2 hay algunos atributos de texto central para subrayar un texto, pero no tengo idea de cómo usarlo.

Eso es lo que the documentation es para.