次のUITableViewCellコードが与えられた場合、myObj.firstName
は赤、myObj.lastName
は緑色にするにはどうすればよいですか?UITableViewCellの複数の色
UITableViewCell *cell;
....
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", myObj.firstName, myObj.lastName];
次のUITableViewCellコードが与えられた場合、myObj.firstName
は赤、myObj.lastName
は緑色にするにはどうすればよいですか?UITableViewCellの複数の色
UITableViewCell *cell;
....
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", myObj.firstName, myObj.lastName];
あなたはUILabel
がNSAttributedString
の描画インスタンスをサポートしていないため、これを達成するためにUILabel
の2つのインスタンスを必要とするつもりです。 UITableViewCell
をサブクラス化し、drawRect:
をオーバーライドし、テキストをNSStringの-drawTextAtPoint:
メソッドで必要な色で手動で描画することもできます。
cell.textlabelをOHAttributedLabelインスタンスに設定できます。
これはios5では動作しないようです。 CoreTextフレームワークを追加しても、多くのエラーが発生します。 – jwhat
おそらくこれは、私は色が動的データに応じて設定する必要があります...
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
...
// Get the size of UILabel firstName.
CGSize size = [[cell.firstName text] sizeWithFont:[cell.firstName font]];
// Draw UILabel lastName next to UILabel firstName.
// UILabel firstName was initialized in CustomCell.m in -(void)layoutSubviews.
cell.lastName.frame = CGRectMake(size.width + 50, 23, 40, 15);
// Make lastName green, oh yeah!
cell.lastName.textColor = [UIColor colorWithRed:0 green:0.6 blue:0 alpha:1];
}
でこれを解決してきた、他人を助けることがあります。これはサブクラスでも使えるものなのでしょうか? – jwhat