这里记录了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