UIView易被忽略的基础知识

 

  • UILabel
  1. 设置属性字符串
  2. 根据字体自动调整宽度
  3. 粗体、斜体
  4. 根据文本自适应高度/宽度

 

1     UILabel *lable;
2     lable.attributedText; //设置属性字符串;
3     lable.adjustsFontSizeToFitWidth;//根据字体大小自动调整宽度
4     
5     UIFont;
6     [UIFont boldSystemFontOfSize:(CGFloat)];//设置加粗字体大小
7     [UIFont italicSystemFontOfSize:(CGFloat)];//设置斜体字体大小
8     //根据文字计算宽或者高
9     [text boundingRectWithSize:(CGSize) options:(NSStringDrawingOptions) attributes:(nullable NSDictionary<NSString *,id> *) context:(nullable NSStringDrawingContext *)];

 

 

  • UIButton

  1. 工厂模式初始化
  2. 内容/图片/标题 偏移
  3. 设置属性字符串
  4. 图片内容模式
  5. 内容模式
  6. 两种设置图片的区别 
 1     UIButton *btn;
 2     [UIButton buttonWithType:(UIButtonType)];//初始化(工厂模式)
 3     btn.contentEdgeInsets;//内容偏移
 4     btn.imageEdgeInsets;//图片偏移
 5     btn.titleEdgeInsets;//标题偏移
 6     //设置属性字符串标题
 7     - (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)
 8     //内容模式
 9     btn.imageView.contentMode;
10     //两种设置图片的区别
11     [UIImage imageNamed:@""];//有缓存
12     [[UIImage alloc] initWithContentsOfFile:(nonnull NSString *)];//没有缓存

 

#pragma mark - imageView contentMode

 1 - (void)testImageContentMode
 2 {
 3     //image默认是填充整个区域
 4     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"angry_01"]];
 5     imageView.backgroundColor = [UIColor blackColor];
 6     imageView.frame = CGRectMake(0, 0, 200, 500);
 7     //内容模式:按最佳比例填充
 8     imageView.contentMode = UIViewContentModeScaleAspectFit;
 9     [self.view addSubview:imageView];
10 }

 

 

#pragma mark - 测试按钮image和background的区别、获取按钮信息

 1 - (void)testButtonImageAndBackgroundImageDiff
 2 {
 3     UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
 4     button.frame = CGRectMake(0, 0, 100, 100);
 5     [button setTitle:@"正常状态" forState:UIControlStateNormal];
 6     [button setTitle:@"高亮" forState:UIControlStateHighlighted];
 7     [button setTitle:@"登录" forState:UIControlStateSelected];
 8     button.backgroundColor = [UIColor redColor];
 9     button.center = self.view.center;
10     button.selected = YES;
11     
12     //注意:setImage 和 setBackgroundImage可以共存。
13     
14     //setImage: 当图片的大小大于按钮,图片会等比缩放。反之图片大小不变。
15     [button setImage:[UIImage imageNamed:@"fart"] forState:UIControlStateNormal];
16     
17     //setBackgroundImage: 拉伸图片,保证填充整个区域
18     [button setBackgroundImage:[UIImage imageNamed:@"usercenter_hd_checktag"] forState:UIControlStateNormal];
19     
20     [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
21     [self.view addSubview:button];
22     
23 }
24 
25 - (void)buttonClick:(UIButton *)button
26 {
27     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, 50, 50)];
28     [self.view addSubview:imageView];
29     
30     //获取当前的按钮image
31     imageView.image = button.currentImage;
32     
33     NSLog(@"%d",button.selected);
34     
35     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
36         //获取当前的文字
37         NSLog(@"%@",button.currentTitle);
38     });
39     
40 #if 0
41     //获取指定状态的文字
42     NSLog(@"%@",[button titleForState:UIControlStateSelected]);
43 #endif
44 }

 

 

  • UIView
  1. 特定位置插入一个视图

  2. 把试图放到前面

  3. 把试图方法后面

  4. 交换两个试图

  5. 删除一个视图

  6. 剪切一个视图超出父视图之外的部分

 1     UIView;
 2     1.如何在特定位置插入一个视图;
 3     [self.view insertSubview:<#(nonnull UIView *)#> aboveSubview:<#(nonnull UIView *)#>];
 4     [self.view insertSubview:<#(nonnull UIView *)#> atIndex:<#(NSInteger)#>];
 5     [self.view insertSubview:<#(nonnull UIView *)#> belowSubview:(nonnull UIView *)];
 6     
 7     2.如何把试图放到前面;
 8     [self.view bringSubviewToFront:<#(nonnull UIView *)#>];
 9     
10     3.如何把试图方法后面
11     [self.view sendSubviewToBack:<#(nonnull UIView *)#>];
12     
13     4.交换两个试图
14     [self.view exchangeSubviewAtIndex:<#(NSInteger)#> withSubviewAtIndex:<#(NSInteger)#>];
15     
16     5.如何删除一个视图
17     [self.view removeFromSuperview];
18     
19     6.如何剪切一个视图超出父视图之外的部分;
20     self.view.clipsToBounds = YES;

 

  • UIView易错点
  1. UIView在界面上不显示:

         1.没有创建。

         2.没有设置大小或者坐标问题

         3.没有加到父视图上

         4.隐藏

         5.移除了。

         6.被覆盖了。

         7.背景颜色

  2. 数组没有数据:

         1.没有初始化

         2.移除了元素

         3.没有添加元素

 

#pragma mark - bounds

 1 //思考frame,bounds,center区别
 2 - (void)testBounds
 3 {
 4     /*
 5      1.修改bounds(x,y)不会影响自身的位置.会影响子视图的位置。
 6      2.bounds(x,y)默认是(0,0)
 7      */
 8     
 9     //红色
10     UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 300, 300)];
11     v1.bounds = CGRectMake(50, 50, 300, 300);
12     
13     v1.backgroundColor = [UIColor redColor];
14     [self.view addSubview:v1];
15     
16     //黑色
17     UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
18     v2.backgroundColor = [UIColor blackColor];
19     [v1 addSubview:v2];
20     
21 }

 

#pragma mark - UIView的clipsToBounds

- (void)testClipsToBounds
{
    UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 100, 100)];
    //剪切超过父视图的范围
    v1.clipsToBounds = YES;
    v1.backgroundColor = [UIColor redColor];
    [self.view addSubview:v1];
    
    UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(50, 60, 100, 100)];
    v2.backgroundColor = [UIColor blackColor];
    [v1 addSubview:v2];
}

 

posted on 2016-01-12 10:39  Wilson_CYS  阅读(422)  评论(0编辑  收藏  举报

导航