iOS Alert內容左對齊

2021-08-08 14:36:37 字數 3439 閱讀 2396

最近經常有人問我,uialertview和uialertcontroller如何能讓內容文字左對齊,其實有三種方式可以解決這個問題。

接下來,我就簡單分析一下。

nsstring *message = @"1. 調整***功能,更新為***;2. 優化***功能,提供了***;3. 優化***介面;4. 優化網路載入速度,新增網路預載入功能";
我們就簡單實現一下,利用nsstring自帶的方法,把;替換成\n來實現換行。

message = [message stringbyreplacingoccurrencesofstring:@";" withstring:@"\n"];
建立uialertcontroller,並把修改好的message傳入進去

uialertcontroller *alertvc = [uialertcontroller

alertcontrollerwithtitle:@"更新提示!"

message:message preferredstyle:

uialertcontrollerstylealert];

uialertaction *cancelaction = [uialertaction

actionwithtitle:@"取消"

style:

uialertactionstylecancel

handler:

nil];

uialertaction * updateaction = [uialertaction

actionwithtitle:@"更新"

style:

uialertactionstyledefault

handler:^(uialertaction * _nonnull action) ];

[alertvc addaction:cancelaction];

[alertvc addaction:updateaction];

[self

presentviewcontroller: alertvc animated:

yescompletion:

nil];

但是我們發現,每行的文字內容預設是居中對齊的,效果如下:

這樣看起來給使用者的感覺不是特別好,那麼我們如何讓文字居左對齊呢?

例項化uialertcontroller之後,可以遍歷其view的子檢視,當我們遍歷到第6層的subviews的時候,會發現找到了我們需要的title和message所對應的label。在這裡,我們直接獲取第6層的subviews。

nsarray *subviews = alertvc.view

.subviews[0].subviews[0].subviews[0].subviews[0].subviews[0].subviews

;

獲取title和message所對應的label。

uilabel *titlelb = subviews[0];

uilabel *messagelb = subviews[1];

設定message所對應的label居左對齊。

messagelb.textalignment =nstextalignmentleft;
這樣既可滿足我們之前的需求,效果如下:

建立uialertview,並把修改好的message傳入進去

uialertview *alert = [[uialertview alloc] initwithtitle:@"更新提示!"

message:message delegate:

self

cancelbuttontitle:@"取消"

otherbuttontitles:@"更新", nil];

[alert show];

效果如圖1,每行的文字內容預設是居中對齊的。

還用之前的方式,獲取其subviews子檢視,我們發現,裡面沒有內容。那我們換一種方式。看一看uialertview內部都有哪些屬性或者成員變數。

通過runtime獲取其內部成員變數,我們發現uialertview大致有如下成員變數。

@inte***ce

uialertview : uiview

細心的讀者可能已經發現,裡面有乙個成員變數是_alertcontroller,型別就是我們上面使用的uialertcontroller

注意:其實在之前的ios版本中,uialertview的成員變數裡包含_titlelabel,_bodytextlabel,但是在ios9以後,內部實現已經改變,這兩個成員變數已經消失。

那我們就利用一下這個成員變數_alertcontroller

uialertcontroller *alertvc = [alert valueforkey:@"_alertcontroller"];
同上,遍歷其view的子檢視,同樣也是第6層,那麼,我們直接獲取第6層的subviews。

nsarray *subviews = alertvc.view

.subviews[0].subviews[0].subviews[0].subviews[0].subviews[0].subviews

;

獲取title和message所對應的label。

uilabel *titlelb = subviews[0];

uilabel *messagelb = subviews[1];

設定message所對應的label居左對齊。

messagelb.textalignment =nstextalignmentleft;
同樣也能實現我們的需求,效果如下:

對比上面兩種方式來說,我更推薦使用這種方式。

完全使用自定義的alertview,顏色更改,字型更改,自定義彈框內容等,能夠更加靈活的更改。

以上就是本篇文章的基本內容,如有什麼問題,還望讀者給予意見。

UIAlertView 設定內容左對齊

思路 蘋果原生是不請允許我們對這個alertview進行齊它設定的,但是我們可以用kvc的方式對label進行重設 nsstring ccc 1 增加白菜黨等特色標籤篩選 n2 增加頻道熱度排行 n3 增加夜間模式 n4 material design風格優化 n5 滑動返回優化 n6 其他bug修...

對齊和內容對齊

horizontalalignment left,center,hight,stretch default verticalalignment top,center,bottom,stretch default 可以讓乙個元素自己決定如何使用父面板給它的額外空間。只有當乙個父面板給子元素的空間比子元...

UIButton文字左對齊

uibutton 文字是 titlelabel顯示的 如果設定 btn.textlabel.textalignment uitextalignmentleft 表明 textlabel 裡面的文字是左對齊的 但實際並沒有左對齊 因為 uibutton 裡面的 titlelabel 的frame,會適...