我需要启用自动换行和尾巴截断,在同一时间,在UIButton的titleLabel.将numberOfLines设置为大于0的值不起作用,文本保持在一行.
我已经四处寻找并且没有找到解决方案.任何的想法?
Mar*_*rry 41
这不正确:
lblTemp.lineBreakMode = NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail
lblTemp.numberOfLines = 0;
Run Code Online (Sandbox Code Playgroud)
NSLineBreakMode在NSParagraphStyle.h中定义为:
typedef NS_ENUM(NSInteger, NSLineBreakMode) { /* What to do with long lines */
NSLineBreakByWordWrapping = 0, /* Wrap at word boundaries, default */
NSLineBreakByCharWrapping, /* Wrap at character boundaries */
NSLineBreakByClipping, /* Simply clip */
NSLineBreakByTruncatingHead, /* Truncate at head of line: "...wxyz" */
NSLineBreakByTruncatingTail, /* Truncate at tail of line: "abcd..." */
NSLineBreakByTruncatingMiddle /* Truncate middle of line: "ab...yz" */
} NS_ENUM_AVAILABLE_IOS(6_0);
Run Code Online (Sandbox Code Playgroud)
请注意,它是NS_ENUM,而不是NS_OPTION,因此它不能用作掩码.欲了解更多信息,请参阅本.
实际上使用| 这些常量上的运算符导致与NSLineBreakByTruncatingTail匹配的掩码:
(NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail) == 4
NSLineBreakByTruncatingTail == 4
Run Code Online (Sandbox Code Playgroud)
据我所知,截断Core Text中的最后一行并进行自动换行无法使用简单的CTFramesetterCreateWithAttributedString和CTFrameDraw API完成,但可以使用逐行布局完成,UILabel必须这样做.
iOS 6通过在NSStringDrawing.h中公开新的绘图API简化了这一点:
typedef NS_ENUM(NSInteger, NSStringDrawingOptions) {
NSStringDrawingTruncatesLastVisibleLine = 1 << 5, // Truncates and adds the ellipsis character to the last visible line if the text doesn't fit into the bounds specified. Ignored if NSStringDrawingUsesLineFragmentOrigin is not also set.
NSStringDrawingUsesLineFragmentOrigin = 1 << 0, // The specified origin is the line fragment origin, not the base line origin
NSStringDrawingUsesFontLeading = 1 << 1, // Uses the font leading for calculating line heights
NSStringDrawingUsesDeviceMetrics = 1 << 3, // Uses image glyph bounds instead of typographic bounds
} NS_ENUM_AVAILABLE_IOS(6_0);
@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
Run Code Online (Sandbox Code Playgroud)
因此,如果您使用的是UILabel,则需要将NSAttributedString的NSParagraphStyle或标签本身的lineBreakMode设置为:
NSLineBreakByTruncatingTail
Run Code Online (Sandbox Code Playgroud)
并且标签上的numberOfLines属性必须设置为0.
从numberOfLines上的UILabel标题:
// if the height of the text reaches the # of lines or the height of the view is less than the # of lines allowed, the text will be
// truncated using the line break mode.
Run Code Online (Sandbox Code Playgroud)
从UILabel文档:
This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.
If you constrain your text using this property, any text that does not fit within the maximum number of lines and inside the bounding rectangle of the label is truncated using the appropriate line break mode.
Run Code Online (Sandbox Code Playgroud)
UILabel这个有点模糊的特性引起的唯一问题是你无法在绘制之前获得大小(这是一些UITableView + UITableViewCell动态布局的必要),而无需动态修改NSAttributedString的NSParagraphStyle.
从iOS 6.1.4开始,调用-boundingRectWithSize:options:具有NSLttributedString且具有NSLineBreakByTruncatingTail换行符模式(对于UILabel)的上下文,即使传入以下选项,也会返回不正确的单行高度:
(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine)
Run Code Online (Sandbox Code Playgroud)
(请注意,NSStringDrawingUsesLineFragmentOrigin是多行字符串的必需品.)
更糟糕的是,UILabel的lineBreakMode并没有覆盖NSAttributedStrings段落样式,所以你必须修改你的属性串的段落样式为您的容量计算,后来因为它传递给的UILabel,因此可以借鉴它.
也就是说,NSLineBreakByWordWrapping for -boundingRectWithSize:options:context和NSLineBreakByTruncatingTail for UILabel(所以它可以,在内部使用NSStringDrawingTruncatesLastVisibleLine,或者它用来剪辑最后一行的任何内容)
如果你不想多次改变你的字符串的段落样式,唯一的选择是做一个简单的UIView子类,它覆盖-drawRect :(同时将相应的contentMode设置为重绘),并使用iOS 6的新绘图API :
- (void)drawWithRect:(CGRect)rect options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(6_0);
Run Code Online (Sandbox Code Playgroud)
记得使用NSLineBreakByWordWrapping并传入(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine)作为选项.
最后,在iOS 6之前,如果你想对一个属性字符串进行自动换行+尾部截断,你必须自己使用Core Text进行逐行布局.
我在发布这个问题的同一天解决了这个问题,方法是将 a 放在 aUIButton上UILabel,numberOfLines设置为 3。我没有接受这个问题,看看是否有人有更好的主意,但显然没有其他解决方案。
[self.costomButton.titleLabel setTextAlignment:UITextAlignmentLeft];
[self.costomButton.titleLabel setNumberOfLines:3];
Run Code Online (Sandbox Code Playgroud)
确保您应该Alignment先设置 ps:仅当系统版本大于 5.0 时才有效
| 归档时间: |
|
| 查看次数: |
33743 次 |
| 最近记录: |