UIView UILabel UIButton知识整理

来源:互联网 发布:c语言编写银行家算法 编辑:程序博客网 时间:2024/05/16 10:40

UIView

实例化

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];    view.backgroundColor = [UIColor colorWithRed:12.0/255.0 green:0 blue:123.0/255.0 alpha:1.0];    view.alpha = 0.5;//单独进行设置时 父视图透明度会影响子视图    view.hidden = NO;    [self.window addSubview:view]; 

位置及大小

//前面两个坐标影响view本身坐标系原点位置 向左上移动    view.center = CGPointMake (x,y);    view.bounds = CGRectMake(0,0,width,height); 

变形 .transform, 以中心点为变形 中心点不变 执行一个

//大小变形:CGAffineTransformMakeScale(sx, sy) 倍数    view.transform = CGAffineTransformMakeScale(1.5 , 0.5);//角度变形 M_PI  M_PI_2(正 顺转 )    view.transform = CGAffineTransformMakeRotation(M_PI_2/2);

圆角 .layer

//圆角半径    view.layer.cornerRadius = 33.0;//边框 默认宽0 黑色    view.layer.borderWidth = 3.0;    view.layer.borderColor = [UIColor orangeColor].CGColor;//切割子视图超出部分 默认NO 只有NO时有阴影效果    view.layer.masksToBounds = YES;//使用贝塞尔曲线绘制- (void)drawRect:(CGRect)rect  {    CGRect bounds = self.bounds;    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:8. 0] addClip];    [self.image drawInRect:bounds];}

切割

//记录之前的图片frame    CGRect viewFrame = imageView.frame; //设置可裁剪属性    imageView.clipsToBounds = YES;     imageView.frame = viewFrame;

阴影 .layer.shadowOpacity

//透明度 默认完全透明     view.layer.shadowOpacity = 0.6;//阴影偏移     view.layer.shadowOffset = CGSizeMake(10,10);//阴影颜色     view.layer.shadowColor = [UIColor darkGrayColor].CGColor;//阴影圆角 羽化     view.layer.shadowRadius = 20.0;

层次关系

//移除视图 视图以数组下标形式保存    [view3 removeFromSuperview];//将视图移动到最上面    [self.view bringSubviewToFront:view1];//将视图移动到最下面    [self.view sendSubviewToBack:view5];//将某个视图移动到某视图上面    [self.view insertSubview:view2 aboveSubview:view4];//将某个视图移动到某视图下面    [self.view insertSubview:view1 belowSubview:view2];//将某个视图移动到指定位置    [self.view insertSubview:view1 atIndex:3];//交换两个视图    [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:2];//超出父视图范围的button无响应,切掉多余部分     view.clipsToBounds = YES

UILabel

实例化

     label.text = @"Hello world!You love Xiao Mi";     label.textColor = [UIColor blackColor];

文字

//设置字体     label.font = [UIFont systemFontOfSize:float];//粗体     label.font = [UIFont boldSystemFontOfSize:15.0];//斜体 英文     label.font = [UIFont italicSystemFontOfSize:25.0];//字体文字     label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.0];//遍历系统字体     NSArray *array = [UIFont familyNames];     for (NSString *type in array) {          NSArray *nameArray = [UIFont fontNamesForFamilyName:type];          for (NSString *name in nameArray) {               NSLog(@"%@",name);}     }

对齐

//对齐方式 默认左对齐     label.textAlignment = NSTextAlignmentCenter;//行数 默认一行  0是自动换行     label.numberOfLines = 2;//自适应文字大小 默认NO     label.adjustsFontSizeToFitWidth = YES;//自适应label高度     [label sizeToFit];//设置圆角时     label.clipsToBounds = YES;     label.layer.cornerRadius = 10.0;//参与互动。父视图用户影响,只有label和imageview默认NO     label.userInteracetionEnabled = YES; 

UIButton

实例化

    UIButton *but1 = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];//UIButtonTypeRoundedRect iOS7之前有效,有圆角效果    UIButton *but2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

按钮是否可用

     but.enabled = YES;

设置文字

     [but2 setTitle:@"Normal" forState:UIControlStateNormal];/*     UIControlStateNormal      一般状态     UIControlStateHighlighted 高亮状态 点中出现     UIControlStateDisabled    不可用时的*/

颜色

    [but2 setTitleColor:<#(UIColor *)#> forState:<#(UIControlState)#>]

大小

    but2.titleLabel.font = [UIFont systemFontOfSize:22.0];

添加事件 点击事件必须实现

//带参数的写法     [but2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];     // UIControlEventTouchUpInside         里进里出     // UIControlEventTouchUpOutside        里进外出     // UIControlEventTouchDragOutside      里进外拖拽     // UIControlEventTouchDragInside       里进里拖拽     // UIControlEventTouchDragExit         拖拽出去     // UIControlEventTouchDragEnter        拽出拽回     // UIControlEventTouchDownRepeat       双击     // UIControlEventTouchDown             点中即输出

添加图片 setImage

     //frame 小于等于图片大小 压缩或正常显示     //frame 大于图片大小    图按原大小显示     //文字显示在图片右边     [button setImage:img forState:UIControlStateNormal];

背景图

     //frame小于等于图片大小 压缩或正常显示     //frame大于图片大小    图会拉伸     //文字显示在图片中间     [button setBackgroundImage:[UIImage imageNamed:@"map.png"] forState:UIControlStateNormal];
0 0