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

前端 未结 8 1119
温柔的废话
温柔的废话 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 06:49

    I found a better way to do this:

    -  (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  {
    
        CGRect frame = CGRectMake(0, 0, 320, 25);
        UIView *customView = [[UIView alloc] initWithFrame:frame];
        UILabel *sectionTitle = [[UILabel alloc] init];
        [customView addSubview:sectionTitle]; 
        customView.backgroundColor = [UIColor redColor];
    
        frame.origin.x = 10; //move the frame over..this adds the padding!
        frame.size.width = self.view.bounds.size.width - frame.origin.x;
    
        sectionTitle.frame = frame;
        sectionTitle.text = @"text";
        sectionTitle.font = [UIFont boldSystemFontOfSize:17];
        sectionTitle.backgroundColor = [UIColor clearColor];
        sectionTitle.textColor = [UIColor whiteColor];
    
        [sectionTitle release];
    
        tableView.allowsSelection = NO;
    
        return [customView autorelease];
    
    }
    

提交回复
热议问题