Iphone 如何在tableview行中写入两个或多个文本,并对它们进行不同的定位?

Iphone 如何在tableview行中写入两个或多个文本,并对它们进行不同的定位?,iphone,objective-c,uitableview,uilabel,Iphone,Objective C,Uitableview,Uilabel,我有一个UITableView,我想右对齐一行中的一个文本,另一个文本应该左对齐。这两个文本都应该从行的边界开始。我不想让他们从这一行的边界开始。我该怎么做 我想在每一行写左对齐的文本,也写右对齐的文本 提前感谢。UILabel在样式和布局方面非常有限。考虑使用多个布局或CoreText。UILabel在样式和布局方面非常有限。考虑使用多个布局或CoreText。您可以对标签进行子类化,并像这样重写其drawtextinrect函数 @interface UILabelCustom : UIL

我有一个UITableView,我想右对齐一行中的一个文本,另一个文本应该左对齐。这两个文本都应该从行的边界开始。我不想让他们从这一行的边界开始。我该怎么做

我想在每一行写左对齐的文本,也写右对齐的文本


提前感谢。

UILabel在样式和布局方面非常有限。考虑使用多个布局或CoreText。

UILabel在样式和布局方面非常有限。考虑使用多个布局或CoreText。

您可以对标签进行子类化,并像这样重写其drawtextinrect函数

@interface UILabelCustom : UILabel 

- (void)drawTextInRect:(CGRect)rect;
@end


@implementation UILabelCustom

-(void)drawTextInRect:(CGRect)rect{
    UIEdgeInsets insets = {5, 5, 5, 5};
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

@end
这将允许您在标签边缘周围留有边距 您还可以创建一个包含两个自定义标签的uiview,并相应地将其左对齐和右对齐。将uvview用作两个标签的容器,以便根据需要对其进行定位。我想这会产生你想要的效果

编辑:刚刚看到您对表格单元格的编辑。您需要创建自定义单元格和自定义uilabel

@interface CustomCell : UITableViewCell {

}


@property (retain, nonatomic) UILabelCustom *label1,*label2;


@end

@implementation CustomCell

@synthesize label1, label2;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)dealloc
{

    [label1 release];
    [label2 release];


    [super dealloc];
}

@end

您可以做的是将标签子类化,并像这样重写其drawtextinrect函数

@interface UILabelCustom : UILabel 

- (void)drawTextInRect:(CGRect)rect;
@end


@implementation UILabelCustom

-(void)drawTextInRect:(CGRect)rect{
    UIEdgeInsets insets = {5, 5, 5, 5};
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

@end
这将允许您在标签边缘周围留有边距 您还可以创建一个包含两个自定义标签的uiview,并相应地将其左对齐和右对齐。将uvview用作两个标签的容器,以便根据需要对其进行定位。我想这会产生你想要的效果

编辑:刚刚看到您对表格单元格的编辑。您需要创建自定义单元格和自定义uilabel

@interface CustomCell : UITableViewCell {

}


@property (retain, nonatomic) UILabelCustom *label1,*label2;


@end

@implementation CustomCell

@synthesize label1, label2;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)dealloc
{

    [label1 release];
    [label2 release];


    [super dealloc];
}

@end

在cellForRowAtIndexPath方法中,只需创建一个带有2个UILabel的单元格,并为它们指定所需的位置。小例子:

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}    
  UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 50, 20)];
  UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(80, 10, 50, 20)];
  label1.textAlignment = UITextAlignmentLeft;
  label2.textAlignment = UITextAlignmentRight;
  //other labels customization and add them to your cell
  return cell;
}
在附近没有SDK的情况下执行,因此可能会出现一些错误


希望它对您的cellForRowAtIndexPath方法有所帮助,只需创建一个包含2个UILabel的单元格,并为它们指定所需的位置即可。小例子:

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}    
  UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 50, 20)];
  UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(80, 10, 50, 20)];
  label1.textAlignment = UITextAlignmentLeft;
  label2.textAlignment = UITextAlignmentRight;
  //other labels customization and add them to your cell
  return cell;
}
在附近没有SDK的情况下执行,因此可能会出现一些错误


希望有帮助

你能展示一些图片吗?对不起,我写错标签了。它的表格行。你可以借助自定义单元格,在你的tableview中实现同样的功能,并让它看起来像你喜欢的那样。有几个链接可用:我最喜欢的是和。希望对你们有帮助。你们能展示一些图片吗?对不起,我写错标签了。它的表格行。你可以借助自定义单元格,在你的tableview中实现同样的功能,并让它看起来像你喜欢的那样。有几个链接可用:我最喜欢的是和。希望这对你有帮助。你的意思是我应该在每个单元格中添加两个标签,然后按我想要的方式定位它们?是的,你会在IB或代码中的自定义单元格中定位它们一次,然后当表加载该自定义单元格时,它们会在你想要的位置。你的意思是我应该在每个单元格中添加两个标签,然后按我想要的方式定位它们?是的,你知道吗将它们放置在IB或代码中的自定义单元格中一次,然后当表加载该自定义单元格时,它们将位于您想要的位置