在IOS应用程序中,我在滚动视图中有一个标签,其中包含我在下面描述的属性
self.detailMainLabel?.numberOfLines = 0
self.detailMainLabel?.lineBreakMode = .ByWordWrapping
self.detailMainLabel?.sizeToFit()
并且有高度限制:
Relation = Greater than or equal
当我运行应用程序时,标签会根据文本自动自动展开。但是我想知道我能做些什么来实现文本标签末尾的"显示更多"或"显示更少"链接以扩展或降低高度。
有什么想法或建议吗?
首先,您必须将行数设置为1或2,以显示更多按钮。喜欢
self.detailMainLabel?.numberOfLines = 2
self.detailMainLabel?.lineBreakMode = .ByWordWrapping
self.detailMainLabel?.sizeToFit()
然后在"显示更多"按钮上单击"设置"
self.detailMainLabel?.numberOfLines = 0
self.detailMainLabel?.sizeToFit()
然后再次单击"显示更少"按钮,将行数设置为 2。如果您同时使用一个按钮来显示更少和显示更多,则还要维护一个布尔标志。
> https://github.com/TTTAttributedLabel/TTTAttributedLabel
#import "TTTAttributedLabel.h"
IBOutlet TTTAttributedLabel * lblname;
// Expand Button Press
- (IBAction)btnforexpande:(id)sender
{
if (expande == 1 ){
NSString *strText = @“STRING”;
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:strText attributes:
@{
(id)kCTForegroundColorAttributeName : (id)[UIColor blackColor].CGColor,
NSFontAttributeName : [UIFont boldSystemFontOfSize:13],
}];
lblname.text = attString;
lblname.numberOfLines = 1;
lblname.lineBreakMode=NSLineBreakByTruncatingTail;
NSAttributedString *showMore = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"..+more"] attributes:
@{
NSForegroundColorAttributeName:[UIColor blackColor],
NSBackgroundColorAttributeName:[UIColor colorWithRed:233.0f/255.0f green:233.0f/255.0f blue:233.0f/255.0f alpha:1.0f],
NSFontAttributeName : [UIFont boldSystemFontOfSize:13],
}];
[lblname setAttributedTruncationToken:showMore];
[lblname sizeToFit];
}
else{
// Expand
lblname.numberOfLines = 0;
lblname.lineBreakMode=NSLineBreakByWordWrapping;
CGSize sizeTitle1 = [lblcc.attributedText boundingRectWithSize:CGSizeMake(lblcc.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
_lblccHight.constant = sizeTitle1.height;
}
}