iOS - 标签

标签的使用

标签用于显示静态内容,由单行或多行组成。


重要属性

  • textAlignment
  • textColor
  • text
  • numberOflines
  • lineBreakMode

添加自定义方法 addLabel

实例


-(void)addLabel {
   UILabel *aLabel = [[UILabel alloc]initWithFrame:
   CGRectMake(20, 200, 280, 80)];
   aLabel.numberOfLines = 0;
   aLabel.textColor = [UIColor blueColor];
   aLabel.backgroundColor = [UIColor clearColor];
   aLabel.textAlignment = UITextAlignmentCenter;
   aLabel.text = @"This is a sample text\n of multiple lines.
   here number of lines is not limited.";
   [self.view addSubview:aLabel];
}

更新 ViewController.m 中的 viewDidLoad 如下 −

实例


- (void)viewDidLoad {
   [super viewDidLoad];
   
   //The custom method to create our label is called
   [self addLabel];
   // Do any additional setup after loading the view, typically from a nib.
}

输出

当我们运行应用程序时,我们将得到以下输出 −

iOS 教程

❮ iOS - UI 元素