学习中ing...

masonry自适应文字大小

2017-01-11  本文已影响110人  那片阳光已醉
//
//  YellowViewController.m
//  MasonryTest
//
//  Created by 舒通 on 2017/1/11.
//  Copyright © 2017年 shutong. All rights reserved.
//

#import <Masonry.h>
#import "YellowViewController.h"
#import "ViewController.h"

@interface YellowViewController ()

@property (nonatomic, strong) UIView *yellowView;
@property (nonatomic, strong) UIView *blueView;
@property (nonatomic, strong) UIView *greenView;
@property (nonatomic, strong) UILabel *textLabel;

@end

@implementation YellowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
    [self createView:self.index];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - creat UI
- (void) createView:(NSInteger)index {
    switch (index) {
        case 10:{
//            设置内边距
            [self.yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view).with.offset(10);
                make.top.equalTo(self.view).with.offset(10);
                make.right.equalTo(self.view).with.offset(-10);
                make.bottom.equalTo(self.view).with.offset(-10);
            }];
        }
            break;
        case 11:{
//            设置内边距
            [self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
//                下右不需要写负号,insets方法中已经为我们做了取反的操作了
                make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
            }];
            
            
        }
            break;
        case 12:{
            [self.greenView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.view);
         // 这里通过mas_equalTo给size设置了基础数据类型的参数,参数为CGSize的结构体
                make.size.mas_equalTo(CGSizeMake(300, 300));
            }];
            
            __weak typeof(self)weakSelf = self;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.greenView mas_updateConstraints:^(MASConstraintMaker *make) {
                    __strong typeof(weakSelf)blockSelf = weakSelf;
                    make.centerX.equalTo(blockSelf.view).offset(100);
                    make.size.mas_equalTo(CGSizeMake(100, 100));
                }];
            });
            
        }
            break;
        case 13:{
            self.textLabel.text = @"这是测试的字符串。能看到1、2、3个步骤,第一步当然是上传照片了,要上传正面近照哦。上传后,网站会自动识别你的面部,如果觉得识别的不准,你还可以手动修改一下。左边可以看到16项修改参数,最上面是整体修改,你也可以根据自己的意愿单独修改某项,将鼠标放到选项上面,右边的预览图会显示相应的位置。";
            
            [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.view);
                
//                设置宽度小于等于200
                make.width.lessThanOrEqualTo(@200);
//                设置高度大于等于10
                make.height.greaterThanOrEqualTo(@0);
            }];
            
            UIView *view = [[UIView alloc]init];
            view.backgroundColor = [UIColor redColor];
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.textLabel.mas_bottom).offset(10);
                make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width, 20));
            }];
        }
            break;
            
        case 14:{
            /**
             Masonry为我们提供了三个默认的方法,priorityLow()、priorityMedium()、priorityHigh(),这三个方法内部对应着不同的默认优先级。
             除了这三个方法,我们也可以自己设置优先级的值,可以通过priority()方法来设置。
             
             Masonry也帮我们定义好了一些默认的优先级常量,分别对应着不同的数值,优先级最大数值是1000。
             */
            [self.yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.view);
                make.width.equalTo(self.view).priorityLow();
                make.width.equalTo(@20).priorityHigh();
                make.height.equalTo(self.view).priority(200);
                make.height.equalTo(@100).priority(1000);
                
            }];
            
        }
            break;
            
        case 15: {
//             设置当前约束值乘以多少,例如这个例子是redView的宽度是self.view宽度的0.2倍
            [self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.view);
                make.height.mas_equalTo(30);
                make.width.mas_equalTo(self.view).multipliedBy(0.5);
            }];
        }
            break;
            
        default:
            break;
    }
    
}

#pragma mark - touch Event
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    ViewController *viewVC = [[ViewController alloc]init];
    [self dismissViewControllerAnimated:viewVC completion:^{
        
    }];
}


#pragma mark - setter / getter

- (UIView *)yellowView {
    if (_yellowView == nil) {
        _yellowView = [[UIView alloc]init];
        _yellowView.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:_yellowView];
    }
    return _yellowView;
}
- (UIView *)blueView {
    if (_blueView == nil) {
        _blueView = [[UIView alloc]init];
        _blueView.backgroundColor = [UIColor blueColor];
        [self.view addSubview:_blueView];
    }
    return _blueView;
}
- (UIView *)greenView {
    if (_greenView == nil) {
        _greenView = [[UIView alloc]init];
        _greenView.backgroundColor = [UIColor greenColor];
        [self.view addSubview:_greenView];
    }
    return _greenView;
}
- (UILabel *)textLabel {
    if (_textLabel == nil) {
        _textLabel = [UILabel new];
        _textLabel.font = [UIFont systemFontOfSize:14];
        _textLabel.textColor = [UIColor redColor];
        _textLabel.backgroundColor = [UIColor greenColor];
        _textLabel.numberOfLines = 0;
        [self.view addSubview:_textLabel];
    }
    return _textLabel;
}

/*
#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

上一篇下一篇

猜你喜欢

热点阅读