14

我想创建一个非常简单的 customView,上面有几个 UIlabel,我应该怎么做。任何教程或建议将不胜感激。我是新手,以前没试过。

我用xib试过这个。

@interface MyCustomView : UIView

@property (strong, nonatomic) IBOutlet UILabel *Label;

@end

执行

#import "MyCustomTimer.h"
@implementation MyCustomView
-(id)initWithCoder:(NSCoder *)aDecoder{
      if ((self = [super initWithCoder:aDecoder])){
      [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self     options:nil] objectAtIndex:0]];
       }
     return self;
}
@end

但我需要以编程方式进行,请帮助。谢谢

4

5 回答 5

30

这是一个简单的方法,希望对你有所帮助。

//in subclassed UIView 
#import "CustomView.h"
@implementation CustomView

 - (id)initWithFrame:(CGRect)frame
 { 
  self = [super initWithFrame:frame];
  if (self) {
    // Initialization code
    // initilize all your UIView components
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(20,30, 200, 44)];
    label1.text = @"i am label 1";
    [self addSubview:label1]; //add label1 to your custom view

    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(20,80, 200, 44)];
    label2.text = @"i am label 2";
    [self addSubview:label2]; //add label2 to your custom view



    [label1 release];//i am using without ARC, comment if u are using ARC
    [label2 release];//i am using without ARC, comment if u are using ARC
  }
   return self;
  }



   // in your class where u want to use that view
  #import "ViewController.h"
  #import "CustomView.h"//import it

  @interface ViewController ()

  @end

  @implementation ViewController

 - (void)viewDidLoad
  {
     [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    //create your view where u want
    CustomView *cv = [[CustomView alloc]initWithFrame:CGRectMake(10, 10, 230, 400)];   //create an instance of your custom view
     [self.view addSubview:cv]; // add to your main view
    [cv release];//comment if u are using ARC
 }


于 2013-08-01T13:06:39.520 回答
5

您说您不想使用 XIB 并希望以编程方式完成所有操作。

您需要实现该initWithFrame:方法:

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame])) {
        // create/initialize your subviews here
        self.myLabel = [[UILabel alloc] init...];

        // configure the label
        self.myLabel.font = ...;
        self.myLabel.autoresizingMask = ...;

        [self addSubview:self.myLabel];
    }

    return self;
}

因此,您创建和配置您的控件(字体、颜色、自动调整大小掩码等)并将它们添加为子视图,所有这些都来自该initWithFrame:方法。您可能希望将代码分解为不同的方法以保持清洁。

如果您使用自动布局,您还想从 init 方法创建所有约束。

如果您使用自动布局,则应实现该-layoutSubviews方法。这将在适当的时候调用来布局您的子视图(例如,当您的视图框架发生变化时):

- (void)layoutSubviews
{
    self.myLabel.frame = ...;
}

从该layoutSubviews方法中您可以self.bounds了解当时视图的大小。这将使您知道正确对齐或包装东西需要多少宽度/高度。

在创建视图实例时,只需使用[[MyCustomView alloc] init](将initWithFrame:使用空矩形调用)或[[MyCustomView alloc] initWithFrame:...]. 设置它的框架并将其添加到某个视图中。该layoutSubviews方法将在所有适当的时间被调用,并相应地进行布局。

于 2013-08-01T13:08:28.273 回答
3

更新您的类以实现initWithFrame:以及initWithCoder:,然后您可以调用:

[[MyCustomView alloc] initWithFrame:...];

在你的代码中。因此,您以编程方式创建了一个实例(已配置)。

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        UILabel *label = [[UILabel alloc] initWithFrame:...];
        // configure the label
        [self addSubview:label];
    }
 return self;
}
于 2013-08-01T12:50:46.510 回答
2

您可以将这些行放在您的 initWithFrame 中:

self = [super initWithFrame:frame]
if (self!=null) {
// if self has been allocated

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 40)];
[label setText:@"Label"];
[label setTextAlignment:NSTextAlignmentJustified];
[self addSubview:label];
}

这是一个非常简单的示例..仅添加一个标签,您可以以编程方式添加任意数量的对象..具有您想要的位置和框架大小以及您想要的任何属性...

也许您应该以编程方式搜索“UILabel ..

于 2013-08-01T13:05:25.067 回答
2

创建您自己的子类 UIView,然后覆盖 initWithFrame 或 drawRect。重写 initWithFrame 对于新手来说更容易。

您的标头 (.h) 文件应以此开头:

@interface YourOwnView : UIView

 ...

 @end

然后,在实现文件 (.m) 中,您应该实现自己的 initWithFrame。

 - (id)initWithFrame:(CGRect)frame {
     self = [super initWithFrame:frame];
     if (self) {
       //ADDING NEW CONTENT
       UILabel *yourLabel = [[UILabel alloc]init];
      //CUSTOMIZE your label's font, text, etc.
        ....

      //Add your label to your own view
      [self addSubview:yourLabel];
    }

    return self;
 }

您可能需要初始化程序中的额外数据或选项,例如标签的文本。在这种情况下,您可以定义一个带有额外参数的 init 方法,如下所示:

- (id)initWithFrame:(CGRect)frame andLabelText:(NSString *)labelText;
于 2013-08-01T13:08:46.330 回答