Search code examples
iphoneobjective-ciosuilabelxcode4.5

How to highlight an email address after tapping


In my table view , I have a first name, a last name, an email address and a phone number. I have web services through which I can fetch data from a server and display it on my device. I have an issue, the data that I'm fetching from the server is being displaying perfectly, but some email addresses are quite long, therefore half of the email address can be seen followed by dot dot (i.e raboert.baderasam11@gma....).

Is there any way so that when I tap on the email address, I can see all its text or label highlighted, i.e the full email address? or any tooltip something that highlights the email address?I want it in single view and not multiple. This task has to be done. So please help me. I'm very new to iPhone development.

   here is the code :-


 // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath 
{    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    NSArray* currentListOfItems=nil;
    Contact *aContact=nil;
    if(searching){
        currentListOfItems=searchResult;
        aContact=[searchResult objectAtIndex:indexPath.row];
    }else{
        currentListOfItems=content;
        aContact=[[[content objectAtIndex:indexPath.section] objectForKey:@"rowValues"]
          objectAtIndex:indexPath.row];
    }
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
    reuseIdentifier:CellIdentifier];
    UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, 150, 20)];
    [lbl3 setTextColor:[UIColor blackColor]];
    [lbl3 setFont:[UIFont boldSystemFontOfSize:16.0]];
    lbl3.text =[NSString stringWithFormat:@"%@ %@", aContact.lastName,aContact.firstName];
    [cell addSubview:lbl3];
    UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(12, 20, 200, 20)];
    [lbl1 setTextColor:[UIColor blackColor]];
    [lbl1 setFont:[UIFont boldSystemFontOfSize:12.0]];
    lbl1.text = aContact.email;
    [cell addSubview:lbl1];
    UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(210, 20, 300, 20)];
    [lbl2 setTextColor:[UIColor grayColor]];
    [lbl2 setFont:[UIFont fontWithName:@"Arial" size:12.0]];
    lbl2.text = [NSString stringWithFormat:@"%@%@", @"| ",aContact.phone] ;
    [cell addSubview:lbl2];
    return cell;
}

Solution

  • You could the change the width of the label with the email address when the user touches it. To receive the touch enable userInteractionEnabled on the label and add a UITapGestureRecognizer. In the method to the gesture recognizer change the width of the view property to the gesture recognizer.

    Edit:

    A label does not allow user interaction by default. In order to receive gestures you have to enable user interaction like so

    myLabel.userInteractionEnabled = YES;
    

    To add a UITapGestureRecognizer do the following:

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lableWasTapped:)];
    [myLabel addGestureRecognizer:tapGestureRecognizer];
    

    To react on the taps implement the following:

    - (void)lableWasTapped:(UITapGestureRecognizer*)sender {
        UILabel *label = (UILabel*)sender.view;
        CGSize labelSize = [label.text sizeWithFont:label.font];
        CGRect labelFrame = label.frame;
        labelFrame.size.width = labelSize.width;
        label.frame = labelFrame;
    }