我有两列标签.ColumnA包含五个空标签,columnB包含五个带有一些随机文本的标签.ColumnA最初隐藏,当用户点击ColumnB中的任何标签时进入视图。两列中两个标签的大小不同,因为columnB大小与其包含的文本相符,但columnA没有文本,因此其大小取决于框架。我想当我点击columnB中的任何标签时,columnA中所有标签的大小应该与tapped标签的大小相同,我怎样才能得到所有这些东西?见图
在此图片中,当我点击文字 abs 的灰色标签时,有不同大小的蓝色空标签。我怎样才能获得蓝色空标签尺寸标签? 这是我的代码`//顶行的顶部 CGFloat y1 = 100.0;
// left of Column A
CGFloat x1 = 20.0;
// left of Column B
CGFloat x2 = 220.0;
// Vertical spacing for rows of labels
CGFloat yInc = 40.0;
// initialize a CGRect
CGRect rect1 = CGRectMake(x1, y1, 100, 30);
for (int i = 0; i < length; i++)
{
// Create UILabel for Column A
_lblA = [[UILabel alloc] initWithFrame:rect1];
_lblA.tag = 100+i;
_lblA.textAlignment = NSTextAlignmentCenter;
_lblA.backgroundColor = [UIColor blueColor];
_lblA.textColor = [UIColor whiteColor];
_lblA.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
//[self.gameLayer addSubview:_lblA];
//lblA.userInteractionEnabled = YES;
_lblA.text=[MainArrayFirst objectAtIndex:i];
[_lblA sizeToFit];
// "move" the rectangle, so the next label starts at column B
rect1.origin.x = x2;
// Create UILabel for Column B
_lblB = [[UILabel alloc] initWithFrame:rect1];
_lblB.tag = 200+i;
_lblB.layer.borderColor = [UIColor blueColor].CGColor;
_lblB.layer.borderWidth = 3.0;
[self.gameLayer addSubview:_lblB];
__lblB.hidden = YES;
CGSize maximumLabelSize = CGSizeMake(100,30);
CGSize expectedLabelSize = [_lblA.text sizeWithFont:_lblA.font
constrainedToSize:maximumLabelSize
lineBreakMode:_lblA.lineBreakMode];
CGRect newFrame = _lblA.frame;
newFrame.size.width = expectedLabelSize.width;
self.lblB.frame = newFrame;
CGRect newFrame1 = _lblB.frame;
newFrame1.size.width = expectedLabelSize.width;
newFrame1.origin.x = _lblC.frame.origin.x + self.lblC.frame.size.width;
// add a Tap Gesture Recognizer to the label
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotTapped:)];
[_lblB addGestureRecognizer:tap];
// "move" the rectangle, so the next label starts at column C
// this is the right-edge of the Column A label
rect1.origin.x = _lblC.frame.origin.x + _lblC.frame.size.width;
}
- (void) gotTapped:(id)sender {
// a label in Column A was tapped, so show Column B labels if they were hidden,
// or hide them if they were visible
for (UILabel *v in _columnB) {
v.hidden = !v.hidden;
}
}
`