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

six*_*way 21 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;
}
Run Code Online (Sandbox Code Playgroud)

任何帮助深表感谢!谢谢!

Bro*_*son 40

有关可用解决方案的完整列表,请参阅以下答案:UILabel文本边距


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

OSLabel.h

#import <UIKit/UIKit.h>

@interface OSLabel : UILabel

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

  • 如果您希望自动调整标签大小以适合文本,则此解决方案不适用于多行.如果添加的插图导致文本换行到另一行,则标签不会增长以显示它.关于如何针对该案例调整此解决方案的任何想法? (3认同)

Dan*_*Liu 17

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

[NSString stringWithFormat:@"  %@",text];
Run Code Online (Sandbox Code Playgroud)

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

  • 这不适用于多行,然后只有第一行缩进.对于任何尝试此操作的人来说,只是未来参考,仅适用于单行标签. (15认同)

six*_*way 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];

}
Run Code Online (Sandbox Code Playgroud)


Jas*_*wig 8

同样在customView上设置backgroundColor

- (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;    
}
Run Code Online (Sandbox Code Playgroud)

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

#define HEADER_HEIGHT 60.0f
#define LABEL_PADDING 10.0f
Run Code Online (Sandbox Code Playgroud)

应该给这个 预习