首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有文本视图或标签的更多和更少按钮

带有文本视图或标签的更多和更少按钮
EN

Stack Overflow用户
提问于 2015-12-23 20:43:38
回答 2查看 3.9K关注 0票数 3

嗨,我想要一个看到更多和更少功能的textviewlabel。我尝试了一下label,并成功地得到了如下结果:

但是,如果文本会增加它,button more将如下所示:

在某些情况下,它会看起来:

我正在尝试设置button将根据text.can自动设置,任何人建议我更好的方式得到这个。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-23 21:40:19

如果你正在使用UILabel,那么你可以使用TTTAttributedLabel,因为你可以设置可点击的文本。

在字符串"see more“或"less functionality”后附加标签文本,并使其可点击。

票数 2
EN

Stack Overflow用户

发布于 2016-06-11 02:38:12

所以这里有我用TTTAttributedLabel创建标签来实现‘更多...’的简单代码。和“更少...”标签的功能:

代码语言:javascript
复制
- (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];
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34435996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档