La cadena atribuida tiene solo un atributo: fuente Helvetica NeueUI de 17 puntos, que abarca toda la cadena. La línea 1 ~ 3 es puramente inglesa, la línea 4 ~ 6 son mezclas de inglés y chino, la línea 7 ~ 8 son puramente chinas.Espaciado entre líneas en el diseño de varios idiomas con Core Text
Luego se coloca con CTFramesetter y el marco resultante se dibuja con CTFrameDraw.
// UIView subclass
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica NeueUI"), 17.f, NULL);
_text = [[NSAttributedString alloc] initWithString:string attributes:
[NSDictionary dictionaryWithObject:(id)font forKey:(id)kCTFontAttributeName]];
CFRelease(font);
_framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)_text);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds);
_frame = CTFramesetterCreateFrame(_framesetter, CFRangeMake(0, 0), path, NULL);
CGPathRelease(path);
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
// Flip the coordinate system.
CGContextTranslateCTM(context, 0.f, self.bounds.size.height);
CGContextScaleCTM(context, 1.f, -1.f);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CTFrameDraw(_frame, context);
CGContextRestoreGState(context);
}
El problema es que el espacio entre la línea 7 y 8 (líneas puramente chinas) es mucho más pequeño que otros.
Por el contrario, pongo un UILabel
debajo de él, con la misma fuente y texto. Ignorando la diferencia de representación de los caracteres individuales, puede ver que los espacios de línea en UILabel
son uniformes, incluido el último entre dos líneas chinas.
Aviso: esto da como resultado anterior se puso en un dispositivo real. Si ejecuta el mismo código en el simulador, obtendrá resultados muy diferentes para la vista de texto principal: los espacios entre las líneas que contienen chino son mucho más grandes que otros.
- ¿Por qué el espacio entre líneas chinas es más pequeño (más grande)?
- ¿Cómo puedo diseñar texto en varios idiomas con una altura de línea uniforme usando Core Text?
PD: aquí está el sample project puede probarse usted mismo.
Si no le importa, me gustaría ver su respuesta de iOS 5 en los foros de desarrolladores (hasta que pueda publicarla aquí). –
ESPERA ENTREGAR PARA ENTREGAR. – mxcl