如何在编程创build的UILabel上添加填充左侧?
我知道这是一个noob问题,但是…我有一个tableview这些标签,但文本完全挤在左边。 我想添加一些填充。 我如何去做呢?
- (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; }
任何帮助深表感谢! 谢谢!
有关可用解决scheme的完整列表,请参阅此答案: UILabel文本边距
向UILabel添加填充最灵活的方法是inheritanceUILabel并添加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
你可以简单地在你的文本开头添加空格;
[NSString stringWithFormat:@" %@",text];
添加“填充”是“邪恶”的方式,但可能有帮助。
我发现了一个更好的方法来做到这一点:
- (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]; }
在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; }
尽量不要硬编码幻数:(将这些添加到文件顶部)
#define HEADER_HEIGHT 60.0f #define LABEL_PADDING 10.0f
应该给这个
尝试以下方法,并使用填充等
- (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; }
您可以创buildUILabel
的子类并覆盖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; }
诚然,这有点不准确,但是你可以在这个月份的名字前添加一个空格,如下所示:
headerLabel.text = [NSString stringWithFormat:@" %@", [[_months objectAtIndex:section] objectForKey:@"name"]];
你可以使用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];