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)
Dan*_*Liu 17
您可以在文本的开头简单地添加空格;
[NSString stringWithFormat:@" %@",text];
Run Code Online (Sandbox Code Playgroud)
添加'填充'是'邪恶'的方式,但它可能有所帮助.
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)
同样在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)
应该给这个

| 归档时间: |
|
| 查看次数: |
66558 次 |
| 最近记录: |