iphone - 如何在 iOS 中渲染拉伸(stretch)文本?

标签 iphone ios fonts core-graphics uilabel

给定一个矩形区域,我想使用特定字体渲染一些文本,并让渲染的文本填充矩形。如下图所示:

enter image description here

  1. 这与仅仅改变字体大小不同
  2. 将它渲染为位图然后缩放它不是一个选项(它看起来很糟糕)
  3. 矢量图是做到这一点的方式

解决方案

我想出了以下似乎适合我的目的。该代码绘制一行文本缩放以填充边界。子类 UIView 并替换 drawRect 如下。

- (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;
}

示例用法:

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

最佳答案

您可以设置 UILabel 变换属性并缩放宽度:

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

关于iphone - 如何在 iOS 中渲染拉伸(stretch)文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5250442/

相关文章:

iphone - 推送通知证书问题

iphone - 为 iOS 设计线程评论系统

iOS:使用 UIAppearance 定义自定义 UITableViewCell 颜色

ios - #define 或 const 字符串*

javascript - 如何从javascript指定字体?

Android Studio TextView fontFamily 在主题中不起作用

iphone 项目 - 在 xcode 4.3 中复制并重命名

iPhone MPMoviePlayerController : download files while streaming en play them locally

ios - 一段时间后如何显示 UIAlertView?

html - CSS 覆盖 'font-weight' 属性