UILabel可见部分文字 [英] UILabel visible part of text

查看:70
本文介绍了UILabel可见部分文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在单词包装 UILabel 中获取文本的可见部分?我的意思是完全是最后一个可见字符?

Is there a way to get the visible part of text in word wrapped UILabel? I mean exactly the last visible character?

我想制作两个标签围绕图像,并希望继续第一个标签上的矩形文本第二个。

I'd like to make two labels rounding the image and would like to continue the text which was out of rect for first label on the second one.

我知道 [NSString sizeWithFont ...] 但有一些像<$ c一样的逆转$ c> [NSString stringVisibleInRect:withFont:...] ? : - )

I know [NSString sizeWithFont...] but are there something reversing like [NSString stringVisibleInRect: withFont:...] ? :-)

提前谢谢。

推荐答案

你可以使用类别来扩展NSString并创建你提到的方法

You could use a category to extend NSString and create the method you mention

@interface NSString (visibleText)

- (NSString*)stringVisibleInRect:(CGRect)rect withFont:(UIFont*)font;

@end

@implementation NSString (visibleText)

- (NSString*)stringVisibleInRect:(CGRect)rect withFont:(UIFont*)font
{
    NSString *visibleString = @"";
    for (int i = 1; i <= self.length; i++)
    {
        NSString *testString = [self substringToIndex:i];
        CGSize stringSize = [testString sizeWithFont:font];
        if (stringSize.height > rect.size.height || stringSize.width > rect.size.width)
            break;

        visibleString = testString;
    }
    return visibleString;
}

@end

这篇关于UILabel可见部分文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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