IOS自适应前段库-Masonry的使用

Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性,而且同时支持 iOS 和 Max OS X。Masonry是一个用代码写iOS或OS界面的库,可以代替Auto layout。Masonry的github地址:https://github.com/SnapKit/Masonry

本章内容

- Masonry配置

- Masonry使用

- Masonry实例

Masonry配置

- 推荐使用pods方式引入类库,pod ‘Masonry‘,若不知道pod如何使用,情况我的另一篇文章: 提高ios开发效率的工具

- 引入头文件 #import "Masonry.h"

Masonry使用讲解

mas_makeConstraints 是给view添加约束,约束有几种,分别是边距,宽,高,左上右下距离,基准线。添加过约束后可以有修正,修正有offset(位移)修正和multipliedBy(倍率)修正。

语法一般是 make.equalTo or make.greaterThanOrEqualTo or make.lessThanOrEqualTo + 倍数和位移修正。

注意点1: 使用 mas_makeConstraints方法的元素必须事先添加到父元素的中,例如[self.view addSubview:view];

注意点2: masequalTo 和 equalTo 区别:masequalTo 比equalTo多了类型转换操作,一般来说,大多数时候两个方法都是 通用的,但是对于数值元素使用mas_equalTo。对于对象或是多个属性的处理,使用equalTo。特别是多个属性时,必须使用equalTo,例如 make.left.and.right.equalTo(self.view);

注意点3: 注意到方法with和and,这连个方法其实没有做任何操作,方法只是返回对象本身,这这个方法的左右完全是为了方法写的时候的可读性 。make.left.and.right.equalTo(self.view);和make.left.right.equalTo(self.view);是完全一样的,但是明显的加了and方法的语句可读性 更好点。

Masonry初级使用例子


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

-(void)exp1{

    UIView *view = [UIView new];

    [view setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:view];

    [view mas_makeConstraints:^(MASConstraintMaker *make) {

         make.center.equalTo(self.view);

         make.size.mas_equalTo(CGSizeMake(400,400));

    }];

}

-(void)exp2{

    UIView *view = [UIView new];

    [view setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:view];

    [view mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));

        

        

        

        

    }];

}

-(void)exp3{

    UIView *view1 = [UIView new];

    [view1 setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:view1];

    UIView *view2 = [UIView new];

    [view2 setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:view2];

    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.mas_equalTo(self.view.mas_centerY);

        make.height.mas_equalTo(150);

        make.width.mas_equalTo(view2.mas_width);

        make.left.mas_equalTo(self.view.mas_left).with.offset(10);

        make.right.mas_equalTo(view2.mas_left).offset(-10);

    }];

    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.mas_equalTo(self.view.mas_centerY);

        make.height.mas_equalTo(150);

        make.width.mas_equalTo(view1.mas_width);

        make.left.mas_equalTo(view1.mas_right).with.offset(10);

        make.right.equalTo(self.view.mas_right).offset(-10);

    }];

}

Masonry高级使用例子1

iOS计算器使用Masorny布局:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

-(void)exp4{

    

    UIView *displayView = [UIView new];

    [displayView setBackgroundColor:[UIColor blackColor]];

    [self.view addSubview:displayView];

    UIView *keyboardView = [UIView new];

    [self.view addSubview:keyboardView];

    

    [displayView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.view.mas_top);

        make.left.and.right.equalTo(self.view);

        make.height.equalTo(keyboardView).multipliedBy(0.3f);

    }];

    [keyboardView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(displayView.mas_bottom);

        make.bottom.equalTo(self.view.mas_bottom);

        make.left.and.right.equalTo(self.view);

    }];

    

    UILabel *displayNum = [[UILabel alloc]init];

    [displayView addSubview:displayNum];

    displayNum.text = @"0";

    displayNum.font = [UIFont fontWithName:@"HeiTi SC" size:70];

    displayNum.textColor = [UIColor whiteColor];

    displayNum.textAlignment = NSTextAlignmentRight;

    [displayNum mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.and.right.equalTo(displayView).with.offset(-10);

        make.bottom.equalTo(displayView).with.offset(-10);

    }];

    

    NSArray *keys = @[@"AC",@"+/-",@"%",@"÷"

                     ,@"7",@"8",@"9",@"x"

                     ,@"4",@"5",@"6",@"-"

                     ,@"1",@"2",@"3",@"+"

                     ,@"0",@"?",@".",@"="];

    int indexOfKeys = 0;

    for (NSString *key in keys){

        

        indexOfKeys++;

        int rowNum = indexOfKeys %4 ==0? indexOfKeys/4:indexOfKeys/4 +1;

        int colNum = indexOfKeys %4 ==0? 4 :indexOfKeys %4;

        NSLog(@"index is:%d and row:%d,col:%d",indexOfKeys,rowNum,colNum);

        

        UIButton *keyView = [UIButton buttonWithType:UIButtonTypeCustom];

        [keyboardView addSubview:keyView];

        [keyView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [keyView setTitle:key forState:UIControlStateNormal];

        [keyView.layer setBorderWidth:1];

        [keyView.layer setBorderColor:[[UIColor blackColor]CGColor]];

        [keyView.titleLabel setFont:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30]];

        

        [keyView mas_makeConstraints:^(MASConstraintMaker *make) {

            

            if([key isEqualToString:@"0"] || [key isEqualToString:@"?"] ){

                if([key isEqualToString:@"0"]){

                    [keyView mas_makeConstraints:^(MASConstraintMaker *make) {

                        make.height.equalTo(keyboardView.mas_height).with.multipliedBy(.2f);

                        make.width.equalTo(keyboardView.mas_width).multipliedBy(.5);

                        make.left.equalTo(keyboardView.mas_left);

                        make.baseline.equalTo(keyboardView.mas_baseline).with.multipliedBy(.9f);

                    }];

                }if([key isEqualToString:@"?"]){

                    [keyView removeFromSuperview];

                }

            }

            

            else{

                make.width.equalTo(keyboardView.mas_width).with.multipliedBy(.25f);

                make.height.equalTo(keyboardView.mas_height).with.multipliedBy(.2f);

                

                switch (rowNum) {

                    case 1:

                    {

                        make.baseline.equalTo(keyboardView.mas_baseline).with.multipliedBy(.1f);

                        keyView.backgroundColor = [UIColor colorWithRed:205 green:205 blue:205 alpha:1];

                    }

                        break;

                    case 2:

                    {

                        make.baseline.equalTo(keyboardView.mas_baseline).with.multipliedBy(.3f);

                    }

                        break;

                    case 3:

                    {

                        make.baseline.equalTo(keyboardView.mas_baseline).with.multipliedBy(.5f);

                    }

                        break;

                    case 4:

                    {

                        make.baseline.equalTo(keyboardView.mas_baseline).with.multipliedBy(.7f);

                    }

                        break;

                    case 5:

                    {

                        make.baseline.equalTo(keyboardView.mas_baseline).with.multipliedBy(.9f);

                    }

                        break;

                    default:

                        break;

                }

                

                switch (colNum) {

                    case 1:

                    {

                        make.left.equalTo(keyboardView.mas_left);

                    }

                        break;

                    case 2:

                    {

                        make.right.equalTo(keyboardView.mas_centerX);

                    }

                        break;

                    case 3:

                    {

                        make.left.equalTo(keyboardView.mas_centerX);

                    }

                        break;

                    case 4:

                    {

                        make.right.equalTo(keyboardView.mas_right);

                        [keyView setBackgroundColor:[UIColor colorWithRed:243 green:127 blue:38 alpha:1]];

                    }

                        break;

                    default:

                        break;

                }

            }

        }];

    }

}

本例子使用的baseline去控制高度位置,这似乎不是太准,如果想要精准控制高度位置,可以使用一行一行添加的方法,每次当前行的top去equelTo上一行的bottom。 给个提示:


1

2

3

4

for(遍历所有行)

    for(遍历所以列)

    

    ......

下一个例子中,使用上面类似的方法

Masonry高级使用例子2

根据设计图,使用masonry布局:

步骤1

步骤2

步骤1


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

 -(void)createUI{

    UIView *titleView = [UIView new];

    titleView.backgroundColor = [UIColor redColor];

    UIView *caredView = [UIView new];

    [self.view addSubview:caredView];

    UIView *brifeView = [UIView new];

    [self.view addSubview:brifeView];

    

    self.view.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1.000];

    

    UIImageView *plantThrm = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"defalutPlantReferenceIcon"]];

    [self.view addSubview:plantThrm];

    [plantThrm mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.and.top.equalTo(self.view).with.offset(10);

    }];

    

       [self.view addSubview:titleView];

       UIImageView *bgTitleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg-plant-reference-title"]];

    [titleView addSubview:bgTitleView];

    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.equalTo(self.view.mas_right);

        make.left.equalTo(plantThrm.mas_right).with.offset(20);

        make.centerY.equalTo(plantThrm.mas_centerY);

   }];

    [bgTitleView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(titleView);

    }];

    UILabel *title = [[UILabel alloc]init];

    title.textColor =  [UIColor whiteColor];

    title.font = [UIFont fontWithName:@"Heiti SC" size:26];

    title.text = _reference.name;

    [titleView addSubview:title];

    [title mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(titleView.mas_left).offset(10);

        make.width.equalTo(titleView.mas_width);

        make.centerY.equalTo(titleView.mas_centerY);

    }];

    

    UILabel *caredTitle = [[UILabel alloc]init];

    caredTitle.textColor =  [UIColor colorWithRed:0.172 green:0.171 blue:0.219 alpha:1.000];

    caredTitle.font = [UIFont fontWithName:@"Heiti SC" size:10];

    caredTitle.text = @"植物养护";

    [self.view addSubview:caredTitle];

    [caredTitle mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(plantThrm.mas_bottom).with.offset(20);

        make.left.and.right.equalTo(self.view).with.offset(10);

        make.height.mas_equalTo(10);

    }];

    

    caredView.layer.cornerRadius = 5;

    caredView.layer.masksToBounds = YES;

    

    caredView.layer.borderWidth = 1;

    caredView.layer.borderColor = [[UIColor colorWithWhite:0.521 alpha:1.000] CGColor];

    caredView.backgroundColor = [UIColor whiteColor];

    [caredView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(caredTitle.mas_bottom).with.offset(5);

        make.left.equalTo(self.view.mas_left).with.offset(10);

        make.right.equalTo(self.view.mas_right).with.offset(-10);

        make.height.equalTo(brifeView);

    }];

    

    UILabel *brifeTitle = [[UILabel alloc]init];

    brifeTitle.textColor =  [UIColor colorWithRed:0.172 green:0.171 blue:0.219 alpha:1.000];

    brifeTitle.font = [UIFont fontWithName:@"Heiti SC" size:10];

    brifeTitle.text = @"植物简介";

    [self.view addSubview:brifeTitle];

    [brifeTitle mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(caredView.mas_bottom).with.offset(20);

        make.left.and.right.equalTo(self.view).with.offset(10);

        make.height.mas_equalTo(10);

    }];

    

    brifeView.layer.cornerRadius = 5;

    brifeView.layer.masksToBounds = YES;

    

    brifeView.layer.borderWidth = 1;

    brifeView.layer.borderColor = [[UIColor colorWithWhite:0.521 alpha:1.000] CGColor];

    brifeView.backgroundColor = [UIColor whiteColor];

    [brifeView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(brifeTitle.mas_bottom).with.offset(5);

        make.left.equalTo(self.view.mas_left).with.offset(10);

        make.right.equalTo(self.view.mas_right).with.offset(-10);

        make.bottom.equalTo(self.view.mas_bottom).with.offset(-10);

        make.height.equalTo(caredView);

    }];

}

完成之后如下图 步骤1

步骤2,在上面的基础上,增加植物养护部分ui构造的代码,思想是,先构造出四行,然后根据每行单独构造出行样式。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

-(void)createIndexUIWithView:(UIView *)view{

    

    UIView *row1 = [UIView new];

    UIView *row2 = [UIView new];

    UIView *row3 = [UIView new];

    UIView *row4 = [UIView new];

    [view addSubview:row1];

    [view addSubview:row2];

    [view addSubview:row3];

    [view addSubview:row4];

    [row1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.and.left.equalTo(view);

        make.height.equalTo(view.mas_height).multipliedBy(0.25);

        make.top.equalTo(view.mas_top);

    }];

    [row2 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.and.left.equalTo(view);

        make.top.equalTo(row1.mas_bottom);

        make.height.equalTo(view.mas_height).multipliedBy(0.25);

    }];

    [row3 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.equalTo(view.mas_right);

        make.top.equalTo(row2.mas_bottom);

        make.height.equalTo(view.mas_height).multipliedBy(0.25);

        make.left.equalTo(view.mas_left);

    }];

    [row4 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.and.left.equalTo(view);

        make.top.equalTo(row3.mas_bottom);

        make.height.equalTo(view.mas_height).multipliedBy(0.25);

    }];

    [self createIndexRowUI:PlantReferenceWaterIndex withUIView:row1];

    [self createIndexRowUI:PlantReferenceSumIndex withUIView:row2];

    [self createIndexRowUI:PlantReferenceTemperatureIndex withUIView:row3];

    [self createIndexRowUI:PlantReferenceElectrolyteIndex withUIView:row4];

}

-(void)createIndexRowUI:(PlantReferenceIndex) index withUIView:(UIView *)view{

    

    UILabel *indexTitle = [UILabel new];

    indexTitle.font = [UIFont fontWithName:@"HeiTi SC" size:14];

    indexTitle.textColor = [UIColor colorWithWhite:0.326 alpha:1.000];

    [view addSubview:indexTitle];

    [indexTitle mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(view.mas_left).with.offset(20);

        make.centerY.equalTo(view.mas_centerY);

    }];

    switch (index) {

        case PlantReferenceWaterIndex:

        {

            indexTitle.text = @"水分";

            UIImageView * current;

            for(int i=1;i<=5;i++){

                if(i<_reference.waterIndex){

                    current = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_water_light"]];

                }else{

                    current = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_water_dark"]];

                }

                [view addSubview:current];

                

                [current mas_makeConstraints:^(MASConstraintMaker *make) {

                    make.left.equalTo(view.mas_right).with.multipliedBy(0.12*(i-1) +0.3);

                    make.centerY.equalTo(view.mas_centerY);

                }];

            }

        }

              break;

        case PlantReferenceSumIndex:

        {

            indexTitle.text = @"光照";

            UIImageView * current;

            for(int i=1;i<=5;i++){

                if(i<_reference.temperatureIndex){

                    current = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_summer_light"]];

                }else{

                    current = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_summer_dark"]];

                }

                [view addSubview:current];

                

                [current mas_makeConstraints:^(MASConstraintMaker *make) {

                    make.left.equalTo(view.mas_right).with.multipliedBy(0.12*(i-1) +0.3);

                    make.centerY.equalTo(view.mas_centerY);

                }];

            }

        }

              break;

        case PlantReferenceTemperatureIndex:

        {

            indexTitle.text = @"温度";

            UIImageView * current;

            for(int i=1;i<=5;i++){

                if(i<_reference.sumIndex){

                    current = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_temperature_light"]];

                }else{

                    current = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_temperature_dark"]];

                }

                [view addSubview:current];

                

                [current mas_makeConstraints:^(MASConstraintMaker *make) {

                    make.left.equalTo(view.mas_right).with.multipliedBy(0.12*(i-1) +0.3);

                    make.centerY.equalTo(view.mas_centerY);

                }];

            }

        }

              break;

        case PlantReferenceElectrolyteIndex:

        {

            indexTitle.text = @"肥料";

            UIImageView * current;

            for(int i=1;i<=5;i++){

                if(i<_reference.electrolyteIndex){

                    current = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_electolyte_light"]];

                }else{

                    current = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_electolyte_dark"]];

                }

                [view addSubview:current];

                

                [current mas_makeConstraints:^(MASConstraintMaker *make) {

                    make.left.equalTo(view.mas_right).with.multipliedBy(0.12*(i-1) +0.3);

                    make.centerY.equalTo(view.mas_centerY);

                }];

            }

        }

            break;

        default:

            break;

    }

}

-(void)createUI{

    self.title = _reference.name;

    UIView *titleView = [UIView new];

    UIView *caredView = [UIView new];

    [self.view addSubview:caredView];

    UITextView *brifeView = [UITextView new];

    [self.view addSubview:brifeView];

    

    self.view.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1.000];

    

    UIImageView *plantThrm = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"defalutPlantReferenceIcon"]];

    [self.view addSubview:plantThrm];

    [plantThrm mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.and.top.equalTo(self.view).with.offset(10);

    }];

    

       [self.view addSubview:titleView];

       UIImageView *bgTitleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg-plant-reference-title"]];

    [titleView addSubview:bgTitleView];

    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.equalTo(self.view.mas_right);

        make.left.equalTo(plantThrm.mas_right).with.offset(20);

        make.centerY.equalTo(plantThrm.mas_centerY);

   }];

    [bgTitleView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(titleView);

    }];

    UILabel *title = [[UILabel alloc]init];

    title.textColor =  [UIColor whiteColor];

    title.font = [UIFont fontWithName:@"Heiti SC" size:26];

    title.text = _reference.name;

    [titleView addSubview:title];

    [title mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(titleView.mas_left).offset(10);

        make.width.equalTo(titleView.mas_width);

        make.centerY.equalTo(titleView.mas_centerY);

    }];

    

    UILabel *caredTitle = [[UILabel alloc]init];

    caredTitle.textColor =  [UIColor colorWithRed:0.172 green:0.171 blue:0.219 alpha:1.000];

    caredTitle.font = [UIFont fontWithName:@"Heiti SC" size:10];

    caredTitle.text = @"植物养护";

    [self.view addSubview:caredTitle];

    [caredTitle mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(plantThrm.mas_bottom).with.offset(20);

        make.left.and.right.equalTo(self.view).with.offset(10);

        make.height.mas_equalTo(10);

    }];

    

    [self createIndexUIWithView:caredView];

    

    caredView.layer.cornerRadius = 5;

    caredView.layer.masksToBounds = YES;

    

    caredView.layer.borderWidth = 1;

    caredView.layer.borderColor = [[UIColor colorWithWhite:0.521 alpha:1.000] CGColor];

    caredView.backgroundColor = [UIColor whiteColor];

    [caredView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(caredTitle.mas_bottom).with.offset(5);

        make.left.equalTo(self.view.mas_left).with.offset(10);

        make.right.equalTo(self.view.mas_right).with.offset(-10);

        make.height.equalTo(brifeView);

    }];

    

    UILabel *brifeTitle = [[UILabel alloc]init];

    brifeTitle.textColor =  [UIColor colorWithRed:0.172 green:0.171 blue:0.219 alpha:1.000];

    brifeTitle.font = [UIFont fontWithName:@"Heiti SC" size:10];

    brifeTitle.text = @"植物简介";

    [self.view addSubview:brifeTitle];

    [brifeTitle mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(caredView.mas_bottom).with.offset(20);

        make.left.and.right.equalTo(self.view).with.offset(10);

        make.height.mas_equalTo(10);

    }];

    

    brifeView.layer.cornerRadius = 5;

    brifeView.layer.masksToBounds = YES;

    

    brifeView.layer.borderWidth = 1;

    brifeView.layer.borderColor = [[UIColor colorWithWhite:0.447 alpha:1.000] CGColor];

    brifeView.backgroundColor = [UIColor whiteColor];

    

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];

    paragraphStyle.lineHeightMultiple = 20.f;

    paragraphStyle.maximumLineHeight = 25.f;

    paragraphStyle.minimumLineHeight = 15.f;

    paragraphStyle.alignment = NSTextAlignmentJustified;

    NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:12], NSParagraphStyleAttributeName:paragraphStyle, NSForegroundColorAttributeName:[UIColor colorWithWhite:0.447 alpha:1.000]};

    

    

    brifeView.attributedText = [[NSAttributedString alloc] initWithString: _reference.brief attributes:attributes];

    [brifeView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(brifeTitle.mas_bottom).with.offset(5);

        make.left.equalTo(self.view.mas_left).with.offset(10);

        make.right.equalTo(self.view.mas_right).with.offset(-10);

        make.bottom.equalTo(self.view.mas_bottom).with.offset(-10);

        make.height.equalTo(caredView);

    }];

}

完成之后如下图 步骤2

时间: 2024-10-10 07:52:14

IOS自适应前段库-Masonry的使用的相关文章

iOS自适应布局之Masonry(一)

前言 iPhone 5之前我们对应用布局停留在3.5寸一个屏幕的阶段,当时的安卓屌丝们是多么的羡慕iOS开发,不会被大量的屏幕适配所烦恼.随着iPhone产品的迭代,逐渐出现4寸.4.7寸.5.5寸-.,你们说,iphone10+会有多少寸?O(∩_∩)O~ 简介 正因为上面说的那样,屏幕的尺寸逐步增多,还沿用以前的方法布局显然是不行的,这时候为布局引进新的结局办法NSAutoLayout,但苹果提供的比较复杂麻烦,所以就有人在此基础上进行封装,而提供一种高效的框架--Masonary 安装 如

iOS 开发第三方库全集

拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能.可以自定义上下拉刷新的文字说明.具体使用看"使用方法". (国人写) XHRefreshControl – XHRefreshControl 是一款高扩展性.低耦合度的下拉刷新.上提加载更多的组件.(国人写) CBStoreHo

IOS常用第三方库《转》

UI 动画 网络相关 Model 其他 数据库 缓存处理 PDF 图像浏览及处理 摄像照相视频音频处理 响应式框架 消息相关 版本新API的Demo 代码安全与密码 测试及调试 AppleWatch VPN 完整项目 好的文章 Xcode插件 美工资源 其他资源 开发资源 UI 下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITableView或者Collect

Autolayout第三方库Masonry的入门与实践

在如今的iOS开发中,Autolayout已经是不得不使用了,而且是我们主动的去拥抱Autolayout.使用Autolayout最普遍的方式就是在xib或者storyboard中可视化的添加各种约束,这也是Autolayout入门需要掌握的,关于这部分内容,可以参考<iOS开发--Autolayout的实践与技巧>这篇博客.对于比较简单的App开发,可能使用可视化Autolayout也就足够了.但是如果UI布局稍微复杂多变一点,那么就不得不使用代码了.对于iOS原生的代码自动布局,真的是过于

iOS经典开源库

iOS开源库 youtube下载神器:https://github.com/rg3/youtube-dl我擦咧 vim插件:https://github.com/Valloric/YouCompleteMevim插件配置:https://github.com/spf13/spf13-vim ----------------Mac完整项目----------电台:https://github.com/myoula/sostart豆瓣FM:https://github.com/turingou/do

iOS 创建静态库文件时去掉其中的Symbols

在工程中创建静态库文件时,默认会将一些Symbols加到静态库文件中,这样做有两个缺点: 1.如果引用静态库文件的工程中发生了bug,就会直接跳转到静态库的源码. 2.静态库文件的大小会因此翻几番.本人最近做的这个静态库文件中,去掉symbols前大小为7.8MB左右,去掉以后大小为2.8MB. 要去掉Symbols,首先打开Build Settings并选中静态库的Target,然后设置下列选项: 如果有错误或遗漏,欢迎批评指正. iOS 创建静态库文件时去掉其中的Symbols,布布扣,bu

【转】Unity上同时兼容Android和IOS的JSON库

转自卡神博客Unity上同时兼容Android和IOS的JSON库 虽然说JSON解析很常见,而且也经常看见大家讨论怎么解析.但是还是很多人经常出现各种问题.这篇文章就一次性帮你解决JSON解析的问题. 本篇文章使用JSON解析在真实项目中使用,同时兼容PC.android和IOS.没啥好说,一个C#写的解析JSON的工具类,在unity中能正常解析. 不太好用的JSON解析库:LitJson在IOS上不稳定,有时正常,有时不正常..MiniJson据说支持不完整,没测试. 本文给出的JSON解

WWDC2014之iOS使用动态库 framework【转】

from:http://www.cocoachina.com/industry/20140613/8810.html JUN 12TH, 2014 苹果的开放态度 WWDC2014上发布的Xcode6 beta版有了不少更新,其中令我惊讶的一个是苹果在iOS上开放了动态库,在Xcode6 Beta版的更新文档中是这样描述的: Frameworks for iOS. iOS developers can now create dynamic frameworks. Frameworks are a

&lt;转&gt;iOS第三方开源库的吐槽和备忘

iOS第三方开源库的吐槽和备忘 做iOS开发总会接触到一些第三方库,这里整理一下,做一些吐槽. 目前比较活跃的社区仍旧是Github,除此以外也有一些不错的库散落在Google Code.SourceForge等地方.由于Github社区太过主流,这里主要介绍一下Github里面流行的iOS库. 首先整理了一份Github上排名靠前的iOS库(大概600个repos) 除了逛一下每日/每月流行之外,也可以到这里来看一下整个iOS Repos的排名. 下面是一些比较流行的第三方库: HTTP 相比