我一直在爲此工作很長一段時間。 我需要展開並摺疊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