[英]IOS 7 - Can't set font color of UITextView in custom UITableView cell
我堅持認為我認為這是一件簡單的事情。 我有一個自定義表格單元格,其中包含一個UITextView對象。 我希望該對象中的文本為灰色。 當我在界面生成器中設置顏色時,它顯示為灰色,但是在模擬器中,它保持黑色。
以下是其設置方式的摘要:
我更新了MasterViewController來填充我的自定義單元。 當我運行模擬器時,就顯示數據的自定義單元而言,它們都可以正常工作。
如前所述,當我嘗試更改自定義單元格的UITextView中的文本顏色時,就會出現問題。 如果在界面構建器中將其設置為“淺灰色”,則它在模擬器中無效。
我在下面提供了相關的代碼段和屏幕截圖。 我不確定100%是否與問題相關,什么與問題無關,如果您需要查看下面未顯示的內容,請詢問並提供。
**編輯
當我注銷UITextView的color屬性時,它輸出(空)。 請參見下面的MasterViewController.m代碼。
自定義單元格-NTFYNoteCell.h
#import <UIKit/UIKit.h>
@interface NTFYNoteCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextView *descriptionText;
@end
取自MasterViewController.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NTFYNoteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NTFYNote *note = _notes[indexPath.row];
cell.titleLabel.text = [note title];
cell.descriptionText.text = [note shortDescription];
NSLog(@"TEXTVIEW: %@", cell.descriptionText);
NSLog(@"TEXT COLOR: %@", cell.descriptionText.textColor); // This outputs (null)
return cell;
}
從上面的代碼中調試:
2014-01-19 19:42:52.001 appname[5050:70b] TEXTVIEW: <UITextView: 0xa1bfe00; frame = (14 26; 273 57); text = 'This is a generic descrip...'; clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x8be0530>; layer = <CALayer: 0x8be1fe0>; contentOffset: {0, 0}>
2014-01-19 19:42:52.001 appname[5050:70b] TEXT COLOR: (null)
2014-01-19 19:42:52.004 appname[5050:70b] TEXTVIEW: <UITextView: 0xa1cd400; frame = (14 26; 273 57); text = 'This is a generic descrip...'; clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x8b8fb40>; layer = <CALayer: 0x8bf5300>; contentOffset: {0, 0}>
2014-01-19 19:42:52.005 appname[5050:70b] TEXT COLOR: (null)
2014-01-19 19:42:52.007 appname[5050:70b] TEXTVIEW: <UITextView: 0xe17e000; frame = (14 26; 273 57); text = 'This is a generic descrip...'; clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0xd857bb0>; layer = <CALayer: 0xd85d610>; contentOffset: {0, 0}>
2014-01-19 19:42:52.007 appname[5050:70b] TEXT COLOR: (null)
2014-01-19 19:42:52.009 appname[5050:70b] TEXTVIEW: <UITextView: 0x99af200; frame = (14 26; 273 57); text = 'This is a generic descrip...'; clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x8a81ea0>; layer = <CALayer: 0x8a580c0>; contentOffset: {0, 0}>
2014-01-19 19:42:52.009 appname[5050:70b] TEXT COLOR: (null)
UITextView連接到“自定義單元”中的插座:
在界面生成器中將字體顏色設置為淺灰色:
iPhone模擬器中的外觀:
解決了這個問題后,我帶着新信息回到了Google,發現了以下Stack Overflow問題:
事實證明,如果不選中UITextView屬性下的“ Selectable”復選框,它將不響應任何字體更改,包括顏色。
我選中了此框,現在一切正常。
謝謝你的幫助!
如果您的UITextView包含被檢測為地址/電話號碼/等的文本,則必須設置tintColor來更改檢測到的項目的文本顏色,如下所示:
self.tvPhone.tintColor = [UIColor blueColor];
這對我有用。 設置可選的是,設置顏色后設置否
cell.txtvComment.selectable = YES;
cell.txtvComment.textColor = [Globals getTextColor]; //set your UIColor
cell.txtvComment.text = comment.commentText;
cell.txtvComment.selectable = NO;
令人困惑的是,此更改在您的應用中沒有生效,但是由於您已經為文本字段創建了出口,因此不妨添加一行代碼以編程方式設置顏色:
cell.descriptionText.textColor = [UIColor grayColor];
這樣可以確保將顏色設置為所需的顏色,因此您不必依賴Interface Builder。
編輯:您提到您已經嘗試了此。 我會嘗試在您的代碼中插入一些NSLog()語句,以查看您要進行的一系列更改:
NTFYNoteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NTFYNote *note = _notes[indexPath.row];
cell.titleLabel.text = [note title];
NSLog(@"descriptionText color:%@", cell.descriptionText.textColor);
cell.descriptionText.text = [note shortDescription];
cell.descriptionText.textColor = [UIColor grayColor];
NSLog(@"descriptionText color:%@", cell.descriptionText.textColor);
return cell;
如果輸出為(null),則文本字段一定有問題,因為它必須具有顏色,甚至是默認顏色(黑色)。 將NSLog語句之一替換為:
NSLog(@"%@", cell.descriptionText)
確保“ descriptionText”實際上指向您的文本字段。 您應該能夠使用該指針設置文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.