iOS UILabel自动收缩所以单词不会截断为两行 [英] iOS UILabel autoshrink so word doesn't truncate to two lines

查看:392
本文介绍了iOS UILabel自动收缩所以单词不会截断为两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让UILabel缩小,以便单词不会截断到下一行。不只是在文本区域的末尾截断文本。

I'm trying to have the UILabel shrink so that words don't truncate to the next line. Not just text truncating at the end of the text area.

如果我有一个50x100的盒子,我想在盒子里加入类似American的东西在25.0点,我最终得到:

If I have a box that is 50x100, and I want to put in something like "American" in the box at 25.0pt, I end up getting:

   50px
 -------
|Ameri- |
|can    |
|Beauty | 100px
|       |
 -------

文字收缩似乎没有做任何事情在这种情况下,因为它仍然适合UILabel框架。当文本非常像Willie Wonka的巧克力工厂时,它的效果非常好,但我不希望断字。

The text shrinking doesn't seem to do anything during this situation, since it still fits in the UILabel frame. It works pretty well when the text is really long like "Willie Wonka's Chocolate Factory", but I don't want word truncation.

这是该场景中的理想输出:

This is the ideal output in that scenario:

    50px
 --------
[American|
|Beauty  | 100px
|        |
|        |
|        |
 --------

任何建议都是超级适用的!

Any suggestions would be super appreicated!

编辑:解决方案

这要归功于我的建议答案如下。它很棒!

Here is what I ended up doing thanks to the suggestion in the answer below. It works great!

- (CGFloat) calculateFromUILabel:(UILabel *)label
{
    NSString *stringToMeasure = label.text;
    NSLog(@"FontSizeMeasurement.calculateFromUILabel() %@", stringToMeasure);

    NSRange range = NSMakeRange(0, 1);
    NSAttributedString *attributedString = label.attributedText;
    NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:&range];

    NSMutableCharacterSet *characterSet = [[NSCharacterSet whitespaceAndNewlineCharacterSet] mutableCopy];
    [characterSet addCharactersInString:@"-"];
    NSArray *words = [stringToMeasure componentsSeparatedByCharactersInSet:characterSet];

    CGSize maxSize = CGSizeZero;
    NSMutableAttributedString *maxWidthString = nil;
    for(int i = 0; i < words.count; i++) {
        NSString *word = words[i];
        CGSize wordSize = [word sizeWithAttributes:attributes];
        if(wordSize.width > maxSize.width) {
            maxSize = wordSize;
            maxWidthString = [[NSMutableAttributedString alloc] initWithString:word attributes:attributes];
        }
    }

    UIFont *font = [label.font copy];
    while(maxSize.width > self.maxWidth) {
        font = [font fontWithSize:(font.pointSize-0.1)];
        [maxWidthString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, maxWidthString.length)];
        maxSize = [maxWidthString size];
    }

    return font.pointSize;
}


推荐答案

我什么也想不到直接内置。所以我建议:

I can think of nothing that's directly built in. So I'd suggest:

通过 [NSCharacterSet + whitespaceAndNewlineCharacterSet] 将字符串拆分为组件 [NSString -componentsSeparatedByCharactersInSet:] 。我考虑推荐更高级别的 NSLinguisticTagger 来获取整个单词,但这样就不允许像结尾的单词这样的内容。

Split the string into components by [NSCharacterSet +whitespaceAndNewlineCharacterSet] and [NSString -componentsSeparatedByCharactersInSet:]. I considered recommending the higher-level NSLinguisticTagger to get out whole words but that wouldn't allow for things like words with a colon on the end.

在这些词中,使用UIKit添加找到排版最大的 NSString -sizeWithAttributes:(在iOS 7下)或 - sizeWithFont:(6岁或以下)。当你缩小字体大小时,你会假设最大值仍然是最大的,我认为这总是正确的,因为Apple没有做出激进的字体提示。

Of those words, find the typographically largest using the UIKit addition NSString -sizeWithAttributes: (under iOS 7) or -sizeWithFont: (under 6 or below). You're going to make the assumption that the largest will remain the largest as you scale the font size down, which I think will always be true because Apple doesn't do aggressive font hinting.

如果这个词的宽度已经远小于你的视图宽度,那么你就完成了。只显示字符串。

If that word is already less wide than your view's width, you're done. Just show the string.

否则使用快速二进制搜索,反复查询大小,直到找到所需的较小字体大小,在您认为合适的精度范围内(0.1分对我来说听起来很合理,但你明白了)。然后以该大小显示整个字符串。

Otherwise use a quick binary search, repeatedly querying the size, until you find the smaller font size that you need to within whatever precision you think is appropriate (0.1 of a point sounds reasonable to me but you get the point). Then show the whole string at that size.

这篇关于iOS UILabel自动收缩所以单词不会截断为两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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