2014-10-08 44 views
1

我一直在为此工作很长一段时间。 我需要展开并折叠UILabel文本,点击位于文本末尾的按钮UILabel如何在UILabel的末尾添加更多按钮?

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

+0

你为什么不简单地改变标签的框架?根据具体情况给它一个更小或更大的框架。 – Marc 2014-10-08 06:19:37

+0

将标签拆分为两个,并计算第二个的框架和可见性。第二个可以用来检测水龙头。 – Krzysztof 2014-10-08 06:23:16

+0

一个简单的按钮下面的标签... – Wain 2014-10-08 06:27:45

回答

2

我建议你只用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 
+0

嗯,这就是我一直在寻找...我是新目标-C所以你可以请张贴一些相同的代码... – 2014-10-08 10:10:00

+0

@HemantSabale,完成:) – 2014-10-08 10:38:15

相关问题