如何在表格视图行中写入两个或多个文本并以不同的方式放置它们?

发布于 2024-12-27 22:20:18 字数 214 浏览 2 评论 0原文

我有一个 UITableView,我想右对齐一行中应该右对齐的文本和应该左对齐的另一个文本。这两个文本都应该从行边框的一些空格开始。我不希望它们从行的边框开始。我怎样才能做到这一点?

在此处输入图像描述

我想在每一行中写入左对齐的文本以及右对齐的文本。

提前致谢。

I have a UITableView and i want to right a text in a row which should be right aligned and another text which should be left aligned. And both of those texts should start with some space from border of the row. I don't want them to start with the border of the row. How can i do that?

enter image description here

I want to write text in every row which is left aligned and also a text which is right alighned.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不寐倦长更 2025-01-03 22:20:18

在您的 cellForRowAtIndexPath 方法中,只需创建一个具有 2 个 UILabels 的单元格,并为它们提供所需的位置。小例子:

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,所以可能会出现一些错误。

希望有帮助

In your cellForRowAtIndexPath method just create a cell with 2 UILabels and give them the desired location. Small example:

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

did it without SDK nearby, so there might be some mistakes.

Hope it helps

樱娆 2025-01-03 22:20:18

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

UILabel is very limited in terms of styling and layout. Considering either using multiple layouts or CoreText.

亢潮 2025-01-03 22:20:18

您可以做的是对标签进行子类化并重写其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

您就可以在标签边缘周围留出边距
您还可以创建一个包含 2 个自定义标签的 uiview,并相应地左右对齐它们。使用 uvview 作为 2 个标签的容器,根据需要放置它们。我认为这可以创建您正在寻找编辑的效果

:刚刚看到您的编辑是针对表格单元格的。您需要创建一个自定义单元格以及自定义 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

what you could do is subclass the label and over write its drawtextinrect function like so

@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

this would allow you to give a margin around the edges of the label
also you could create a uiview that contains 2 of this custom labels and left and right align them accordingly. using the uvview as a container for the 2 labels to position them as needed. i reckon that could create the effect you looking for

edit: just seen your edit was for table cell. you would need to create a custom cell as well as custom 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文