objective c - How do I prevent a text stroke from cutting off at the edge of a UILabel? -
i'm adding wide stroke of few pixels text in uilabel
, , depending on line spacing, if edges of text touch edges of label, sides of stroke can cut off, if go outside of bounds of label. how can prevent this? here's code i'm using apply stroke (currently of 5px):
- (void) drawtextinrect: (cgrect) rect { uicolor *textcolor = self.textcolor; cgcontextref c = uigraphicsgetcurrentcontext(); cgcontextsetlinewidth(c, 5); cgcontextsetlinejoin(c, kcglinejoinround); cgcontextsettextdrawingmode(c, kcgtextstroke); self.textcolor = [uicolor colorwithred: 0.165 green: 0.635 blue: 0.843 alpha: 1.0]; [super drawtextinrect: rect]; }
here's example of clipping @ side of label, think needs happen 1 of following:
- that when text splits onto multiple lines, space given inside label's frame stroke occupy.
- or, stroke allowed overflow outer bounds of label.
yup, clipping didn't work.
what if create insets on uilabel
sub-class though. you'd make frame of label big need be, set insets. when draw text, use insets give padding around edge need.
the downside is, won't able judge line wrapping @ glance in ib. you'd have take label , subtract insets you'll see line on screen.
.h
@interface mylabel : uilabel @property (nonatomic) uiedgeinsets insets; @end
.m
@implementation mylabel - (id)initwithcoder:(nscoder *)adecoder { self = [super initwithcoder:adecoder]; if (self) { self.insets = uiedgeinsetsmake(0, 3, 0, 3); } return self; } - (void)drawrect:(cgrect)rect { cgcontextref c = uigraphicsgetcurrentcontext(); cgcontextsavegstate(c); cgrect actualtextcontentrect = rect; actualtextcontentrect.origin.x += self.insets.left; actualtextcontentrect.origin.y += self.insets.top; actualtextcontentrect.size.width -= self.insets.right; actualtextcontentrect.size.height -= self.insets.bottom; cgcontextsetlinewidth(c, 5); cgcontextsetlinejoin(c, kcglinejoinround); cgcontextsettextdrawingmode(c, kcgtextstroke); self.textcolor = [uicolor colorwithred: 0.165 green: 0.635 blue: 0.843 alpha: 1.0]; [super drawtextinrect:actualtextcontentrect]; cgcontextrestoregstate(c); self.textcolor = [uicolor whitecolor]; [super drawtextinrect:actualtextcontentrect]; }
edit: added full code uilabel
subclass. modified code show both large stroke , the normal lettering.
Comments
Post a Comment