"Swift" UILabel set the inner margin

Source: Internet
Author: User

Objective

For someone who's ever developed Android, without these basic attributes it's outrageous, or it's a table spray, write code--#

Statement
Welcome reprint, but please keep the original source of the article:)
Blog Park: http://www.cnblogs.com
Farmer Uncle: Http://over140.cnblogs.com

Body

classUilabelpadding:uilabel {Privatevar padding =Uiedgeinsetszero @IBInspectable var paddingleft:cgfloat {Get{returnPadding.left}Set{Padding.left =NewValue}} @IBInspectable var paddingright:cgfloat {Get{returnPadding.right}Set{padding.right =NewValue}} @IBInspectable var paddingtop:cgfloat {Get{returnPadding.top}Set{padding.top =NewValue}} @IBInspectable var paddingbottom:cgfloat {Get{returnPadding.bottom}Set{Padding.bottom =NewValue}} Overridefunc Drawtextinrect (rect:cgrect) {super.drawtextinrect (Uiedgeinsetsinsetrect (rect, padding))}OverrideFunc textrectforbounds (Bounds:cgrect, Limitedtonumberoflines numberoflines:int)CGRect {let insets=self.padding var rect=super.textrectforbounds (Uiedgeinsetsinsetrect (Bounds, insets), Limitedtonumberoflines:numberoflines) Rect.ori Gin.x-=Insets.left RECT.ORIGIN.Y-=insets.top rect.size.width+ = (Insets.left +insets.right) Rect.size.height+ = (Insets.top +insets.bottom)returnrect}}

Code Description:

Ibinspectable can be used to support uilable in the Storyboard can specify the internal margin, very convenient:

    

Reference

Refer to this article http://stackoverflow.com/questions/21167226/resizing-a-uilabel-to-accomodate-insets change, note this article http/ There's a problem with Stackoverflow.com/questions/3476646/uilabel-text-margin.

End

Sorry for naming with Android, and then Super Love ibinspectable this thing.

"Swift" UILabel set the inner margin

Related Article

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.