0

次のリンクのように UILabel テキストの色を徐々に変更するにはどうすればよいですか? 誰かが私にいくつかのコードを提案できますか?

4

4 に答える 4

2

書式設定されたテキストを使用できます。

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

またはコードを再利用します。

https://github.com/mattt/TTTAttributedLabel

于 2012-12-15T10:13:05.850 に答える
0

リンクしたアプリ[http://www.youtube.com/watch?v=_vOYvaNhSHw]は、おそらくを使用して作成されていcocos2dます。
ではcocos2d、アニメーションでもテキストの色を簡単に変更できます。

ここに例があります:http:
//www.cocos2d-iphone.org/forum/topic/5903

ここでcosos2dsdkを試してみることをお勧めします。これは非常に強力なので、http:
//www.cocos2d-iphone.org/

楽しい。

于 2012-12-15T11:11:34.550 に答える
0

iOS 6.0からごNSAtributedString利用ください。UILabeliOS 6.0 より前のバージョンでは、 NSAtributedStringをサポートする TTTAttributedLabel を使用します。

それによってあなたにattributed string応じて変更requirementsettingagainUILabel

colored textたとえば、ループ内で必要に応じて編集を追加します

ラベルの最初の 1 秒間: 私はいい子です。

ラベルの 2 秒目 :はいい子です。

ラベルの 3 秒目: 私いい子です。

ラベルの 4 秒目 : 私はいい子です。

ラベルの 5 秒目: I am good boy .

于 2012-12-15T10:14:36.807 に答える
0

これが私のサンプルコードの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;
    }];
于 2012-12-15T10:24:54.837 に答える