gpt4 book ai didi

ios - 如何在 UILabel 的末尾添加更多按钮?

转载 作者:行者123 更新时间:2023-11-29 10:37:18 26 4
gpt4 key购买 nike

我已经研究了很长时间了。我需要通过单击位于 UILabel 文本末尾的按钮来展开和折叠 UILabel 文本。

我觉得我试过了我已经使用 VSWordDetector 来检测 UILabel 的哪个单词被点击了,但它没有给出正确的单词被点击。

最佳答案

我建议你只使用 UIButton 没有可见框架 titleLabel.text @"..."@"▼ “。因此,例如,您有一个字符串 @"Some long long, really long string which couldn't be presented in one line"。然后为 UILabel 文本取一个子字符串,并在您的标签右侧放置一个上述按钮。为 ▼-buuton 添加一个操作来更新 label.text 并隐藏按钮。代码片段:

@interface YourClass

@property (strong, nonatomic) UILabel* longStringLabel;
@property (strong, nonatomic) UIButton* moreButton;
@property (strong, nonatomic) NSString* text;

@end

@implementation YourClass

// Some method, where you add subviews, for example viewDidLoad
{
// ...
self.longStringLabel.frame = CGRectMake(0, 0, 100, 44);
[self addSubview:self.longStringLabel];

self.moreButton.frame = CGRectMake(CGRectGetMaxX(self.longStringLabel.frame), 0, 20, 44);
[self addSubview:self.moreButton];
// ...
}

- (UILabel*)longStringLabel
{
if (!_longStringLabel)
{
_longStringLabel = [UILabel new];
_longStringLabel.lineBreakMode = NSLineBreakByTruncatingTail;
}

return _longStringLabel;
}

- (UIButton*)moreButton
{
if (!_moreButton)
{
_moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
_moreButton.titleLabel.text = @"▼";
[_moreButton addTarget:self action:@selector(moreButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];
}
return _moreButton;
}

- (void)moreButtonDidTap:(UIButton*)sender
{
self.longStringLabel.frame = [self.text boundingRectWithSize:CGSizeMake(self.longStringLabel.frame.size.width + self.moreButton.frame.size.width, 100)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName : self.longStringLabel.font }
context:nil];
self.longStringLabel.text = self.text;

self.moreButton.hidden = YES;
}

@end

关于ios - 如何在 UILabel 的末尾添加更多按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26250201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com