iOS 6 中引入了AutoLayout,像UILabel、UIButton等控件设置text和font之后可以自动计算控件大小,是因为ContentHugging 和 ContentCompressionResistance
相关函数如下
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
ContentHuggingPriority 确定view有多大优先级阻止自己变大,默认为250
ContentCompressionResistancePriority 确定view有多大优先级阻止自己变小,默认为750
示例
有三个文本,文本一、文本二、文本三,在一行,显示优先级为文本一>文本二>文本三,也就是优先显示文本一的内容,再次为文本二的内容,最后显示文本三的内容。我们可以修改ContentHuggingPriority和ContentCompressionResistancePriority来实现。
会用到第三方库Masonry,https://github.com/SnapKit/Masonry
主界面如下
只显示文本一内容时,如下
2.png显示文本一和文本二内容时,如下
3.png文本一和文本二以及文本三内容都显示时,如下
4.png主代码如下
#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
/** UI */
@property (nonatomic, strong) UILabel *labelOne;
@property (nonatomic, strong) UILabel *labelTwo;
@property (nonatomic, strong) UILabel *labelThree;
@property (nonatomic, strong) UIImageView *qrCodeImgView;
@property (nonatomic, strong) UILabel *signLabel;
@end
@implementation ViewController
#pragma mark - life cycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"点滴86";
self.view.backgroundColor = [UIColor whiteColor];
__weak __typeof__(self) weakSelf = self;
[self.view addSubview:self.labelOne];
[self.labelOne mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.top.mas_equalTo(100);
}];
[self.view addSubview:self.labelTwo];
[self.labelTwo mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.labelOne.mas_right);
make.top.mas_equalTo(100);
}];
[self.view addSubview:self.labelThree];
[self.labelThree mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.labelTwo.mas_right);
make.top.mas_equalTo(100);
make.right.equalTo(weakSelf.view.mas_right);
}];
[self.view addSubview:self.qrCodeImgView];
self.qrCodeImgView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 129) / 2, 240, 129, 129);
[self.view addSubview:self.signLabel];
self.signLabel.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 170, 400, 150, 30);
}
- (void)setDataModel:(DMModel *)dataModel
{
self.labelOne.text = dataModel.oneStr;
self.labelTwo.text = dataModel.twoStr;
self.labelThree.text = dataModel.threeStr;
}
#pragma mark - getter and setter
- (UILabel *)labelOne
{
if (_labelOne == nil) {
_labelOne = [[UILabel alloc] init];
_labelOne.backgroundColor = [UIColor clearColor];
// 多大的优先级阻止自己变大 默认250
[_labelOne setContentHuggingPriority:255 forAxis:UILayoutConstraintAxisHorizontal];
// 多大的优先级阻止自己变小 默认750
[_labelOne setContentCompressionResistancePriority:750 forAxis:UILayoutConstraintAxisHorizontal];
}
return _labelOne;
}
- (UILabel *)labelTwo
{
if (_labelTwo == nil) {
_labelTwo = [[UILabel alloc] init];
_labelTwo.backgroundColor = [UIColor clearColor];
[_labelTwo setContentHuggingPriority:254 forAxis:UILayoutConstraintAxisHorizontal];
[_labelTwo setContentCompressionResistancePriority:745 forAxis:UILayoutConstraintAxisHorizontal];
}
return _labelTwo;
}
- (UILabel *)labelThree
{
if (_labelThree == nil) {
_labelThree = [[UILabel alloc] init];
_labelThree.backgroundColor = [UIColor clearColor];
[_labelThree setContentHuggingPriority:251 forAxis:UILayoutConstraintAxisHorizontal];
[_labelThree setContentCompressionResistancePriority:740 forAxis:UILayoutConstraintAxisHorizontal];
}
return _labelThree;
}
- (UIImageView*)qrCodeImgView
{
if (_qrCodeImgView == nil) {
_qrCodeImgView = [[UIImageView alloc] init];
_qrCodeImgView.backgroundColor = [UIColor clearColor];
_qrCodeImgView.image = [UIImage imageNamed:@"qrcode"];
}
return _qrCodeImgView;
}
- (UILabel*)signLabel
{
if (_signLabel == nil) {
_signLabel = [[UILabel alloc] init];
_signLabel.backgroundColor = [UIColor clearColor];
_signLabel.font = [UIFont systemFontOfSize:20];
_signLabel.text = @"By 点滴86";
_signLabel.textAlignment = NSTextAlignmentRight;
}
return _signLabel;
}
@end
示例就到此结束啦
网友评论