How to add padding-left on a UILabel created programmatically?

前端 未结 8 985
温柔的废话
温柔的废话 2020-12-29 06:33

I know this is a noob question but ...I have these labels on a tableview, but the text is completely squished to the left. I want to add a bit of padding. How do I go about

8条回答
  •  伪装坚强ぢ
    2020-12-29 07:03

    Try the following & play around with the padding etc.

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  {
    
        CGFloat headerHeight = 60, padding = 10;
    
        UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,headerHeight)] autorelease];
        customView.backgroundColor = [UIColor colorWithHexString:[[_months objectAtIndex:section] objectForKey:@"color"]];
    
        CGRect frame = CGRectMake(padding,padding,320 - 2*padding,headerHeight-2*padding);
    
        UILabel *headerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
    
        headerLabel.font = [UIFont boldSystemFontOfSize:18];
        headerLabel.backgroundColor = [UIColor clearColor];
        headerLabel.text =  [[_months objectAtIndex:section] objectForKey:@"name"];
    
        headerLabel.textColor = [UIColor whiteColor];
    
        [customView addSubview:headerLabel];
    
        return customView;
    }
    

提交回复
热议问题