0:首先还是通过纯的代码来实现
0:删除3个文件ViewController.h,ViewController.m,Main.storyboard
1:修改点击左边的蓝色按钮,然后选择general-》developer info-》main interface ,将这个main interface 晴空
2:然后再创建一个MainUIViewController ,它继承自UIViewController
1:AppDelegate.m的didfinshlaunchingwithoptions方法的更新
2:
//
// MainUiViewController.m
// FourthDemo
//
// Created by 千雅爸爸 on 16/10/9.
// Copyright ? 2016年 kodulf. All rights reserved.
//#import "MainUiViewController.h"@interface MainUiViewController ()@end@implementation MainUiViewController- (void)viewDidLoad {[super viewDidLoad];[self.view setBackgroundColor:[UIColor whiteColor]];//使用宏定义来注释掉if 0 endif
#if 0UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];[self.view addSubview:label];label.text = @"Hello World";//UILable 默认的就是没有背景色的label.backgroundColor =[UIColor greenColor];label.textColor = [UIColor whiteColor];// 字体颜色label.font = [UIFont systemFontOfSize:20];//字体大小label.textAlignment = NSTextAlignmentCenter;//居中显示//查找支持的肢体NSArray *fontList = [UIFont familyNames];NSLog(@"字体%@",fontList);//设置字体和大小label.font =[UIFont fontWithName:@"Papyrus" size:20];label.text=@"888888888888888888888/默认的行数为1,如果是0表示无数行;这里是88888888888888888888888888888888888888888888888888888888888";label.numberOfLines=4;//默认的行数为1,如果是0表示无数行;这里是4行,如果只能显示3行,显示不下了还是使用省略号//考虑文本的大小根据文本的内容改变++++++开始+++++++label.numberOfLines = 0;CGSize maxSize = CGSizeMake(CGRectGetWidth(label.frame), CGFLOAT_MAX);//这里的lable.frame来获取长度,CGSize textSize = [label.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Papyrus" size:20]} context:nil].size;//NSStringDrawingUsesLineFragmentOrigin如果没有这个选项,可能会导致我们计算出错,这个事必须加入的//NSStringDrawingUsesFontLeading 考虑行高的计算的//attributes 是label 原有的属性例如这里是attributes:{NSFontAttributeName:[UIFont fontWithName:@"Papyrus" size:20]}//context一般都是传nil//别忘了最后的.sizeCGRect frame = label.frame;frame.size = textSize;//更改sizelabel.frame = frame;//考虑文本的大小根据文本的内容改变++++++结束+++++++
#endif//风格化的文本显示内容NSString *aString = @"iOS";UILabel *uiLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, 375, 45)];[self.view addSubview:uiLabel];uiLabel.text=aString;uiLabel.textAlignment = NSTextAlignmentCenter;NSLog(@"属性的文字%@",uiLabel.attributedText);NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:aString];//可变的属性后的字符串,[attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50] range:NSMakeRange(0, 1)];//这个太溜了,可以分开来定义字体,和java相比太溜了。[attributeString addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 1)];[attributeString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(1, 2)];uiLabel.attributedText = [attributeString copy];//可以直接赋值,但是为了防止误操作,因为attributeString是可变的,而uiLabel.attributedText是不可变的,所以最好是用copy来做// Do any additional setup after loading the view.
}- (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