次のリンクのように UILabel テキストの色を徐々に変更するにはどうすればよいですか? 誰かが私にいくつかのコードを提案できますか?
4 に答える
書式設定されたテキストを使用できます。
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Hello World"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(1,5)];
iOS < 6
次に、UILabel をサブクラス化し、この文字列を drawRect メソッド内に出力する必要があります。発話速度に応じて色を変える何らかのタイプのループを作成する必要があります。
iOS6
attributedText プロパティを使用できます (サブクラス化する必要はありません)。
- (void)drawTextInRect:(CGRect)rect
またはコードを再利用します。
リンクしたアプリ[http://www.youtube.com/watch?v=_vOYvaNhSHw]は、おそらくを使用して作成されていcocos2d
ます。
ではcocos2d
、アニメーションでもテキストの色を簡単に変更できます。
ここに例があります:http:
//www.cocos2d-iphone.org/forum/topic/5903
ここでcosos2dsdkを試してみることをお勧めします。これは非常に強力なので、http:
//www.cocos2d-iphone.org/
楽しい。
iOS 6.0からごNSAtributedString
利用ください。UILabel
iOS 6.0 より前のバージョンでは、 NSAtributedStringをサポートする TTTAttributedLabel を使用します。
それによってあなたにattributed string
応じて変更requirement
setting
again
UILabel
colored text
たとえば、ループ内で必要に応じて編集を追加します
ラベルの最初の 1 秒間: 私はいい子です。
ラベルの 2 秒目 :私はいい子です。
ラベルの 3 秒目: 私はいい子です。
ラベルの 4 秒目 : 私はいい子です。
ラベルの 5 秒目: I am good boy .
これが私のサンプルコードの1つです。TTTAttributedLabel クラスのブロック メソッドを使用すると、役立つ場合があります。
[cell.lblAtt setText:strAtt afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
UIFont *italicSystemFont = [UIFont boldSystemFontOfSize:12];
CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL);
NSUInteger lenght = [[tempObj objectForKey:@"username"] length];
NSUInteger lenght2 = [[NSString stringWithFormat:@"%d",[tempArr count]] length];
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[ThemeColor CGColor] range:NSMakeRange(0,lenght)];
[mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName value:(__bridge UIFont*)italicFont range:NSMakeRange(0,lenght)];
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[ThemeColor CGColor] range:NSMakeRange(lenght+11,lenght2)];
[mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName value:(__bridge UIFont*)italicFont range:NSMakeRange(lenght+11,lenght2)];
return mutableAttributedString;
}];