IOS界面開發之UILabel

這裏記錄了UILabel的一些常見操作,和關閉當前界面的代碼。

//
//  LabelViewController.m
//  UIViewDemo
//
//  Created by dcr on 2016/12/16.
//  Copyright © 2016年 All rights reserved.
//

#import "LabelViewController.h"

@interface LabelViewController ()

@end

@implementation LabelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    CGRect rect = [[UIApplication sharedApplication] statusBarFrame];
    UIButton *btnImageView = [UIButton buttonWithType:UIButtonTypeSystem];
    btnImageView.frame = CGRectMake(0, rect.size.height, 100, 40);
    btnImageView.backgroundColor = [UIColor whiteColor];
    btnImageView.titleLabel.font = [UIFont systemFontOfSize:16];
    [btnImageView setTitle:@"返回" forState:UIControlStateNormal];
    [btnImageView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnImageView addTarget:self action:@selector(btnBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnImageView];
    
    //文本標籤
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(10, 100, 250, 200);
    label.backgroundColor = [UIColor yellowColor];
    //設置文本
    label.text = @"學習IOS記錄,Foundation框架學習,由於Swift語言一直有在更新,且IOS開發中有很多庫都是使用Objective-C語言的,所以Objective-C的學習還是很有必要的,下面是對NSString學習的一些記錄。Foundation框架學習之NSArray和NSMutableArray";
    //文字佈局模式
    label.textAlignment = NSTextAlignmentCenter;
    //文字顏色,可自定義顏色值
    label.textColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
    //label這透明
    //label.alpha = 0.5;
    //設置字體
    //label.font = [UIFont systemFontOfSize:20];
    //加粗
    //label.font = [UIFont boldSystemFontOfSize:20];
    //傾斜,只對英文有效
    label.font = [UIFont italicSystemFontOfSize:20];
    //遍歷所有字體
    for(NSString *name in [UIFont familyNames]){
        NSLog(@"font family name = %@", name);
    }
    //設置系統支持的任意字體
    label.font = [UIFont fontWithName:@"Times New Roman" size:25];
    //設置陰影
    //label.shadowColor = [UIColor redColor];
    //label.shadowOffset = CGSizeMake(2, 2);
    
    //換行自適應大小
    //換行模式 一般選擇字符換行,還有單詞NSLineBreakByWordWrapping
    label.lineBreakMode = NSLineBreakByCharWrapping;
    //顯示行數  0或者-1表示不限制行數
    label.numberOfLines = 0;
    
    //根據字符串的大小計算Label的大小
    CGSize size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(250, 10000) lineBreakMode:NSLineBreakByWordWrapping];
    //根據字符區域大小重新設置label的大小
    label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, size.width, size.height);
    [self.view addSubview:label];
}

- (void)btnBack{
    //關閉當前窗口
    [self dismissViewControllerAnimated:true completion:^{
        NSLog(@"LabelViewController back completion");
    }];
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

完整示例代碼下載:http://download.csdn.net/detail/deng0zhaotai/9715127

發佈了213 篇原創文章 · 獲贊 121 · 訪問量 85萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章