Implement a view that automatically determines the height of a autolayout when it has a width constraint under the

Source: Internet
Author: User

I once had a problem: I needed to implement a custom label (similar to Uilabel), and I needed to take into account the size-adaptive nature of Uilabel. The label is usually a fixed width, specifying its width constraint by AutoLayout, but not specifying a height to adapt it to the content.

We know that the UIView method intrinsiccontentsize can help us determine the size of the view under AutoLayout, thus avoiding the constraint of setting its wide height. So I took the solution: to use the width of the label as its property, I had to specify the width of the label before using it, and the label itself determines the height by its Width property. However, this method is not very convenient, because the width sometimes requires us to calculate, for example, it is the screen width minus a value. However I am a lazy enough programmer, do not want to manually set the width each time.

Eventually, though, I didn't know if it was the best approach, but at least it solved the problem. After the AutoLayout layout is complete, we can know the final width of the view. Then we can pass

The Invalidateintrinsiccontentsize method recalculates the size of the view.

The code is as follows:

1 @interface Mylabel:uiview 2 3 @property (nonatomic, Strong) Uifont *font; 4 @property (nonatomic, Strong) NSString *text; 5 6 @end

1 @interfaceMyLabel ()2 3@property (nonatomic, strong) Uifont *Defaultfont;4 5 @end6 7 @implementationMyLabel8 9-(instancetype) initTen { OneSelf =[Super init]; A     if(self) { -Self.defaultfont = [Uifont systemfontofsize: -]; -     } the     returnSelf ; - } -  -- (void) SetText: (NSString *) Text + { -_text =text; + [self setneedslayout]; A [self setneedsdisplay]; at } -  -- (void) Layoutsubviews - { - [self invalidateintrinsiccontentsize];//force recalculation of size at layout time - [Super Layoutsubviews]; in } -  to-(cgsize) Sizethatfits: (cgsize) Size + { -Uifont *font = Self.font?Self.font:self.defaultFont; theSize.width =ceil (size.width);//Avoid being incorrectly calculated as two lines of height when calculating a single line of text two times *     if(Size.width >0) { $         return[Self.text boundingrectwithsize:size Options:nsstringdrawinguseslinefragmentorigin |nsstringdrawingusesfontleading Attributes:@{nsfontattributename:font} context:nil].size;Panax Notoginseng}Else{//here is for compatibility without width (single line of text) -         return[Self.text Sizewithattributes:@{nsfontattributename:font}]; the     } + } A  the-(cgsize) intrinsiccontentsize + { -     return[Self Sizethatfits:cgsizemake (Cgrectgetwidth (self.bounds), Cgfloat_max)]; $ } $  --(cgsize) Systemlayoutsizefittingsize: (cgsize) targetsize - { the     return[self sizethatfits:targetsize]; - }Wuyi  the- (void) DrawRect: (cgrect) Rect - { WuUifont *font = Self.font?Self.font:self.defaultFont; - [Self.text Drawinrect:rect Withattributes:@{nsfontattributename:font}]; About } $  - @end

Let's test it out:

1MyLabel *label = [MyLabelNew];2Label.backgroundcolor =[Uicolor Yellowcolor];3 [Self.view Addsubview:label];4[Label mas_makeconstraints:^ (Masconstraintmaker *Make ) {//There is no height constraint here5Make.left.mas_equalTo (Ten);6Make.top.mas_equalTo ( -);7Make.width.mas_lessThanOrEqualTo ( -);8     }];9 TenLabel.text =@"This is a long text. A long text.";

Implement a view that automatically determines the height of a autolayout when it has a width constraint under the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.