Pregunta

He intentado mostrar el texto usando un contexto de cuarzo, pero no importa lo que haya intentado, simplemente no he tenido la suerte de mostrar el texto (aunque puedo mostrar todo tipo de objetos de cuarzo ). ¿Alguien sabe qué podría estar haciendo mal?

ejemplo:

-(void)drawRect:(CGRect)rect
{   
  // Drawing code
  CGContextRef  context = UIGraphicsGetCurrentContext();
  CGContextSelectFont(context, "Arial", 24, kCGEncodingFontSpecific);
  CGContextSetTextPosition(context,80,80);
  CGContextShowText(context, "hello", 6);
  //not even this works
  CGContextShowTextAtPoint(context, 1,1, "hello", 6);
}    
¿Fue útil?

Solución

Aquí hay un fragmento de código que estoy usando.

UIColor *mainTextColor = [UIColor whiteColor];
[mainTextColor set];
drawTextLjust(@"Sample Text", 8, 50, 185, 18, 16);

Y:

static void drawTextLjust(NSString* text, CGFloat y, CGFloat left, CGFloat right,
                          int maxFontSize, int minFontSize) {
    CGPoint point = CGPointMake(left, y);
    UIFont *font = [UIFont systemFontOfSize:maxFontSize];
    [text drawAtPoint:point forWidth:right - left withFont:font
       minFontSize:minFontSize actualFontSize:NULL
       lineBreakMode:UILineBreakModeTailTruncation
       baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
}

Otros consejos

Está bien, lo tengo. En primer lugar, cambie el modo de codificación a kCGEncodingMacRoman. En segundo lugar, inserte esta línea debajo de ella:

CGContextSetTextMatrix(canvas, CGAffineTransformMake(1, 0, 0, -1, 0, 0));

Esto establece la matriz de conversión para el texto para que se dibuje correctamente. Si no pones esa línea, tu texto estará al revés y al revés. No tengo idea de por qué esto no era el predeterminado. Por último, asegúrese de haber configurado el color de relleno correcto. Es un error fácil de cometer si olvida cambiar el color del fondo al color del texto y termina con el texto blanco sobre blanco.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top