2016-04-30 23 views
-1

我有多行標籤,它的幀根據文本長度正確變化。當我得到標籤的寬度時,它計數整個標籤的寬度,而我只想要第一行寬度。 我正在使用此代碼來獲取每一行。從多行標籤獲得第一行 - 物鏡c

NSArray *linesArray = [self getLinesArrayOfStringInLabel:label]; 

當文本太長其工作正常,但是當文字很短的休息線在第二行最後一個字和標籤它顯示單行。

enter image description here

和數組是

linesArray = (
    " this is multiline label. I want just first ", 
    "line." 
) 

怎麼回事錯在這裏?

+0

http://stackoverflow.com/questions/30435406/get-string-of-first-line-in-a-uilabel –

+0

我想知道你爲什麼要這樣做?也許有另一種方式做你想做的... –

+0

我使用的是相同的代碼... – Nisha

回答

0

您可以用下面的方法

//label is UILabel 
CGSize expectedLabelSize = [label.text boundingRectWithSize:self.view.frame.size 
             options:NSStringDrawingUsesLineFragmentOrigin 
             attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Font you are using" size:<size of font>]} 
             context:nil].size; 

您可以使用尺寸爲寬度和高度獲取標籤的動態大小。

+0

請寫評論在投票之前iy有什麼錯誤。 –