objective c - Add padding into UIlabel text? -


i've label in table cell , wish add padding top,bottom,left , right.

cgrect initialframe = cgrectmake(10,10,100,20);     uiedgeinsets contentinsets = uiedgeinsetsmake(5, 0, 5, 0);     cgrect padd = uiedgeinsetsinsetrect(initialframe, contentinsets);     self.rewardlabel = [[uilabel alloc] initwithframe:padd];     self.rewardlabel.backgroundcolor =[uicolor colorwithred:0.192 green:0.373 blue:0.561 alpha:1];     self.rewardlabel.layer.cornerradius = 5.0f;     self.rewardlabel.layer.maskstobounds = yes;     self.rewardlabel.textcolor = [uicolor whitecolor];     self.rewardlabel.linebreakmode = nslinebreakbywordwrapping;     self.rewardlabel.numberoflines = 1;     self.rewardlabel.font = [uifont fontwithname:@"helveticaneue" size:14];     [self.contentview addsubview:self.rewardlabel]; 

but seem not working.can tell me how do?

there several ways on how achieve this:

  1. if not need specific background color label adjust labels frame add padding (e.g. if text should start 20px cell's left side, set label's frame's x 20).
  2. to add padding, use custom uilabel subclass , override drawtextinrect: , intrinsiccontentsize methods. (see question details)
  3. if need left , right padding use nsattributedstring add insets uilabel:

uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(0, 0, 200, 40)]; nsmutableparagraphstyle *paragraphstyle = [[nsmutableparagraphstyle alloc] init]; paragraphstyle.headindent = 5.0; paragraphstyle.firstlineheadindent = 5.0; paragraphstyle.tailindent = -5.0; nsdictionary *attrsdictionary = @{nsparagraphstyleattributename: paragraphstyle}; label.attributedtext = [[nsattributedstring alloc] initwithstring:@"your text" attributes:attrsdictionary]; 


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -