Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to render stretched text in iOS?
    primarykey
    data
    text
    <p>Given a rectangular area, I want to render some text using a specific font and have the rendered text <em>fill out the rectangle</em>. As in the image below:</p> <p><img src="https://i.stack.imgur.com/Sm2hq.png" alt="enter image description here"></p> <ol> <li>This is not the same as just changing font size</li> <li>Rendering it as a bitmap and then scale it is not an option (it looks horrible)</li> <li>Vector graphics is the way to do it</li> </ol> <h2>Solution</h2> <p>I came up with the following which seems to work for my purposes. The code draws a single line of text scaling to fill the bounds. Subclass <code>UIView</code> and replace <code>drawRect</code> as follows. </p> <pre><code>- (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; } </code></pre> <p>Example usage:</p> <pre><code>CGRect rect = CGRectMake(0, 0, 50, 280); MyCTLabel *label = [[MyCTLabel alloc] initWithFrame:rect]; label.backgroundColor = [UIColor whiteColor]; [self addSubview:label]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload