2011-10-18 37 views
2

我正在用UILabel With Timer加載歌詞。現在我想用不同的顏色突出顯示該標籤的每個單詞。我怎樣才能做到這一點 ?如何在iphone中突出顯示UILabel的單詞

我顯示的歌詞這樣

- (void)setCurrentAudioProgress:(NSTimeInterval)time duration:(NSTimeInterval)duration 
{ 
    float progress = time/duration; 
    long currentPlaybackTime = audioPlayer.currentTime; 
    long remainingTime = (duration-time); 

    int remainingHours = (remainingTime /3600); 

    int remainingMinutes = ((remainingTime /60 -remainingHours*60)); 

    int remainingSeconds = (remainingTime %60); 


    int currentHours = (currentPlaybackTime/3600); 

    int currentMinutes = ((currentPlaybackTime/60) - currentHours*60); 
    int currentSeconds = (currentPlaybackTime % 60); 

    currentTimeLabel.text = [NSString stringWithFormat:@"%i:%02d:%02d", currentHours, currentMinutes, currentSeconds]; 

    remainingTimeLabel.text = [NSString stringWithFormat:@"%i.%02d.%02d", remainingHours , remainingMinutes, remainingSeconds]; 

    [progressView setProgress:progress]; 

    for (int i = 0; i<[lyricsArray count]; i++) { 

     NSString *lyricsTime = [[lyricsArray objectAtIndex:i]valueForKey:@"startTime" ] ; 

     if ([currentTimeLabel.text isEqualToString:lyricsTime]) { 

      songLyricsLabel.text = [NSString stringWithFormat:@"%@",[[lyricsArray objectAtIndex:i]valueForKey:@"songLyrics" ]];   
     }    
    }   
} 
+0

嗨。你有沒有解決這個問題? – ScarletWitch

回答

0

您可以使用下面你只要要突出顯示標籤文字,然後爲相應標籤添加以下行:

 label.highlighted = YES; 
label.highlightedTextColor = [UIColor blueColor]; 

對於不同的顏色,你可以添加一個數組的顏色名稱,並隨機選擇....

+0

我想突出顯示UILAbel的每個單詞 – iProgrammer

+0

當您設置label.highlighted = YES時,它將突出顯示具有指定顏色的標籤的整個文本。但是,如果你想用不同的顏色突出每個單詞,那麼它將不起作用,你需要添加一個函數,它將逐個取出每個字符並設置它的顏色.... – Minakshi