等价于iOS编程的CTFrameGetVisibleStringRange吗? [英] CTFrameGetVisibleStringRange equivalent for iOS programming?

查看:50
本文介绍了等价于iOS编程的CTFrameGetVisibleStringRange吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个类似CTFrameGetVisibleStringRange的方法,该方法可以为我提供以给定大小呈现的文本,并带有换行符模式(即自动换行).例如,我有一长行文本..,并且有一个给定的矩形来绘制包裹在其中的文本,但是无论文本被截断的位置如何,我都会在另一个保留该文本的区域继续进行渲染.所以我需要一个像这样的方法:

I need a method like CTFrameGetVisibleStringRange that can give me the text that will be rendered in a given size supplied with a line break mode (i.e. word wrap). For example I have a long line of text.. and I have a given rectangle to draw the text wrapped in it, but wherever the text gets trunecated, I continue rendering it in another area where it left off. So I need a method like:

NSString * text = "The lazy fox jumped over the creek";
[text drawAtPoint:CGPointMake(0, 0) forWidth:20 withFont:[UIFont fontWithName:@"Arial" size:10] lineBreakMode:UILineBreakModeWordWrap];
// now I do I know how much it drew before it stopped rendering?

有人有什么主意吗?

**请参阅我的解决方案.

**EDITED: Please see my solution.

推荐答案

我遇到了类似的问题,并且我使用了Mike发布的解决方案.

I had a similar problem, and I used the solution Mike posted.

但是,事实证明, trimToWord 经常给我几个单词,超出了我指定的UILabel大小.我发现,如果我将while循环运算符更改为> =而不只是一个>,它会很好地工作.

It turned out, however, that trimToWord was often giving me a few too many words than could fit on my UILabel size specified. I found out that if I changed the while loop operator to a >= and not just a >, it worked perfectly.

我还添加了一些ivars( chopIndex remainingBody ),它们用于获取剩余的字符串,以便可以在下一个UILabel中显示它.

I also added a few ivars(chopIndex and remainingBody) that I used to get the remaining string so I could display it in my next UILabel.

这是我使用的解决方案.

Here's the solution I used.

-(NSString*) rewindOneWord:(NSString*) str{
    // rewind by one word
    NSRange lastspace = [str rangeOfString:@" " options:NSBackwardsSearch];
    if (lastspace.location != NSNotFound){
        int amount = [str length]-lastspace.location;
        chopIndex -= amount;
        return [str substringToIndex:lastspace.location];
    }else {
        // no spaces, lets just rewind 2 characters at a time
        chopIndex -= 2;
        return [str substringToIndex:[str length]-2];
    }
}

// returns only how much text it could render with the given stipulations   
-(NSString*) trimToWord:(NSString*)str sizeConstraints:(CGSize)availableSize withFont:(UIFont*)font{
    if(str == @"")
        return str;

    CGSize measured = [str sizeWithFont:font constrainedToSize:CGSizeMake(availableSize.width, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
    // 'guess' how much we will need to cut to save on processing time
    float choppedPercent = (((double)availableSize.height)/((double)measured.height));
    if(choppedPercent >= 1.0){
        //entire string can fit in availableSize
        remainingBody = @"";
        return str;
    }

    chopIndex = choppedPercent*((double)[str length]);
    str = [str substringToIndex:chopIndex];
    // rewind to the beginning of the word in case we are in the middle of one
    do{
        str = [self rewindOneWord:str];
        measured = [str sizeWithFont:font constrainedToSize:availableSize lineBreakMode:UILineBreakModeWordWrap];
    }while(measured.height>=availableSize.height);

    //increment past the last space in the chopIndex
    chopIndex++;

    //update the remaining string
    remainingBody = [remainingBody substringFromIndex:chopIndex];

    return str;
}

这篇关于等价于iOS编程的CTFrameGetVisibleStringRange吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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