iOS開發 給View新增指定位置的邊框線

2021-08-01 07:17:03 字數 2766 閱讀 7446

略微封裝了一下,給view新增指定位置的邊框線,其中位移列舉的使用詢問了哥們兒,總算搞定;

封裝一:直接封裝成了乙個方法

/// 邊框型別(位移列舉)

typedef ns_enum(nsinteger, uibordersidetype) ;

/** 設定view指定位置的邊框

@param originalview 原view

@param color 邊框顏色

@param borderwidth 邊框寬度

@param bordertype 邊框型別 例子: uibordersidetypetop|uibordersidetypebottom

@return view

*/- (uiview *)borderforview:(uiview *)originalview color:(uicolor *)color borderwidth:(cgfloat)borderwidth bordertype:(uibordersidetype)bordertype

/// 線的路徑

uibezierpath * bezierpath = [uibezierpath bezierpath];

/// 左側

if (bordertype & uibordersidetypeleft)

/// 右側

if (bordertype & uibordersidetyperight)

/// top

if (bordertype & uibordersidetypetop)

/// bottom

if (bordertype & uibordersidetypebottom)

cashapelayer * shapelayer = [cashapelayer layer];

shapelayer.strokecolor = color.cgcolor;

shapelayer.fillcolor = [uicolor clearcolor].cgcolor;

/// 新增路徑

shapelayer.path = bezierpath.cgpath;

/// 線寬度

shapelayer.linewidth = borderwidth;

[originalview.layer addsublayer:shapelayer];

return originalview;

}

封裝二:封裝成了類別

.h內容

#import typedef ns_options(nsuinteger, uibordersidetype) ;

@inte***ce uiview (borderline)

- (uiview *)borderforcolor:(uicolor *)color borderwidth:(cgfloat)borderwidth bordertype:(uibordersidetype)bordertype;

@end

.m內容

#import "uiview+borderline.h"

@implementation uiview (borderline)

- (uiview *)borderforcolor:(uicolor *)color borderwidth:(cgfloat)borderwidth bordertype:(uibordersidetype)bordertype

/// 左側

if (bordertype & uibordersidetypeleft)

/// 右側

if (bordertype & uibordersidetyperight)

/// top

if (bordertype & uibordersidetypetop)

/// bottom

if (bordertype & uibordersidetypebottom)

return self;

}- (cashapelayer *)addlineoriginpoint:(cgpoint)p0 topoint:(cgpoint)p1 color:(uicolor *)color borderwidth:(cgfloat)borderwidth

@end

用法:

uiview *testview = [[uiview alloc] initwithframe:cgrectmake(80.0f, 80.0f, 200.0f, 100.0f)];

testview.backgroundcolor = [uicolor lightgraycolor];

[self.view addsubview:testview];

[self borderforview:testview color:[uicolor redcolor] borderwidth:1.0f bordertype:uibordersidetypetop | uibordersidetypebottom];

效果:

不足之處,邊框線過寬的話,交界處會有留白;

ps:注意,需要先把你的view載入在父view上,[self.view addsubview:testview]; 之後再設定邊框;否則可能會不起作用的;

iOS開發給應用新增新字型的方法

拿到字型包檔案 字尾名為.ttf,或.odf 把字型庫匯入到工程的resouce中 在程式新增以下 輸出所有字型 nsarray familynames uifont familynames for nsstring familyname in familynames 假如你加入的字型為微軟雅黑,這...

Django開發,給admin模組新增富文字

第三步,接著在admin 模組文章類下引入這富文字編輯器 參考 class articleadmin admin.modeladmin list display title desc content click count is recommend date publish user categor...

oracle 新增列到指定位置

oracle中,1.如果表已經裝載了大量資料應該是用檢視來代替最好。alter table tablenm add newfield varchar2 10 rename tablenm to tablenmx create or replace view tablenm asselect fiel...