iOS開發調整UILabel的行間距

2021-07-11 07:52:28 字數 1095 閱讀 2892

最近再做乙個專案時,發現uilabel中text的系統預設行間距不能滿足要求,於是在網上找到了調整行間距的**。跟大家分享一下,希望能對你有所幫助。

cgfloat heih = 20;

nsstring * clabelstring = @"這是測試uilabel行間距的text。這是測試uilabel行間距的text。n 這是測試uilabel行間距的text。n 這是測試uilabel行間距的text。這是測試uilabel行間距的text。這是測試uilabel行間距的text。這是測試uilabel行 間距的text。";

uilabel * clabel = [[uilabel alloc]initwithframe:cgrectmake(20, heih, 280, 200)];

clabel.numberoflines = 0;

clabel.font = [uifont fontwithname:fontname size:16];

clabel.textcolor = [uicolor graycolor];

nsmutableattributedstring * attributedstring1 = [[nsmutableattributedstring alloc] initwithstring:clabelstring];

nsmutableparagraphstyle * paragraphstyle1 = [[nsmutableparagraphstyle alloc] init];

[paragraphstyle1 setlinespacing:8];

[attributedstring1 addattribute:nsparagraphstyleattributename value:paragraphstyle1 range:nsmakerange(0, [clabelstring length])];

[clabel setattributedtext:attributedstring1];

[clabel sizetofit];

[self.view addsubview:clabel];

其實是使用了attributestring來實現字型的間距處理。

iOS開發 UILabel根據內容自動調整高度

寫法一 物件方法,傳入 字型 最大尺寸.即可得到寬高,最大尺寸主要限制寬度,如果是一行就給個 如果是多行就限制x值,y值隨便給 cgsize sizewithfont uifont font maxsize cgsize maxsize return self boundingrectwithsiz...

iOS開發之 UILabel的巧用

主要講解利用uilabel的來做乙個簡單的toast效果。這個是看到wdlabel的實現,感覺挺好的,就 主要的思路就是繼承uilabel,然後在drawrect cgrect rect裡面進行調整,以達到乙個toast的效果。define klabelcornerradius 9.0f void ...

IOS 開發日誌 UILabel相關

ios nsstring text first nsmutableattributedstring textlabelstr nsmutableattributedstring alloc initwithstring text textlabelstr setattributes range ns...