<span style="font-size:18px;">//获取屏幕的尺寸
// UIScreen *screen=[UIScreen mainScreen];
// CGFloat width=screen.bounds.size.width;
// CGFloat height=screen.bounds.size.height;
// CGFloat width=[UIScreen mainScreen].bounds.size.width;
// CGFloat heighth=[UIScreen mainScreen].bounds.size.height;
//UILabel
//初始化Lable
UILabel *label=[[UILabelalloc]initWithFrame:CGRectZero];
// UILabel *label=[[UILabel alloc]init];
//左上角x,y width:宽度 height:高
label.frame=CGRectMake(ScreenWidth/2-150,ScreenHeight/2-150,300,300);
//文本内容
label.text=@"#英文美句#曾经拥有的,不要忘记。不能得到的,更要珍惜。属于自己的,不要放弃。已经失去的,留作回忆。 Don’t forget the things you once you owned. Treasure the things youcan’t get. Don't give up the things that belong to you and keep thoselost things in memory.";
//字体的大小
//label.font=[UIFont systemFontOfSize:12];
//找出所有的字体样式
NSArray *arr=[UIFontfamilyNames];
NSLog(@"%@",arr);
//只对英文和数字有效
//label.font=[UIFont fontWithName:@"Apple Color Emoji" size:20];
label.font=[UIFontsystemFontOfSize:17];//加粗
//在label中使文本换行
label.numberOfLines=0;//0代表不限制label的行数
// label.numberOfLines=2;//显示2行
//文本对齐方式默认是左对齐NSTextAligmentLeft
label.textAlignment=NSTextAlignmentCenter;//居中
//设置文字过长是设置文字格式
label.lineBreakMode=NSLineBreakByTruncatingMiddle;
//更改文本颜色
label.textColor=[UIColorblueColor];
//背景颜色
label.backgroundColor=[UIColoryellowColor];
[self.viewaddSubview:label];//放到view
</span>
<span style="font-size:18px;">UILabel *label1=[[UILabelalloc]initWithFrame:CGRectZero];
NSString *string =@"让你小的没心没肺的那个人,是最爱你的人";
//label1 .frame=CGRectMake(ScreenWidth/2-150, ScreenHeight/2-150, 300, 300);
label1.text=string;
label1.backgroundColor=[UIColoryellowColor];
label1.font=[UIFontsystemFontOfSize:18];
// 得到文字的大小
CGSize size=[string sizeWithAttributes:@{NSFontAttributeName:label1.font}];
// label1.frame=CGRectMake(ScreenWidth/2-size.width/2, ScreenHeight/2-size.height/2, size.width, size.height);
label1.frame=CGRectMake(ScreenWidth/2-150,ScreenHeight/2-size.height/2,300, size.height);
[self.viewaddSubview:label1];</span>
<span style="font-size:18px;">================================================
自动适配高和宽
[label1 sizeToFit];</span>
<span style="font-size:18px;">sizeToFit的应用:
@interface ViewController ()
@property(strong,nonatomic)UITextField *textField;
@property(strong,nonatomic)UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//按钮
UIButton *btn=[[UIButtonalloc] initWithFrame:CGRectMake(60,50, 200, 40)];//确定按钮位置
btn.backgroundColor=[UIColorgrayColor];//按钮颜色
[btn setTitle:@"使用sizeToFit"forState:UIControlStateNormal];//按钮上显示的文字和状态
[btn addTarget:selfaction:@selector(btnEvent)forControlEvents:UIControlEventTouchUpInside];//控件响应
[self.viewaddSubview:btn];//添加到视图上
//textField
_textField=[[UITextFieldalloc] initWithFrame:CGRectMake(60,100, 200, 30)];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(changeCountText)name:UITextFieldTextDidChangeNotificationobject:nil];//添加监听者,当文本被被改变是通知给changeCountText
_textField.backgroundColor=[UIColorpurpleColor];
[self.viewaddSubview:_textField];
//label
_label=[[UILabelalloc]initWithFrame:CGRectMake(60,150, 40, 20)];
_label.backgroundColor=[UIColororangeColor];
[self.viewaddSubview:_label];</span>