iOS UI 輸出!標籤 UILabel

輸出!標籤 UILabel

UILabel 它是一個唯讀的文字框,繼承UIView物件,所以不具任何輸入功能,但提供可以增加文字的陰影與變更陰影顏色的效果。

執行結果

Swift 程式範例

//-----------start-----------
//
//  ViewController.swift
//
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Label初始化
        let dyLabel: UILabel = UILabel(frame: CGRectMake(0,0,200,70))

        // Label背景顏色
        dyLabel.backgroundColor = UIColor.orangeColor()

        // 遮罩功能是否開啟
        dyLabel.layer.masksToBounds = true

        // 遮罩功能開啟後指定圓角大小
        dyLabel.layer.cornerRadius = 10.0

        //文字內容
        dyLabel.text = "哈囉!Swift!!"

        //文字陰影偏移
        dyLabel.shadowOffset = CGSize(width: 1, height: 1)

        //文字陰影的顏色
        dyLabel.shadowColor = UIColor.blackColor()

        //文字大小
        dyLabel.font = UIFont.boldSystemFontOfSize(23)

        // 文字顏色
        dyLabel.textColor = UIColor.brownColor()

        // 文字對齊方式為:中央對齊
        dyLabel.textAlignment = NSTextAlignment.Center

        // layer座標指定,init Label時會將Layer座標設定與frame相同
        dyLabel.layer.position = CGPoint(x: self.view.bounds.width/2,y: 100)

        // view 中的背景顏色
        self.view.backgroundColor = UIColor.whiteColor()

        // 將設定好的Label加入原本的view
        self.view.addSubview(dyLabel)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


}
//------------end------------

Objective-C 程式範例

//-----------start-----------
//
//  ViewController.m
//
//
//

#import "ViewController.h"
#import <UIKit/UIKit.h>




@interface ViewController ()

@end

@implementation ViewController
{
    UILabel *dyLabel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 初始化
    dyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 70)];
    dyLabel.backgroundColor = [UIColor orangeColor];
    dyLabel.layer.masksToBounds = TRUE;
    dyLabel.layer.cornerRadius = 10.0;
    dyLabel.text = @"哈囉!Swift!!";
    dyLabel.shadowOffset = CGSizeMake(1, 1);
    dyLabel.shadowColor = [UIColor blackColor];
    dyLabel.font = [UIFont boldSystemFontOfSize:23];
    dyLabel.textColor = [UIColor brownColor];
    dyLabel.textAlignment = NSTextAlignmentCenter;
    dyLabel.layer.position = CGPointMake(self.view.frame.size.width/2, 100);
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:dyLabel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

//------------end------------

參考資料

UILabel Class