iOS畫矩形 直線 文字的方法

2021-06-22 00:32:31 字數 2098 閱讀 7071

**:

7488353

cgpoint畫直線

cgcontextref ctx = uigraphicsgetcurrentcontext();

cgcontextmovetopoint(ctx, point1.x, point1.y);

cgcontextaddlinetopoint(ctx, point2.x, point2.y); 

//畫曲線用cgcontextaddarc

cgcontextstrokepath(ctx);

在iphone螢幕上畫長方形,直線和文字

完成餅圖後,我悲哀的發現,畫扇形和畫方形、畫直線完全是兩碼事兒,於是不得不重新找資料,完成畫長方形,直線和文字的工作。

//畫長方形

cgcontextref ctx = uigraphicsgetcurrentcontext();

//設定顏色,僅填充4條邊

cgcontextsetstrokecolorwithcolor(ctx, [[uicolor colorwithred:1 green:1 blue:1 alpha:0.5] cgcolor]);

//設定線寬為1 

cgcontextsetlinewidth(ctx, 1.0);

//設定長方形4個頂點

cgpoint poins = ;

cgcontextaddlines(ctx,poins,4);

cgcontextclosepath(ctx);

cgcontextstrokepath(ctx);

//畫直線,x1和y1是起始點,x2和y2是結束點

//預設座標系左上角為0,0

cgcontextmovetopoint(ctx, x1, y1);

cgcontextaddlinetopoint(ctx, x2, y2);

cgcontextclosepath(ctx);

cgcontextstrokepath(ctx);

//畫文字,設定文字內容

nsstring *text = @"text";

//設定字型大小

uifont *font = [uifont systemfontofsize:8];

//在指定x,y點位置畫文字,寬度為18

[text drawatpoint:cgpointmake(x, y) forwidth:18 withfont:font

minfontsize:8 actualfontsize:null

linebreakmode:uilinebreakmodetailtruncation

baselineadjustment:uibaselineadjustmentalignbaselines];

[text release]; 

///// only override drawrect: if you perform custom drawing.

// an empty implementation adversely affects performance during animation.

- (void)drawrect:(cgrect)rect

// drawing code

cgcontextref

context = 

uigraphicsgetcurrentcontext();

cgcontextsetlinewidth(context, 

2.0);

cgcontextsetstrokecolorwithcolor(context, [[

uicolorredcolor] 

cgcolor]);

cgcontextmovetopoint

(context, 

self

.bounds.origin.x, 

self

.bounds.origin.y);

cgcontextaddlinetopoint

(context, 

self

.bounds.size.width, 

self

.bounds.size.height);

cgcontextstrokepath(context);

iOS畫矩形 直線 文字的方法

cgpoint畫直線 cgcontextref ctx uigraphicsgetcurrentcontext cgcontextmovetopoint ctx,point1.x,point1.y cgcontextaddlinetopoint ctx,point2.x,point2.y 畫曲線 用...

iOS 繪製直線 矩形 文字的方式

首先,獲取上下文 cgcontextref context uigraphicsgetcurrentcontext 畫線 設定畫筆線條粗細 cgcontextsetlinewidth context,5.0 設定線條樣式 cgcontextsetlinecap context,kcglinecapb...

IOS開發 畫直線

介紹兩種畫直線的方法 1 通過quartzcore 2 通過uibezierpath 先建立乙個自定義view 然後在匯入quartzcore框架 然後在m檔案打出下面 三種函式都是同樣的效果 import zcview.h import implementation zcview only ove...