IOS UILabel 文字描边详解

来源:互联网 发布:会声会影x9激活软件 编辑:程序博客网 时间:2024/06/01 09:14
刚开始觉得这功能很娱乐……
后来想想,任何设计都是有他的道理,有他的原因的,除非特别不合理,我会选择无视。
无论美丑,人家设计出来,作为RD,你就应该能够够给做出来(值不值得做 另说),就算没几天被砍掉,也没辙……
最开始实在这里找到的答案:
http://stackoverflow.com/questions/1103148/how-do-i-make-uilabel-display-outlined-text
看完之后觉得好简单……
如果你只是简单的想加个描边,而不需要为以后做更好的扩展,或者做成公共的Custom Label控件,下面的代码足够你了。
- (void)drawTextInRect:(CGRect)rect {  CGSize shadowOffset = self.shadowOffset;  UIColor *textColor = self.textColor;  CGContextRef c = UIGraphicsGetCurrentContext();  CGContextSetLineWidth(c, 1);  CGContextSetLineJoin(c, kCGLineJoinRound);  CGContextSetTextDrawingMode(c, kCGTextStroke);  self.textColor = [UIColor whiteColor];  [super drawTextInRect:rect];  CGContextSetTextDrawingMode(c, kCGTextFill);  self.textColor = textColor;  self.shadowOffset = CGSizeMake(0, 0);  [super drawTextInRect:rect];  self.shadowOffset = shadowOffset;}
当然,如果你有更高的追求,更好的封装意识……
用这个:https://github.com/vigorouscoding/KSLabel。
还有这个:https://github.com/MuscleRumble/THLabel
都是很好的Demo。

参考:https://developer.apple.com/library/mac/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_text/dq_text.html#//apple_ref/doc/uid/TP30001066-CH213-TPXREF101

Text drawing modes

Use this mode

When you want to . . .

Example

kCGTextFill

Perform a fill operation on the text.

Filled text

kCGTextStroke

Perform a stroke operation on the text.

Stroked text.

kCGTextFillStroke

Perform both fill and stroke operations on the text.

Filled and stroked test.

kCGTextInvisible

Get text positions for the purpose of measuring text but not display the text. Note that the text position (xy) is updated, as with all of the drawing modes.

Invisible text.

kCGTextFillClip

Perform a fill operation, then add the text to the clipping area.

Filled text added to the clipping area.

kCGTextStrokeClip

Perform a stroke operation, then add the text to the clipping area.

Stroked text, added to the clipping area

kCGTextFillStrokeClip

Perform both fill and stroke operations, then add the text to the clipping area.

Filled and stroked text, aded to the clipping area

kCGTextClip

Add the text to the clipping area, but do not draw the text.

Test added to the clipping area, but not drawn


原创粉丝点击