IOS 給UILabel字型加一個帶顏色的邊框

來源:互聯網
上載者:User

標籤:ado   ati   elf   簡單實現   creat   color   post   current   字型   

今天做項目碰見一個UI效果,給字型加一圈白邊,看起來就像是加了一個背景,思路就是繼承一個UILabel,重新覆寫drawTextInRect方法,就可以簡單實現這個效果,上代碼:

 1 // 2 //  HGLLabel.m 3 //  xxxxxxxxxx.xxx 4 // 5 //  Created by xxxxx on 2017/12/23. 6 //  Copyright ? 2017年 xxxx. All rights reserved. 7 // 8  9 #import "HGLLabel.h"10 11 @implementation HGLLabel12 13 - (void)drawTextInRect:(CGRect)rect {14     CGSize shadowOffset = self.shadowOffset;15     UIColor *textColor = self.textColor;16     17     CGContextRef c = UIGraphicsGetCurrentContext();18     CGContextSetLineWidth(c, 8.0);//字型邊緣的寬度19     CGContextSetLineJoin(c, kCGLineJoinRound);20     21     CGContextSetTextDrawingMode(c, kCGTextStroke);22     self.textColor = [UIColor whiteColor];//字型邊緣加的顏色23     [super drawTextInRect:rect];24     25     CGContextSetTextDrawingMode(c, kCGTextFill);26     self.textColor = textColor;27     self.shadowOffset = CGSizeMake(0, 0);28     [super drawTextInRect:rect];29     30     self.shadowOffset = shadowOffset;31 }32 33 @end

 

 

注釋很清楚,不多說。

IOS 給UILabel字型加一個帶顏色的邊框

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.