(UILabel自体に枠線をつける方法はUIImageViewと同じです)
まず、UILabelを継承したクラスを作成します。
それから外枠の色とサイズを設定できるようにヘッダーファイルを修正します。
@interface UIOutlineLabel : UILabel @property (nonatomic, retain) UIColor *OutlineColor; @property (nonatomic, assign) CGFloat OutlineWidth; @end
次にソースファイルを修正します。
やり方としてはUILabelのもつ -drawTextInRect:rect をオーバーライドして縁取りします。
- (void)drawTextInRect:(CGRect)rect
{
CGSize shadowOffset = self.shadowOffset;
UIColor *txtColor = self.textColor;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, OutlineWidth);
CGContextSetLineJoin(contextRef, kCGLineJoinRound);
CGContextSetTextDrawingMode(contextRef, kCGTextStroke);
self.textColor = OutlineColor;
[super drawTextInRect:CGRectInset(rect, OutlineWidth, OutlineWidth)];
CGContextSetTextDrawingMode(contextRef, kCGTextFill);
self.textColor = txtColor;
[super drawTextInRect:CGRectInset(rect, OutlineWidth, OutlineWidth)];
self.shadowOffset = shadowOffset;
}
あとはこれを使用するViewControllerで実装します。
InterfaceBuildeerを使って画面上にUILabelを貼付けて、そのラベルのIdentity inspectorを開きます。
Custom Classの部分でUIOutlineLabelを選択。
あとはこれを関連づけして色とサイズを指定するだけです。
[aDateLabel setOutlineColor:[UIColor whiteColor]]; [aDateLabel setOutlineWidth:5.f];



0 件のコメント:
コメントを投稿