如何使NSStringDrawingContext缩小文本? [英] How to make NSStringDrawingContext shrink text?

查看:132
本文介绍了如何使NSStringDrawingContext缩小文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用iOS 6的属性字符串API来计算文本大小,并在必要时缩小字体大小。但是,正如文档所说,我无法让它工作。

I'm trying to use the attributed string API of iOS 6 to calculate the size of text and shrink the font size if necessary. However, I can't get it to work as the documentation says.

NSString *string = @"This is a long text that doesn't shrink as it should";

NSStringDrawingContext *context = [NSStringDrawingContext new];
context.minimumScaleFactor = 0.5;

UIFont *font = [UIFont fontWithName:@"SourceSansPro-Bold" size:32.f];

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineBreakMode = NSLineBreakByClipping;

NSDictionary *attributes = @{ NSFontAttributeName: font,
                              NSParagraphStyleAttributeName: paragraphStyle };

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.title attributes:attributes];

CGRect rect = [attributedString boundingRectWithSize:CGSizeMake(512.f, 512.f) options:NSStringDrawingUsesLineFragmentOrigin context:context];

NSLog(@"rect: %@, context: %@", NSStringFromCGRect(rect), context.debugDescription);

但文本不缩小并被截断。 actualScaleFactor 总是 1 。日志结果为:

But the text doesn't shrink and is truncated. actualScaleFactor is always 1. The log results are:

rect:{{0, 0}, {431.64801, 80.447998}}, context:<NSStringDrawingContext: 0x14e85770> minimumScaleFactor:0.500000 minimumTrackingAdjustment:0.000000 actualScaleFactor:1.000000 actualTrackingAdjustment:0.000000 totalBounds:{{0, 0}, {431.64801, 80.447998}}

如果我使用实际的绘图方法而不是测量方法,结果是一样的。如果我删除段落样式,它会使文本换行并且不会缩小它。如果我删除段落样式并且我选择仅允许一行文本的大小,则文本也会被截断而不是缩小。哪里不对?处理 NSStringDrawingContext 的文档或在线资源非常少。我试图避免使用 sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:在iOS 7中不推荐使用。

The result is the same if I use the actual drawing method and not the measuring method. If I remove the paragraph style, it makes the text wrap and doesn't shrink it. If I remove the paragraph style AND I choose a size that only allows one line of text, the text is truncated too instead of being shrunk. What is wrong? There is very little documentation or online resources dealing with NSStringDrawingContext. And I'm trying to avoid the use of sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: which is deprecated in iOS 7.

推荐答案

NSStringDrawingContext的 minimumScaleFactor 在iOS 7中似乎已损坏。

NSStringDrawingContext's minimumScaleFactor appears to broken in iOS 7.

据我所知,即使在iOS 6中,它也不适用于绘图;它适用于测量,因此您可以计算出在 工作的上下文中会发生什么,比如UILabel。这样,您就知道标签的正确最小高度。

As far as I can make out, even in iOS 6, it didn't work for drawing; it worked for measuring, so you could work out what would happen in a context where it does work for drawing, like a UILabel. That way, you know the correct minimum height for the label.

或者,您可以使用生成的比例因子自行缩小文本,因为现在知道适合。

Or, you could use the resulting scale factor to shrink the text yourself, in the knowledge that now it will fit.

示例:

- (void)drawRect:(CGRect)rect
{
    // rect is 0,0,210,31

    NSMutableAttributedString* s = 
        [[NSMutableAttributedString alloc] initWithString: @"This is the army Mister Jones."];
    [s addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:20]} 
        range:NSMakeRange(0,s.length)];

    NSMutableParagraphStyle* para = [[NSMutableParagraphStyle alloc] init];
    para.lineBreakMode = NSLineBreakByTruncatingTail;
    [s addAttributes:@{NSParagraphStyleAttributeName:para} 
        range:NSMakeRange(0,s.length)];

    NSStringDrawingContext* con = [[NSStringDrawingContext alloc] init];
    con.minimumScaleFactor = 0.5;

    CGRect result = 
        [s boundingRectWithSize:rect.size 
            options:NSStringDrawingUsesLineFragmentOrigin context:con];
    CGFloat scale = con.actualScaleFactor;
    // ...(could have a check here to see if result fits in target rect)...

    // fix font to use scale factor, and draw
    [s addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:20*scale]} 
        range:NSMakeRange(0,s.length)];
    [s drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin context:nil];

}

在iOS 6中, scale 大约是0.85,您可以使用它来缩小文本。但是在iOS 7中, scale 保持为1,表明没有发生收缩,NSStringDrawingContext的这个功能现在没用了。我不知道这是一个错误,还是故意放弃了该功能。

In iOS 6, scale is about 0.85 and you can use it as shown to shrink the text. But in iOS 7, scale remains at 1, suggesting that no shrinkage is happening and this feature of NSStringDrawingContext is now useless. I can't tell whether that's a bug or whether the feature has been deliberately abandoned.

这篇关于如何使NSStringDrawingContext缩小文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆