Search code examples
iosuiviewuilabelcgrect

Rounded rectangle box draw -iOS


How could I create this rounded rectangle as shown on the figure? Also my second question is how to get multiple lines , how to format to make it multiple lines as shown in the red rectangle? Thanks in advance!.

CGRect viewRect=CGRectMake(30,30,320,55);
UIView *myView=[[UIView alloc]initWithFrame:viewRect];
myView.backGroundColor=[UIColor whiteColor];
[self.view addSubview:myView]

UILabel *label=[[UILabel alloc]initwithFrame:CGRectMake(30,32,25,12)];
label.font=[UIFont fontWithName:"System" size:12];
label.textColor=[UIColor blackColor];
[self.view addSubview:label]

enter image description here


Solution

  • The example you provided is probably implemented as a UITableView. The address cell has the UITableViewCellStyleValue2 style, which has the textLabel on the left ("address") and detailTextLabel on the right.

    Here's how to format the cell:

    static NSString *CellIdentifier = @"MyCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = @"address";
    cell.detailTextLabel.text = @"7200—7232 W Orem Dr\nHouston Texas 77085\nUnited States";
    cell.detailTextLabel.numberOfLines = 3;
    cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
    

    You'll also have to adjust the height of the cell accordingly, since it will be taller than the default.

    Source: Multi-line UITableViewCell using UILabel (via a similar Stack Overflow question)