1

UITableViewCell の detailTextLabel で 1 つの文字列のフォントを変更するにはどうすればよいですか?

NSString *detailStr = [NSString stringWithFormat:@"%@ %@",post.score,post.domain];

cell.detailTextLabel.text = detailStr;

私は基本的に、post.score文字列をある色にして、post.domain文字列を別の色にしたいと考えています。

4

3 に答える 3

2

回答: NSAttributedString

これを試して :

int count1 = [post.score length];
int count2 = [post.domain length];
NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:@"%@ %@",post.score,post.domain];
[textLabelStr setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(0, count1)];
[textLabelStr setAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(count1 + 1, count2)];
cell.detailTextLabel.attributedText = textLabelStr;

注:テストされていませんが、役立つコードを書いてください。

于 2013-05-09T14:28:04.177 に答える
1

setAttributedText:の方法をお探しだと思いますUILabel。を渡してNSAttributedString、文字列のさまざまな部分をさまざまな方法でスタイル化できます。
以下に、あなたが求めていることを行うサンプルを書きました。

NSAttributedString *scoreAttributedString = [[NSAttributedString alloc] initWithString:[post score] attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];
NSAttributedString *domainAttributedString = [[NSAttributedString alloc] initWithString:[post domain] attributes:@{NSForegroundColorAttributeName: [UIColor greenColor]}];

NSMutableAttributedString *detailMutableAttributedString = [[NSMutableAttributedString alloc] init];
[detailMutableAttributedString appendAttributedString:scoreAttributedString];
[detailMutableAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
[detailMutableAttributedString appendAttributedString:domainAttributedString];

[[cell detailTextLabel] setAttributedText:detailMutableAttributedString];
于 2013-05-09T14:31:36.587 に答える
1

iOS6+ API を使用することに満足している場合は、チェックアウトしNSAttributedStringて使用してくださいcell.detailTextLabel.attributedText = ...

于 2013-05-09T14:28:02.170 に答える