如何在以编程方式创建的UILabel上添加padding-left?

时间:2012-02-29 15:49:48

标签: iphone objective-c ios

我知道这是一个菜鸟问题,但是......我在桌面视图上有这些标签,但文字完全被压到了左边。我想添加一些填充。我该怎么办呢?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  {

    UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(10,0,300,60)] autorelease];

    UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];

    headerLabel.backgroundColor = [UIColor colorWithHexString:[[_months objectAtIndex:section] objectForKey:@"color"]];
    headerLabel.font = [UIFont boldSystemFontOfSize:18];
    headerLabel.frame = CGRectMake(0,0,400,30);
    headerLabel.text =  [[_months objectAtIndex:section] objectForKey:@"name"];

    headerLabel.textColor = [UIColor whiteColor];


    [customView addSubview:headerLabel];

    return customView;
}

非常感谢任何帮助!谢谢!

8 个答案:

答案 0 :(得分:40)

有关可用解决方案的完整列表,请参阅以下答案: UILabel text margin


向UILabel添加填充的最灵活方法是继承UILabel并添加edgeInsets属性。然后,您可以设置所需的插图,并相应地绘制标签。

<强> OSLabel.h

#import <UIKit/UIKit.h>

@interface OSLabel : UILabel

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end

<强> OSLabel.m

#import "OSLabel.h"

@implementation OSLabel

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return self;
}

- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}

@end

答案 1 :(得分:17)

您可以在文字的开头简单地添加空格;

[NSString stringWithFormat:@"  %@",text];

添加'填充'是'邪恶'的方式,但它可能会有所帮助。

答案 2 :(得分:14)

我找到了更好的方法:

-  (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];

}

答案 3 :(得分:8)

在自定义视图上设置背景颜色

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGRect frame = tableView.bounds;
    frame.size.height = HEADER_HEIGHT;
    UIView* customView = [[[UIView alloc] initWithFrame:frame] autorelease];
    customView.backgroundColor = [UIColor redColor];    

    UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectInset(frame, LABEL_PADDING, 0)] autorelease];     
    // Orientation support   
    headerLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;    
    headerLabel.backgroundColor = [UIColor redColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:18];
    headerLabel.text = @"My Text Label";        
    headerLabel.textColor = [UIColor whiteColor];

    [customView addSubview:headerLabel];

    return customView;    
}

尽量不要对幻数进行硬编码:(将这些添加到文件顶部)

#define HEADER_HEIGHT 60.0f
#define LABEL_PADDING 10.0f

应该给这个 preview

答案 4 :(得分:2)

尝试以下&amp;玩弄填充物等。

- (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;
}

答案 5 :(得分:2)

您可以创建UILabel的子类并覆盖intrinsicContentSize- (CGSize)sizeThatFits:(CGSize)size

- (CGSize) intrinsicContentSize
{
    CGSize parentSize = [super intrinsicContentSize];
    parentSize.width += 2*PADDING_VALUE;
    return parentSize;
}

- (CGSize)sizeThatFits:(CGSize)size
{
    CGSize parentSize = [super sizeThatFits:size];
    parentSize.width += 2*PADDING_VALUE;
    return parentSize;
}

答案 6 :(得分:0)

是的,它有点不精确和hackish,但你总是可以在月份名称前添加一个空格,如下所示:

headerLabel.text = [NSString stringWithFormat:@" %@",
                    [[_months objectAtIndex:section] objectForKey:@"name"]];

答案 7 :(得分:0)

您可以使用UITextView。我在Cocoa中做过这个,但我很确定它转换为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];