ios - Add padding to UILabel using Auto Layout or layoutMargins? -
this question has answer here:
- resizing uilabel accommodate insets 6 answers
i want add inset between label's frame , text. under possible using layoutmargins
(http://carpeaqua.com/2014/07/24/auto-layout-in-ios-8-layout-margins/) have no been able this.
i have sample project can see doing (wrong?): https://github.com/runmad/messagingapp
if you, subclass uilabel
, add uiedgeinsets
. in subclass of uilabel
this:
.m file
- (id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self){ //property in header file can add custom insets per instance of class self.edgeinsets = uiedgeinsetsmake(0, 0, 0, 0); } return self; } -(void)drawtextinrect:(cgrect)rect { [super drawtextinrect:uiedgeinsetsinsetrect(rect, self.edgeinsets)]; } /* work auto layout */ -(cgsize)intrinsiccontentsize { cgsize size = [super intrinsiccontentsize]; size.width += self.edgeinsets.left + self.edgeinsets.right; size.height += self.edgeinsets.top + self.edgeinsets.bottom; if (self.numberoflines == 0){ //there bug intrinsice content //size may 1 point short size.height += 1; } return size; }
Comments
Post a Comment