我一直在为此工作很长一段时间。 我需要展开并折叠UILabel
文本,点击位于文本末尾的按钮UILabel
。如何在UILabel的末尾添加更多按钮?
认为我试过 我已经使用VSWordDetector来检测哪个单词的UILabel
被点击,但它没有给出正确的单词点击。
我一直在为此工作很长一段时间。 我需要展开并折叠UILabel
文本,点击位于文本末尾的按钮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
嗯,这就是我一直在寻找...我是新目标-C所以你可以请张贴一些相同的代码... – 2014-10-08 10:10:00
@HemantSabale,完成:) – 2014-10-08 10:38:15
你为什么不简单地改变标签的框架?根据具体情况给它一个更小或更大的框架。 – Marc 2014-10-08 06:19:37
将标签拆分为两个,并计算第二个的框架和可见性。第二个可以用来检测水龙头。 – Krzysztof 2014-10-08 06:23:16
一个简单的按钮下面的标签... – Wain 2014-10-08 06:27:45