简体   繁体   English

如何在第x行修剪NSString?

[英]How do I trim an NSString at line x?

This should be a somewhat simple question. 这应该是一个简单的问题。 I'm trying to trim an NSString to whatever text falls within 150px max height, and then append "Continue Reading" to the end of that trimmed text.. So say we have the following. 我试图将NSString修剪为最大高度不超过150px的任何文本,然后将“ Continue Reading”附加到该修剪的文本的末尾。

a
b
c
d
e
f
g
h
i
j
k
l
m

And we have a font of say, 13... we calculate the total text height and see that it's more than 150px tall at 13px font. 我们有一个说13的字体...我们计算了总的文本高度,发现13px字体的高度超过150px。 My question is, how do I cut the portion of the string that exceeds those 150px? 我的问题是,如何切割超过150px的字符串部分?

you could use 你可以用

- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(NSLineBreakMode)lineBreakMode

(on iOS7 better use boundingRectWithSize:options:attributes:context: ) (在iOS7上最好使用boundingRectWithSize:options:attributes:context: :)

Keep adding one line to the string while (size.height<150.0f) . while (size.height<150.0f)继续向字符串添加一行。

EDIT : This example could work: (Note: If you do not use ARC, trimmedStr should be autoreleased!) 编辑 :此示例可以工作:(注意:如果不使用ARC,trimmedStr应该会自动发布!)

- (NSString*) stringByTrimmingString:(NSString*)str forSize:(CGSize)maxSize withFont:(UIFont*)font
{
    NSMutableString* trimmedStr = [NSMutableString new];
    for (NSString* line in [str componentsSeparatedByString:@"\n"]) {

        NSString *testStr = [trimmedStr stringByAppendingFormat:@"%@\n",line];

        CGSize sz = [testStr sizeWithFont:font forWidth:maxSize.width lineBreakMode:NSLineBreakByWordWrapping];
        if (sz.height > maxSize.height) {
                return [trimmedStr copy];
        }
        [trimmedStr appendFormat:@"%@\n",line];
    }
    return [trimmedStr copy];
}

I do something really rather stupid (brute force) to get this accomplished in my app. 我做了一些相当愚蠢的事情(蛮力),以在我的应用程序中完成此任务。 I manually create new line characters and indents in a UILabel. 我在UILabel中手动创建换行符和缩进。 To do this I find the first space in the text (to get the end of the first word), check if that is beyond my bounds. 为此,我在文本中找到第一个空格(以获取第一个单词的结尾),检查是否超出了我的范围。 If not I move to the second space and check that. 如果没有,我将移至第二个空间并进行检查。

To do something a little more intelligent for you I would split your string in half then see if that half exceeds your bounds. 为了给您做些更聪明的事情,我将您的字符串分成两半,然后看看这一半是否超出了您的范围。 If it does then go to 1/4, if not then go to 3/4 and check that. 如果是,则转到1/4,否则转到3/4并进行检查。 Then once you find the character that makes you exceed your limit then do a backward search for a space (to find the last word that fits) and truncate your string there. 然后,一旦找到使您超出限制的字符,然后向后搜索空格(以找到适合的最后一个单词)并在其中截断字符串。 This may seem like difficult code, but it's just like a binary search algorythm, but through your string. 这看起来像是困难的代码,但就像二进制搜索算法一样,但是通过您的字符串。

You can simply use 您可以简单地使用

-(CGSize)sizeWithFont:(UIFont*)font forWidth:(CGFloat)width lineBreakMode:(NSLineBreakMode)lineBreakMode;

because it takes into account all \\n characters and calculates correct CGSize. 因为它考虑了所有\\ n字符并计算出正确的CGSize。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM