嗨,我想要一个看到更多和更少功能的textview
或label
。我尝试了一下label
,并成功地得到了如下结果:
但是,如果文本会增加它,button
more将如下所示:
在某些情况下,它会看起来:
我正在尝试设置button
将根据text.can自动设置,任何人建议我更好的方式得到这个。
发布于 2015-12-23 21:40:19
如果你正在使用UILabel
,那么你可以使用TTTAttributedLabel,因为你可以设置可点击的文本。
在字符串"see more“或"less functionality”后附加标签文本,并使其可点击。
发布于 2016-06-11 02:38:12
所以这里有我用TTTAttributedLabel创建标签来实现‘更多...’的简单代码。和“更少...”标签的功能:
- (void) setupStoreTitleLabel {
self.titleLabel.delegate = self;
[self.titleLabel setText:self.card.name];
self.titleLabel.numberOfLines = 2;
NSAttributedString *showMore = [[NSAttributedString alloc] initWithString:@" more..." attributes:@{
NSForegroundColorAttributeName:[UIColor blueColor],
NSFontAttributeName : [UIFont boldSystemFontOfSize:12],
NSLinkAttributeName : [NSURL URLWithString:@"more..."]
}];
[self.titleLabel setAttributedTruncationToken:showMore];
}
#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(__unused TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
NSLog(@"%@ pressed", url);
if ([[url absoluteString] isEqualToString:@"more..."]) {
self.titleLabel.numberOfLines = 99;
NSString *cardNameWithLess = [NSString stringWithFormat:@"%@ %@",self.card.name, @" less..."];
[self.titleLabel setText:cardNameWithLess afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
//code
NSRange linkRange = [[mutableAttributedString string] rangeOfString:@" less..." options:NSCaseInsensitiveSearch];
[mutableAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:linkRange];
[mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:linkRange];
[mutableAttributedString addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"less..."] range:linkRange];
return mutableAttributedString;
}];
} else {
self.titleLabel.numberOfLines = 2;
NSAttributedString *showMore = [[NSAttributedString alloc] initWithString:@" more..." attributes:@{
NSForegroundColorAttributeName:[UIColor blueColor],
NSFontAttributeName : [UIFont boldSystemFontOfSize:12],
NSLinkAttributeName : [NSURL URLWithString:@"more..."]
}];
[self.titleLabel setAttributedTruncationToken:showMore];
[self.titleLabel setText:self.card.name];
}
}
https://stackoverflow.com/questions/34435996
复制相似问题