gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 08:01:54 25 4
gpt4 key购买 nike

我知道这是一个菜鸟问题,但是...我在 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;
}

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

最佳答案

有关可用解决方案的完整列表,请参阅此答案: 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

关于iphone - 如何在以编程方式创建的 UILabel 上添加 padding-left?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9502235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com