带有textview或标签的按钮越来越少

时间:2015-12-23 12:43:38

标签: ios objective-c swift uitextview

嗨我希望textviewlabel看到越来越少的功能。我尝试使用label并成功获得结果:

enter image description here

但是如果文字会增加button,则会显示更多内容:

enter image description here

在某些情况下会看起来:

enter image description here

我想设置button将根据text自动设置。任何人都建议我更好地获取此方法。

3 个答案:

答案 0 :(得分:2)

如果您使用UILabel而不是TTTAttributedLabel,则可以设置可点击的文字。

使用标签文字附加字符串“查看更多”或“更少功能”并使其可点击。

答案 1 :(得分:1)

因此,我的简单代码使用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];
    }
}

答案 2 :(得分:0)

这是Xcode最酷的一件事 - “ Autolayout ”。

您可以从Interface Builder / Xcode进行设置,并在运行时对其进行管理。

Apple documentation on Autolayout

Ray Wenderlich tutorial