iOS實(shí)現(xiàn)圓角箭頭矩形的提示框
先來看看我們見過的一些圓角箭頭矩形的提示框效果圖
一、了解CGContextRef
首先需要對(duì) CGContextRef 了解, 作者有機(jī)會(huì)再進(jìn)行下詳細(xì)講解, 這篇中簡(jiǎn)單介紹下, 方便后文閱讀理解. 先了解 CGContextRef 坐標(biāo)系
坐標(biāo)系
舉例說明 : 對(duì)于 商城類App 有很多原價(jià), 現(xiàn)價(jià)對(duì)比 .那 原件的橫線怎么畫, 就可以用CGContextRef
- (void)drawRect:(CGRect)rect { // Drawing code [super drawRect:rect]; // 獲取文本 CGContextRef context = UIGraphicsGetCurrentContext(); // 設(shè)置起始點(diǎn)坐標(biāo) (x軸: 0 . y軸: 控件高度一半) CGContextMoveToPoint(context, 0, rect.size.height * 0.5); // 設(shè)置直線連接點(diǎn) (x軸:控件全長(zhǎng). y軸:控件高度一半 ) CGContextAddLineToPoint(context, rect.size.width, rect.size.height * 0.5); //設(shè)置顏色 寬度 [[UIColor redColor] set]; CGContextSetLineWidth(context, 1); CGContextStrokePath(context); }
說明 : 從上面例子可以看到 是由兩點(diǎn)確定一條直線.
二、自定義UILabel
進(jìn)入正文. 控件可以自定義 UIView . 作者為了簡(jiǎn)單直接 自定義UILabel.
CustomLabel *customLabel = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 200, 400, 500)]; [self.view addSubview:view]; view.backgroundColor = [UIColor orangeColor];
首先 .h文件
.h文件沒什么說的, 定義的 字符串一會(huì)再解釋說明
#import <UIKit/UIKit.h> @interface CustomLabel : UILabel @property (nonatomic, copy) NSString *fillColorStr; @end
.m文件
我們用 - (void)drawRect:(CGRect)rect;
繪制 圓角箭頭
#import "CustomLabel.h" @implementation CustomLabel - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 自定義你需要的屬性 } return self; } // 主要講解 - (void)drawRect:(CGRect)rect { // 隨便設(shè)置個(gè) 長(zhǎng)寬 float w = rect.size.width - 20; float h = rect.size.height - 20; // 獲取文本 CGContextRef context = UIGraphicsGetCurrentContext(); // 設(shè)置 邊線寬度 CGContextSetLineWidth(context, 0.2); //邊框顏色 CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor); //矩形填充顏色 CGContextSetFillColorWithColor(context, [UIColor cyanColor].CGColor); /** 先介紹 CGContextAddArcToPoint 參數(shù) * CGContextRef : 為獲取的文本 * x1, y1 : 第一點(diǎn) * x2, y2 : 第二點(diǎn) * radius : 圓角弧度 * 說明 : 兩點(diǎn)連線 如同矢量線, 有方向性. */ // [開始點(diǎn)] 坐標(biāo)從左上角開始 (6, 0) CGContextMoveToPoint(context, 6, 0); // 設(shè)置 第一點(diǎn) 第二點(diǎn) 弧度. 詳解 : [開始點(diǎn)]到[第一點(diǎn)], 確定一條直線 (6, 0) -> (w, 0); [第一點(diǎn)]到[第二點(diǎn)]確定一條直線(w, 0)->(w, 10) // 兩條線連接與方向 為直角 設(shè)置弧度 CGContextAddArcToPoint(context, w, 0, w, 10, 6); // 右上角圓弧設(shè)置完 // 現(xiàn)在 [開始點(diǎn)] 坐標(biāo)變?yōu)?(w, 10). 第一條線(w, 10) -> (w , h) ; 第二條線(w, h) -> (w - 10, h). CGContextAddArcToPoint(context, w , h , w - 10, h, 6); // 右下角圓弧設(shè)置完 // 現(xiàn)在 [開始點(diǎn)] 坐標(biāo)變?yōu)?(w - 10, h) . 由 (w - 10, h) -> (30, h) 向左畫直線 CGContextAddLineToPoint(context, 30, h ); // 向左畫線 // 現(xiàn)在 [開始點(diǎn)] 坐標(biāo)變?yōu)?(30, h). 由(30, h) -> (25, h + 8) 向左下畫直線 CGContextAddLineToPoint(context, 25, h + 8); // 左下直線 // 現(xiàn)在 [開始點(diǎn)] 坐標(biāo)變?yōu)?(25, h + 8). 由 (25, h + 8)-> (20, h) 向左上畫直線 CGContextAddLineToPoint(context, 20, h ); // 左上直線 // 現(xiàn)在 [開始點(diǎn)] 坐標(biāo)變?yōu)?(20, h ). 第一條線(20, h)-> (0, h) ; 第二條線(0, h)->(0, h - 10) CGContextAddArcToPoint(context, 0, h, 0, h - 10, 6); // 左下角圓弧設(shè)置完 // 現(xiàn)在 [開始點(diǎn)] 坐標(biāo)變?yōu)?(0, h - 10 ). 第一條線(0, h - 10)-> (0, 0) ; 第二條線(0, 0)->(6, 0) // 說明: 最開始設(shè)置的坐標(biāo)點(diǎn)為(6, 0) 不為(0, 0). 就是為了最后可以連接上, 不然 畫的線不能連接上 , 讀者可自行試驗(yàn) CGContextAddArcToPoint(context, 0, 0, 6, 0, 6); // 左上角圓弧設(shè)置完 CGContextDrawPath(context, kCGPathFillStroke); //根據(jù)坐標(biāo)繪制路徑 }
三、簡(jiǎn)單封裝
看到上面 你可以畫出 圓角箭頭矩形
還記得 .h 中 定義的屬性 fillColorStr, 可以進(jìn)行調(diào)用, 隨意更改填充顏色, 圓弧角度等.
僅簡(jiǎn)單封裝下 (讀者自行嚴(yán)謹(jǐn)封裝)
- (void)drawRect:(CGRect)rect { // 默認(rèn)圓角角度 float r = 4; // 居中偏移量(箭頭高度) float offset = 5; // 設(shè)置 箭頭位置 float positionNum = 20; // 定義坐標(biāo)點(diǎn) 移動(dòng)量 float changeNum = r + offset; // 設(shè)置畫線 長(zhǎng) 寬 float w = self.frame.size.width ; float h = self.frame.size.height; // 獲取文本 CGContextRef context = UIGraphicsGetCurrentContext(); // 設(shè)置 邊線寬度 CGContextSetLineWidth(context, 0.2); //邊框顏色 CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor); //矩形填充顏色 if ([self.fillColorStr isEqualToString:@"fillColorChange"]) { CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor); }else{ CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); } CGContextMoveToPoint(context, r, offset); // 開始坐標(biāo)左邊開始 CGContextAddArcToPoint(context, w, offset, w, changeNum, r); // 右上角角度 CGContextAddArcToPoint(context, w , h - offset, w - changeNum, h - offset, r); // 右下角角度 CGContextAddLineToPoint(context, positionNum + 10, h - offset); // 向左劃線 CGContextAddLineToPoint(context, positionNum + 5, h); // 向下斜線 CGContextAddLineToPoint(context, positionNum, h - offset); // 向上斜線 CGContextAddArcToPoint(context, 0, h - offset, 0, h - changeNum, r); // 左下角角度 CGContextAddArcToPoint(context, 0, offset, r, offset, r); // 左上角角度 CGContextDrawPath(context, kCGPathFillStroke); //根據(jù)坐標(biāo)繪制路徑 /** 父類調(diào)用 放在畫完邊線 后. 不然 設(shè)置的文字會(huì)被覆蓋 */ [super drawRect:rect]; } // 當(dāng) 要改變填充顏色 可以進(jìn)行調(diào)用改變 -(void)setFillColorStr:(NSString *)fillColorStr{ _fillColorStr = fillColorStr; // 調(diào)用- (void)drawRect:(CGRect)rect; 重繪填充顏色 [self setNeedsDisplay]; }
將控件顏色 設(shè)置為透明, 一個(gè) 自定義的提示框完成. 作者僅提供一種方法, 讀者也可用 UIBezierPath等 去實(shí)現(xiàn).
四、補(bǔ)充說明
有人說 直接用圖片實(shí)現(xiàn), 都可以 , 看工程需求. 此文主要聊的是
用 - (void)drawRect:(CGRect)rect;
繪制 圓角箭頭
介紹CGContextAddArcToPoint的使用
對(duì)用圖片實(shí)現(xiàn) , 形變問題.
UIImage 類提供
(UIImage*)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight;
簡(jiǎn)單說明下
圖片為 100 * 56 像素
UIImage *tempImage = [UIImage imageNamed:@"tip.png"]; /** 說明 : 創(chuàng)建一個(gè)內(nèi)容可拉伸,邊角不拉伸的圖片, 單位為像素 * LeftCapWidth: 左邊不拉伸區(qū)域的寬度 * topCapHeight: 上面不拉伸區(qū)域高度 * 之后沿此區(qū)域 橫縱拉伸, 邊角不變 */ tempImage = [tempImage stretchableImageWithLeftCapWidth:80 topCapHeight:20]; UIImageView *imgView=[[UIImageView alloc]initWithImage:tempImage]; imgView.frame = CGRectMake(100, 100, 200, 56); [self. view addSubview:imgView];
拉伸后與原圖對(duì)比
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位iOS開發(fā)者們能有所幫助,如果有疑問大家可以留言交流。
相關(guān)文章
iOS利用MJRefresh實(shí)現(xiàn)自定義刷新動(dòng)畫效果
本文主要介紹iOS 利用MJRefresh實(shí)現(xiàn)自定義動(dòng)畫的上拉刷新下拉加載效果,要想實(shí)現(xiàn)此功能,首先得有一套load的圖片數(shù)組。接下來通過本文給大家詳解介紹實(shí)現(xiàn)過程2017-02-02IOS點(diǎn)擊按鈕隱藏狀態(tài)欄詳解及實(shí)例代碼
這篇文章主要介紹了IOS點(diǎn)擊按鈕隱藏狀態(tài)欄詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02iOS給圖片添加濾鏡&使用openGLES動(dòng)態(tài)渲染圖片詳解及實(shí)例
這篇文章主要介紹了iOS給圖片添加濾鏡&使用openGLES動(dòng)態(tài)渲染圖片詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-10-10iOS中UIActionSheet動(dòng)態(tài)添加按鈕
這篇文章主要介紹了iOS中UIActionSheet動(dòng)態(tài)添加按鈕功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06詳解iOS通過ASIHTTPRequest提交JSON數(shù)據(jù)
這篇文章主要介紹了詳解iOS通過ASIHTTPRequest提交JSON數(shù)據(jù),對(duì)代碼進(jìn)行了詳細(xì)的講解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12iOS App開發(fā)中用CGContextRef繪制基本圖形的基本示例
這篇文章主要介紹了iOS App開發(fā)中用CGContextRef繪制基本圖形的基本示例,CGContextRef同時(shí)可以進(jìn)行圖形顏色的填充以及文字的書寫,需要的朋友可以參考下2016-05-05iOS利用NSMutableAttributedString實(shí)現(xiàn)富文本的方法小結(jié)
這篇文章主要給大家介紹了關(guān)于iOS利用NSMutableAttributedString如何實(shí)現(xiàn)富文本的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05IOS登錄頁面動(dòng)畫、轉(zhuǎn)場(chǎng)動(dòng)畫開發(fā)詳解
本篇文章通過詳細(xì)的步驟給大家詳細(xì)講述了IOS登錄頁面動(dòng)畫、轉(zhuǎn)場(chǎng)動(dòng)畫開發(fā)的詳細(xì)教程,有興趣的朋友參考學(xué)習(xí)下。2018-01-01