从较长的NSString返回一个包含字的NSString [英] Return a word-wrapped NSString from a longer NSString

查看:91
本文介绍了从较长的NSString返回一个包含字的NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

UITextView:获取包装信息的文字

I一直在寻找NSString库和众多库,可以使用这样一个长字符串的函数:

I have been scouting the NSString library and numerous libraries for a function that can take a long string like this :

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui.

以及表示宽度的CGSize或浮点数,以及正在使用的字体,并返回给我一个带\ n的字符串和单词包装。

and together with a CGSize or float indicating the width, and the font being used, and return me a string with \n breaks and the words wrapped.

结果(粗略):

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac\n
egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet.\n
 Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. \n
placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra.\n
 Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi.\n
 Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci,\n
 sagittis tempus lacus enim ac dui. 

我已经知道UITextViews这样做了,但这没有用,因为我需要渲染原始OpenGL环境中的文本,所以我没有使用常规UI元素。

I already know that UITextViews and such do this, but this is not helpful since I need to render the text in a raw OpenGL landscape, so I am using no regular UI elements.

我知道这可以作为框架存在,也可以作为某个公共类存在。我只是简单地找不到任何统一的处理方式。

I know this either exists as a framework, or a public class somewhere. I just simply can't find any unified way of handling this.

我想它接近[NSString sizeWithFont:forWidth:lineBreakMode:],但我不需要大小,我需要字符串本身。

I imagine it is close to [NSString sizeWithFont:forWidth:lineBreakMode:], but i do not need the size, I need the string itself.

推荐答案

真的没有必要重新发明这个轮子,因为它正是这个每次你包装文本时,文本引擎都会为你做。什么文本引擎?它是核心文本。如果你下降到核心文本的级别并让CTFramesetter为你布置文本,你可以通过询问产生的CTLines来了解换行的位置。

There really is no need to reinvent this wheel, since it is exactly what the text engine does for you every time you wrap text. And what is the text engine? It is Core Text. If you drop down to the level of Core Text and have a CTFramesetter lay out the text for you, you can learn where it is putting the line breaks by asking for the resulting CTLines.

该文档将帮助您入门:

http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Operations/Operations.html

网上有很多很好的教程。

And there are lots of good tutorials on the Web.

简单的例子:

NSString* s = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
@"eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut "
@"enim ad minim veniam, quis nostrud exercitation ullamco laboris "
@"nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor "
@"in reprehenderit in voluptate velit esse cillum dolore eu fugiat "
@"nulla pariatur. Excepteur sint occaecat cupidatat non proident, "
@"sunt in culpa qui officia deserunt mollit.";
NSAttributedString* text = [[NSAttributedString alloc] initWithString:s];

CTFramesetterRef fs =
CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)text);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0,0,200,100000));
CTFrameRef f = CTFramesetterCreateFrame(fs, CFRangeMake(0, 0), path, NULL);
CTFrameDraw(f, NULL);

NSArray* lines = (__bridge NSArray*)CTFrameGetLines(f);
for (id aLine in lines) {
    CTLineRef theLine = (__bridge CTLineRef)aLine;
    CFRange range = CTLineGetStringRange(theLine);
    NSLog(@"%ld %ld", range.location, range.length);
}
CGPathRelease(path);
CFRelease(f);
CFRelease(fs);

正如您将看到的,输出显示了每行包装文本的范围。这不是你的事吗之后?

As you will see, the output shows the range of each line of wrapped text. Isn't this the sort of thing you're after?

这篇关于从较长的NSString返回一个包含字的NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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