iOS - 结合NSLineBreakByWordWrapping和NSLineBreakByTruncatingTail [英] iOS - Combine NSLineBreakByWordWrapping and NSLineBreakByTruncatingTail

查看:1731
本文介绍了iOS - 结合NSLineBreakByWordWrapping和NSLineBreakByTruncatingTail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要什么

我需要在PDF上写一些文字。文本本身可以是任意长度,但我只能显示2行(通过自动换行),如果空间不够,应该在第二行截断尾部。

I need to write some text to a PDF. The text itself can be any length, but I can only display 2 lines (by word wrapping) and should truncate the tail in the second line if the space isn't enough.

我尝试了什么

NSMutableParagraphStyle *text = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
text.lineBreakMode = NSLineBreakByTruncatingTail;

这样,文本仅在第一行中出现,并在行尾截断。

This way, the text goes only in the first line, and truncates at the end of the line.

NSMutableParagraphStyle *text = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
text.lineBreakMode = NSLineBreakByWordWrapping;

这样,文本换行到第二行,但是如果有足够的文本用于更多行,那么在第二行末尾没有截断。

This way, the text wraps to the second line, but if there's enough text for more lines, there's no truncating at the end of the second line.

我需要一些方法来结合这两种行为。

I need some way to combine these two behaviours.

我研究的内容

我发现了一些关于自动换行问题的问题,但不是这样的。
离我最近的是:

I've found some questions about problems with word wrapping, but nothing like this. The nearest I've got was this:

NSLineBreakByWordWrapping on First Line但NSLineBreakByTruncatingTail For Second Line?

但问题出现了不同。在那个问题中,提问者想要这个行为,但是在标签中,他用一些故事板配置解决了问题,我也在项目的其他部分使用了这个配置。但是这个问题是关于PDF中的这种行为,使用 NSStringDrawingContext drawInRect 方法。

But the problem there was different. In that question, the asker wants this behaviour but in a label, and he fixes the problem with some storyboard configuration that I've also used in other part of my project. But this question is about this behaviour in a PDF, using the NSStringDrawingContext drawInRect method.

我正在使用iOS 7(实际上刚刚更新到7.1,但两者的情况相同)。

I'm using iOS 7 (actually just updated to 7.1, but the situation is the same in both).

提前致谢。

编辑 - 此外,我在询问之前看过这个链接,但没有帮助:

Edit - also, I've looked this link before asking, but with no help:

https://developer.apple.com/library/ios/documentation/cocoa/reference/ApplicationKit/Classes/NSParagraphStyle_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSLineBreakByWordWrapping

推荐答案

参考:

@interface NSAttributedString (NSExtendedStringDrawing)
- (void)drawWithRect:(CGRect)rect options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(6_0);
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(6_0);
@end

通过NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine获取选项,请注意此方法仅适用可用于NSAttributedString。

Pass "NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine" for options, please note this method is only available for NSAttributedString.

这篇关于iOS - 结合NSLineBreakByWordWrapping和NSLineBreakByTruncatingTail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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