Record control adaptive content

TextView

The requirement is that the TextView ADAPTS to the width and height of the content, but has a maximum of 200 and a minimum of 100.

By customizing textView:

Intrinsic Content Size: indicates the Intrinsic Size. If the size is not specified in AutoLayout, it is set to that size. UILabel, UIImageView, UIButton, you just set the position and you don’t set the size.

@implementation EWTextView

-(void)setContentSize:(CGSize)contentSize{
    [super setContentSize:contentSize];

    [self invalidateIntrinsicContentSize];
}
-(CGSize)intrinsicContentSize{
    NSLog(@"%@",NSStringFromCGSize(self.contentSize));
    if (self.contentSize.height <= 100) {
        return CGSizeMake(self.contentSize.width, 100);
    }else if(self.contentSize.height > 200){
        return CGSizeMake(self.contentSize.width, 200);
    }
    return self.contentSize;
}
@end
Copy the code

use

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    EWTextView *textview = [[EWTextView alloc]init];
    textview.backgroundColor = [UIColor redColor];
    [self.view addSubview:textview];

    [textview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.top.equalTo(@100);
        make.right.equalTo(@-100);
        
    }];
}
Copy the code

The Intrinsic conflict

The intrinsics conflict occurs when both labels have Intrinsic contentSizes that add a constraint to the right of Label2. To solve

  1. Both Uilabels abandon the use of intrinsics, setting the width and height for them.
  2. One UILabel uses the Intrinsic ContentSize and the other uses the residual width.

content Hugging

A constraint (without making it bigger) means that if this property of a component has a higher priority than this property of another component, that component stays the same and the other component can be stretched as needed. Attributes are divided into two directions: transverse and longitudinal.

Content Compression Resistance

A constraint (don’t want to get smaller) says: If this property of a component has a higher priority than this property of another component, then that component stays the same and the other component can be compressed as needed. Attributes are divided into two directions: transverse and longitudinal.

In the two UIlabel examples, if one UIlabel uses Intrinsic Content Size, the other one needs to be stretched. So we need to adjust the priority of the Content Hugging constraints of both UILabel s.