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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9502235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:06:01  来源:igfitidea点击:

How to add padding-left on a UILabel created programmatically?

iphoneobjective-cios

提问by sixstatesaway

I know this is a noob question but ...I have these labels on a tableview, but the text is completely squished to the left. I want to add a bit of padding. How do I go about it?

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

any help is much appreciated! Thanks!

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

采纳答案by sixstatesaway

I found a better way to do this:

我找到了一个更好的方法来做到这一点:

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

}

回答by Brody Robertson

For a full list of available solutions, see this answer: UILabel text margin

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



The most flexible approach to add padding to UILabel is to subclass UILabel and add an edgeInsets property. You then set the desired insets and the label will be drawn accordingly.

向 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

回答by Danyun Liu

you can simple add white space at the begin of you text;

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

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

It is 'evil' way to add 'padding', but it may help.

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

回答by Jason Harwig

Set the backgroundColor on the customView also

还要在 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;    
}

Try not to hardcode magic numbers: (add these to top of file)

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

#define HEADER_HEIGHT 60.0f
#define LABEL_PADDING 10.0f

Should give this preview

应该给这个 预览

回答by Shameem

Try the following & play around with the padding etc.

尝试以下操作并使用填充等。

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

回答by yageek

You can create a subclass of UILabeland override intrinsicContentSizeand - (CGSize)sizeThatFits:(CGSize)size:

您可以创建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;
}

回答by PruitIgoe

You could use a UITextView instead. I did this in Cocoa but I'm pretty sure it translates to UITextView:

您可以改用 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];

回答by BP.

True, it's a bit inexact and hackish, but you could always add a space in front of the month name like this:

确实,这有点不准确和不切实际,但您始终可以在月份名称前添加一个空格,如下所示:

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