如何知道UILabel中显示的文字?

发布于 2024-09-15 18:20:34 字数 331 浏览 4 评论 0原文

我有一个包含两个 UILabels 的 UIView,用于显示字符串。 第一个 UILabel 有固定的大小,如果字符串太长,无法容纳在这个 UILabel 中,我想在第一个中显示最大字符数UILabel,并在第二个 UILabel 中显示字符串的其余部分。

但要做到这一点,我必须知道第一个 UILabel 中显示的字符串的确切部分,这并不容易,因为字符串和换行符的随机性。

那么,有没有办法只获取第一个 UILabel 中显示的文本,而无需截断字符串部分?

I have an UIView containing two UILabels, in order to display a string.
The first UILabel has a fixed size, and if the string is too long and can't hold in this UILabel, I want to display the maximum characters I can in the first UILabel, and display the rest of the string in the second UILabel.

But to make this, I must know the exact part of the string displayed in the first UILabel, which is not easy because of the randomness of the string and the linebreaks.

So, is there a way to get just the text displayed in the first UILabel, without the truncated part of the string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫七 2024-09-22 18:20:34
if ([_infoMedia.description length] > 270) {
        NSRange labelLimit = [_infoMedia.description rangeOfString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(270, (_infoMedia.description.length - 270))];
        _descTop.text = [_infoMedia.description substringToIndex:labelLimit.location];
        _descBottom.text = [_infoMedia.description substringFromIndex:(labelLimit.location+1)];
} else {
            _descTop.text = _infoMedia.description;
            _descBottom.text = @"";
}

好吧,这是一个迟到的答案,但也许它可以帮助某人。上面的代码大致是我在我的应用程序中使用的解决方案。

_descTop 是我的第一个标签,_descBottom 是第二个标签。 270 是一个常量,相当于略小于我的第一个标签 _descTop 中显示的平均最大字符数。我手动计算了它,尝试了许多不同的字符串,也许有更好的方法可以做到这一点,但这效果还不错。

如果我要显示的字符串 (_infoMedia.description) 大于 270 个字符,在 270 个字符限制的情况下,我将隔离前 270 个字符加上字符串中下一个单词的末尾(通过搜索下一个空格)会在单词中间切断字符串。然后我将字符串的第一部分放入第一个标签中,将第二部分放入第二个标签中。

如果没有,我只将字符串的全局性放在第一个标签中。

我知道这是一个蹩脚的解决方案,但它确实有效,而且我没有找到更好的方法来做到这一点。

if ([_infoMedia.description length] > 270) {
        NSRange labelLimit = [_infoMedia.description rangeOfString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(270, (_infoMedia.description.length - 270))];
        _descTop.text = [_infoMedia.description substringToIndex:labelLimit.location];
        _descBottom.text = [_infoMedia.description substringFromIndex:(labelLimit.location+1)];
} else {
            _descTop.text = _infoMedia.description;
            _descBottom.text = @"";
}

Okay that's a late answer but maybe it could help someone. The code above is approximatively the solution I used in my app.

_descTop is my first label and _descBottom is the second label. 270 is a constant equivalent to a little less than the average maximum number of characters displayed in my first label, _descTop. I calculated it by hand, trying with many different strings, maybe there's a better way to do that but this worked not bad.

If the string I want to display (_infoMedia.description) is larger than 270 characters, I isolate the first 270 characters plus the end of the next word in the string (by searching the next space), in the case where the 270 characters limit would cut the string in the middle of a word. Then I put the first part of the string in my first label, and the second part in the second label.

If not, I only put the globality of the string in the first label.

I know that's a crappy solution, but it worked and I didn't found any better way to do that.

来日方长 2024-09-22 18:20:34

以下代码可能会帮助您获得您想要的东西!

//If you want the string displayed in any given rect, use the following code..
@implementation NSString (displayedString)

//font- font of the text to be displayed
//size - Size in which we are displaying the text

-(NSString *) displayedString:(CGSize)size font:(UIFont *)font
{
NSString *written = @"";

int i = 0;
int currentWidth = 0;
NSString *nextSetOfString = @"";

while (1)
{
    NSRange range;
    range.location = i;
    range.length = 1;

    NSString *nextChar = [self substringWithRange:range];
    nextSetOfString = [nextSetOfString stringByAppendingString:nextChar];

    CGSize requiredSize = [nextSetOfString sizeWithFont:font constrainedToSize:CGSizeMake(NSIntegerMax, NSIntegerMax)];
    currentWidth = requiredSize.width;

    if(size.width >= currentWidth && size.height >= requiredSize.height)
    {
        written = [written stringByAppendingString:nextChar];
    }
    else
    {
        break;
    }
    i++;
}


    return written;
}

@end

Following code might help you in getting what you want!!

//If you want the string displayed in any given rect, use the following code..
@implementation NSString (displayedString)

//font- font of the text to be displayed
//size - Size in which we are displaying the text

-(NSString *) displayedString:(CGSize)size font:(UIFont *)font
{
NSString *written = @"";

int i = 0;
int currentWidth = 0;
NSString *nextSetOfString = @"";

while (1)
{
    NSRange range;
    range.location = i;
    range.length = 1;

    NSString *nextChar = [self substringWithRange:range];
    nextSetOfString = [nextSetOfString stringByAppendingString:nextChar];

    CGSize requiredSize = [nextSetOfString sizeWithFont:font constrainedToSize:CGSizeMake(NSIntegerMax, NSIntegerMax)];
    currentWidth = requiredSize.width;

    if(size.width >= currentWidth && size.height >= requiredSize.height)
    {
        written = [written stringByAppendingString:nextChar];
    }
    else
    {
        break;
    }
    i++;
}


    return written;
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文