宏定义一个PCH文件IOS->other-PCH File
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
//不要分号";"
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#endif /* PrefixHeader_pch */
=============================================================================
//获取屏幕的尺寸
// 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=@"Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!";
//字体的大小
//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
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];
================================================
自动适配高和宽
[label1 sizeToFit];
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];
}
//响应事件
-(void)btnEvent
{
[_labelsizeToFit];
}
-(void)changeCountText
{
_label.text=_textField.text;
}
跑马灯:
@interface ViewController ()
@property(strong,nonatomic)UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:@selector(time:) userInfo:nil repeats:YES];//每隔0.7运行一遍
}
//响应事件
-(void)time:(NSTimer *)time
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
_label.textColor = [UIColor redColor];
});//延迟0.3执行里面的方法
_label.textColor=[UIColor greenColor];
}