uiappfonts font custom iphone ios fonts core-graphics uilabel

iphone - uiappfonts - swift 4 custom font



¿Cómo renderizar texto estirado en iOS? (3)

Podrías probar CoreText. Obtenga un CTFramesetter , calcule su rect, luego calcule la transformación afín necesaria para comprimir esa rect en los límites que desee y configúrela como CTM. Luego, cuando dibuja el texto, debe estirarlo apropiadamente con calidad total.

Dado un área rectangular, quiero renderizar un texto usando una fuente específica y hacer que el texto renderizado complete el rectángulo . Como en la imagen de abajo:

  1. Esto no es lo mismo que simplemente cambiar el tamaño de la fuente
  2. Presentarlo como un mapa de bits y luego escalarlo no es una opción (se ve horrible)
  3. Los gráficos vectoriales son la forma de hacerlo

Solución

Se me ocurrió lo siguiente que parece funcionar para mis propósitos. El código dibuja una sola línea de escalado de texto para completar los límites. La subclase UIView y reemplaza drawRect siguiente manera.

- (void)drawRect:(CGRect)rect { [self drawScaledString:@"Abcde"]; } - (void)drawScaledString:(NSString *)string { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetTextMatrix(context, CGAffineTransformIdentity); NSAttributedString *attrString = [self generateAttributedString:string]; CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attrString, CFRangeMake(0, string.length), kCTForegroundColorAttributeName, [UIColor redColor].CGColor); CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef) attrString); // CTLineGetTypographicBounds doesn''t give correct values, // using GetImageBounds instead CGRect imageBounds = CTLineGetImageBounds(line, context); CGFloat width = imageBounds.size.width; CGFloat height = imageBounds.size.height; CGFloat padding = 0; width += padding; height += padding; float sx = self.bounds.size.width / width; float sy = self.bounds.size.height / height; CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextTranslateCTM(context, 1, self.bounds.size.height); CGContextScaleCTM(context, 1, -1); CGContextScaleCTM(context, sx, sy); CGContextSetTextPosition(context, -imageBounds.origin.x + padding/2, -imageBounds.origin.y + padding/2); CTLineDraw(line, context); CFRelease(line); } - (NSAttributedString *)generateAttributedString:(NSString *)string { CTFontRef helv = CTFontCreateWithName(CFSTR("Helvetica-Bold"),20, NULL); CGColorRef color = [UIColor blackColor].CGColor; NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys: (id)helv, (NSString *)kCTFontAttributeName, color, (NSString *)kCTForegroundColorAttributeName, nil]; NSAttributedString *attrString = [[[NSMutableAttributedString alloc] initWithString:string attributes:attributesDict] autorelease]; return attrString; }

Ejemplo de uso:

CGRect rect = CGRectMake(0, 0, 50, 280); MyCTLabel *label = [[MyCTLabel alloc] initWithFrame:rect]; label.backgroundColor = [UIColor whiteColor]; [self addSubview:label];


Puede establecer la propiedad de transformación UILabel y escalar el ancho:

[myLabel sizeToFit]; myLabel.transform = CGAffineTransformMakeScale(0.5, 1.0);


también puedes probar con la @property(nonatomic) BOOL adjustsFontSizeToFitWidth y @property minimumFontSize

Inicialmente puede establecer un valor mucho más alto para la propiedad de la fuente y también inicializar el minimumFontSize con un valor de fuente mínimo.