2012-01-26 28 views
2

我創建了一個UITextView編程與後續:iOS UITextView - setText方法不顯示我的文本?

messageBodyTxt = [[UITextView alloc] initWithFrame:CGRectMake(36, 204, 763, 251)]; 
[messageBodyTxt setTextColor:[UIColor colorWithRed:0.627 green:0.627 blue:0.627 alpha:1]]; 
[messageBodyTxt setBackgroundColor:[UIColor clearColor]]; 
[messageBodyTxt setText:@"some random text"]; 

然而,的setText屬性不顯示我的文字。我的頭文件是這樣的:

#import <UIKit/UIKit.h> 

@interface MessageView : UIView { 
    UITextView *messageBodyTxt; 
} 

@end 

,我已經把它添加到我的觀點,像這樣:

[self addSubview:messageBodyTxt]; 

我所有的其他UILabel的功能正常時,我使用的setText方法,但我似乎無法得到UITextView顯示任何東西。

任何想法?

+0

嘗試'messageBodyTxt.text = @「一些隨機文本」;'代替 – 2012-01-26 20:52:05

+2

將textview添加到屏幕的位置。如果將背景顏色更改爲明顯的東西,你還能看到它嗎?不知道爲什麼你的代碼無法工作,除非它沒有在屏幕上使用。 –

+0

@AmitShah同意。另外,在你的視圖層次結構中它是什麼?它會被另一個不透明的UIView遮擋嗎? – ddodev

回答

2

我想通了。父視圖的框架太小,沒有被剪裁,所以我可以看到視圖中顯示的所有其他元素,但它們不是可點擊的(例如按鈕等)。

只要我修正了父視圖框架的大小,'UITextView文本神奇地出現了。

謝謝大家,你做得棒極了!

+0

你可以使用IB來做這樣簡單的事情。這很容易,並防止一些麻煩。 – kaspartus

2

這真的很奇怪。我創建了「1視圖」項目。 然後: ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController: UIViewController { 
    UITextView *exampleTxt; 
} 

@end 

轉到ViewController.m(viewDidLoad中):

-(void)viewDidLoad{ 
    [super viewDidLoad]; 
    exampleTxt = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; 
    [exampleTxt setText:@"Everything Ok"]; 
    [exampleTxt setColor:[UIColor redColor]]; //yes, it's deprecated, but works 
    [self.view addSubview: exampleTxt]; //because its ViewController 
} 

所有的一切都OK。 將您的viewController與您的MessageView鏈接起來看起來很麻煩。嘗試使用Interface Builder。 或顯示更多的代碼。