2014-03-26 31 views
0

我用我的應用程序的IOS庫UIPlaceHolderTextView文本區域的高度:https://github.com/JohnnyZeng/UIPlaceHolderTextView鏈接到以下問題:無法計算的iOS

Placeholder in UITextView

但是本身工作正常,圖書館,我需要在用戶輸入文本後計算區域的高度,不幸的是,儘管文本區域包含文本,但我總是會得到0.00000的值。爲什麼是這樣?這裏是我的相關代碼:

//self.textView is a reference to a UIPlaceHolderTextView object, and text is its attribute 

self.textView.text = @"Text that the user has entered, and can be of any length"; 
    float height = MIN(175, [self.textView.attributedText boundingRectWithSize:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:NULL].size.height); 

NSLog(@"textview height is: %f", self.textView.frame.origin.y); 
NSLog(@"textview height is: %f", self.textView.frame.size.height); 

兩行的輸出都是0.00000。任何人都可以看到爲什麼會發生?我的猜測是問題在於表達式self.textView.attributedText,但不知道我是否正確,如果是這樣,我應該做什麼。

在此先感謝所有回覆的人。

+1

...和'的NSLog(@ 「%F」,高度)哪些工作;'會說...?因爲它似乎只有在你的代碼片段中具有計算的高度,實際的'self.textView'沒有。 – holex

+0

我不確定我是否理解。你能澄清一下嗎? – syedfa

+0

我的輸出是0.00000。我如何糾正你所指出的情況? – syedfa

回答

0

要分配

`self.textView.text = @"Text that the user has entered, and can be of any length";` 

和計算heightfor self.textView.attributedText這是無我想請做相應的改變。它應該工作。

要的是你應該將值分配給self.textView.attributedText

+0

應該沒關係,因爲'attributesText'會自動從'text','font','textColor','textAlignment'等等值中自動填充,反之亦然,正如文檔中所述:https://developer.apple.com/library/ios/documentation/uikit/reference/uitextview_class/Reference/UITextView.html#//apple_ref/occ/instp/UITextView – holex