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

前端 未结 8 987
温柔的废话
温柔的废话 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:02

    You could use a UITextView instead. I did this in Cocoa but I'm pretty sure it translates to UITextView:

        NSTextView *headerLabel = [[[NSTextView alloc] initWithFrame:NSMakeRect(20.0, 20.0, 400.0, 20.0)] autorelease];
    [headerLabel setBackgroundColor: [NSColor redColor]];
    [headerLabel setString: @"Testing Stuff"];
    [headerLabel setTextColor:  [NSColor whiteColor]];
    
    NSSize txtPadding; 
    txtPadding.width = 20.0;
    txtPadding.height = 0.0;
    [headerLabel setTextContainerInset:txtPadding];
    
    [[mainWin contentView] addSubview:headerLabel];
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题