Search code examples
iosobjective-cuilabeltouchesbegan

How to position five copies of a label relative to other label with touch in objective c


I have two columns of labels with some text. How can i get 3rd Column of label attached to column1 when i touch on any label of my second column. Here is code for column1 and Column2 labels

Column1 labels

for(int i=0;i<length;i++)
{


    _lbl = [[UILabel alloc]initWithFrame:rect1];
   // lbl.frame=CGRectMake(x, y, width+10, 30);
    _lbl.center = CGPointMake(x, y);
    _lbl.tag=i;
    _lbl.textAlignment=NSTextAlignmentCenter;
    _lbl.backgroundColor=[UIColor blueColor];
    _lbl.textColor=[UIColor whiteColor];
    _lbl.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
    //label.clipsToBounds=YES;
    //label.layer.cornerRadius=5.0;
    [self.gameLayer addSubview:_lbl];
    _lbl.userInteractionEnabled = YES;
    _lbl.text=@"Text1";
    [_lbl sizeToFit];

Column2 for labels

for(int i=0;i<length;i++)
{


    _lbl2 = [[UILabel alloc]initWithFrame:rect];
   // _lbl2.frame=CGRectMake(x, y, width+10, 30);
    _lbl2.center = CGPointMake(x+20, y);
    _lbl2.tag=i+5;
    _lbl2.textAlignment=NSTextAlignmentCenter;
    _lbl2.backgroundColor=[UIColor greenColor];
    _lbl2.textColor=[UIColor whiteColor];
    _lbl2.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
    //_lbl2.clipsToBounds=YES;
    //_lbl2.layer.cornerRadius=5.0;
    [self.gameLayer addSubview:_lbl2];
    _lbl.userInteractionEnabled = YES;
    _lbl2.text=@"Text2";
    [_lbl2 sizeToFit];

In touches Begin method i call a method makeThirdColumn

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

      {
        CGPoint pt = [[touches anyObject] locationInView:self.superview];
        _xOffset = pt.x - self.center.x;
        _yOffset = pt.y - self.center.y;
        [self makeThirdColumn];

I,m not getting 3rd column of labels at required position as in this image only two columns areenter image description here

I want to get 3rd column concate with column1 Text1 label.


Solution

  • @Moon - I'm still not entirely sure what you are trying to do, but hopefully this will help you on your way.

    To try and make it a little more clear, I used Columns named A, B and C instead of 1, 2 and 3. This is the result - the Red "Column C" labels will show or hide when you tap the Gray "Column B" labels.

    enter image description here

    If this doesn't do what you want (or if you can't figure out how to make this do what you want), you will need to be much more clear in describing your goal.


    //  LabelColumnsViewController.h
    
    #import <UIKit/UIKit.h>
    
    @interface LabelColumnsViewController : UIViewController
    
    @end
    

    //  LabelColumnsViewController.m
    
    #import "LabelColumnsViewController.h"
    
    @interface LabelColumnsViewController ()
    
    @property (strong, nonatomic) UIView *gameLayer;
    
    @property (strong, nonatomic) NSMutableArray *columnA;
    @property (strong, nonatomic) NSMutableArray *columnB;
    @property (strong, nonatomic) NSMutableArray *columnC;
    
    @end
    
    @implementation LabelColumnsViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // add a UIView to hold the Labels
        _gameLayer = [[UIView alloc] initWithFrame:self.view.frame];
        _gameLayer.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:_gameLayer];
    
        // initialize arrays to hold references to the labels
        _columnA = [NSMutableArray array];
        _columnB = [NSMutableArray array];
        _columnC = [NSMutableArray array];
    
        // set rows to 5
        int length = 5;
    
        // top of top row
        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 = 1; i < length; i++)
        {
    
            // Create UILabel for Column A
            UILabel *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=@"Text1";
            [lblA sizeToFit];
    
    
            // "move" the rectangle, so the next label starts at column B
            rect1.origin.x = x2;
    
            // Create UILabel for Column B
            UILabel *lblB = [[UILabel alloc] initWithFrame:rect1];
            lblB.tag = 200+i;
            lblB.textAlignment = NSTextAlignmentCenter;
            lblB.backgroundColor = [UIColor grayColor];
            lblB.textColor = [UIColor whiteColor];
            lblB.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
            [self.gameLayer addSubview:lblB];
            lblB.userInteractionEnabled = YES;
            lblB.text=@"Text2";
            [lblB sizeToFit];
    
            // 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 = lblA.frame.origin.x + lblA.frame.size.width;
    
            // Create UILabel for Column C
            UILabel *lblC = [[UILabel alloc] initWithFrame:rect1];
            lblC.tag = 300+i;
            lblC.textAlignment = NSTextAlignmentCenter;
            lblC.backgroundColor = [UIColor redColor];
            lblC.textColor = [UIColor whiteColor];
            lblC.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
            [self.gameLayer addSubview:lblC];
            lblC.userInteractionEnabled = YES;
            lblC.text=@"Text3";
            [lblC sizeToFit];
    
            // Column C is initially Hidden
            lblC.hidden = YES;
    
    
            // add the labels to their Arrays so we can access them later
            [_columnA addObject:lblA];
            [_columnB addObject:lblB];
            [_columnC addObject:lblC];
    
    
            // reset left of rect to Column A, and increment y (for next row)
            rect1.origin.x = x1;
            rect1.origin.y += yInc;
    
        }
    
    }
    
    - (void) gotTapped:(id)sender {
        // a label in Column B was tapped, so show Column C labels if they were hidden,
        //      or hide them if they were visible
        for (UILabel *v in _columnC) {
            v.hidden = !v.hidden;
        }
    }
    
    @end