You can easily set the edgeinsetslabel of the text padding.

Source: Internet
Author: User

You can easily set the edgeinsetslabel of the text padding.

Final effect:

Source code:

Edgeinsetslabel. h and edgeinsetslabel. m

//// Edgeinsetslabel. h // edgeinsetslabel /// created by youxianming on 14/10/27. // copyright (c) 2014 youxianming. all rights reserved. // # import <uikit/uikit. h> @ interface edgeinsetslabel: uilabel @ property (nonatomic, assign) uiedgeinsets edgeinsets; @ end
//// Edgeinsetslabel. M // edgeinsetslabel /// created by youxianming on 14/10/27. // copyright (c) 2014 youxianming. all rights reserved. // # import "edgeinsetslabel. H "@ implementation edgeinsetslabel-(cgrect) textrectforbounds :( cgrect) bounds limitedtonumberoflines :( nsinteger) numberoflines {uiedgeinsets insets = self. edgeinsets; cgrect rect = [Super textrectforbounds: uiedgeinsetsinsetrect (bounds, insets) limitedtonumberoflines: numberoflines]; rect. origin. x-= insets. left; rect. origin. y-= insets. top; rect. size. width + = (insets. left + insets. right); rect. size. height + = (insets. top + insets. bottom); Return rect;}-(void) drawtextinrect :( cgrect) rect {[Super drawtextinrect: uiedgeinsetsinsetrect (rect, self. edgeinsets)];} @ end

Viewcontroller. m

//// Viewcontroller. M // setinsets /// created by youxianming on 14/10/27. // copyright (c) 2014 youxianming. all rights reserved. // # import "viewcontroller. H "# import" edgeinsetslabel. H "@ interface viewcontroller () @ end @ implementation viewcontroller-(void) viewdidload {[Super viewdidload]; edgeinsetslabel * label = [[edgeinsetslabel alloc] initwithframe: cgrectmake (0, 0,100,100)]; label. edgeinsets = uiedgeinsetsmake (8, 8 + 10, 8, 8 + 10); // set the padding label. font = [uifont fontwithname: @ "helveticaneue-thin" Size: 30.f]; label. TEXT = @ "No Zuo no die"; [label sizetofit]; // recalculate the size label. layer. cornerradius = label. frame. size. height/2.f; label. backgroundcolor = [uicolor blackcolor]; label. textcolor = [uicolor redcolor]; label. layer. maskstobounds = yes; label. center = self. view. center; [self. view addsubview: Label];} @ end

Core Principles:

 

You can easily set the edgeinsetslabel of the text padding.

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.