[英]Detect change of UILabel's text
我有一段時間每隔0.1秒重復一次(我這樣做是因為我需要不斷尋找網站文字的變化)。 我的問題是我需要能夠判斷文本何時從一個單詞變為另一個單詞。 因此,當用戶打開應用程序並讓我們說文本顯示一個歌曲標題但隨后它變為另一個時,它發生變化,我需要能夠檢測到它並執行一個動作。
此外,必須是應用程序首次加載時它不會執行操作,但是當文本更改時它將顯示它(它必須是這個因為我正在拉歌曲標題)。 所以我需要能夠改變每首歌曲的持續時間,但如果正在播放一首歌並且他們在中間打開應用程序,則持續時間會混淆。 所以我有兩個標簽,一個顯示文字,另一個顯示時間。 當應用程序首次加載時,我希望它在歌曲標題中的文本更改之前不顯示任何內容,然后從if語句(下面)中提取持續時間。 這可能有點令人困惑,但問你需要的任何問題,我會嘗試解釋。
以下是我從網站上提取的代碼以及其他可能有用的內容:
- (void)viewDidLoad {
[super viewDidLoad];
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(recentTracksText) userInfo:nil repeats:YES];
}
-(void)recentTracksText {
textForBlog = [webViewForRecents stringByEvaluatingJavaScriptFromString:@"document.getElementById('current_song').textContent;"];
self.strippedTextForBlog = [self stringByStrippingHTMLFromString:textForBlog];
continuousLabel.text = self.strippedTextForBlog;
}
if ([continuousLabel.text isEqual: @"Lady Gaga - Gypsy"]) {
imageView1.image = [UIImage imageNamed:@"Lady Gaga - Gypsy.jpg"];
imageView.hidden = YES;
[self.view insertSubview:toolbar belowSubview:imageView];
[toolbar insertSubview:darkView belowSubview:imageView];
[self.view insertSubview:backgroundImage belowSubview:toolbar];
if([darkView.subviews containsObject:continuousLabel]) {
} else{
dispatch_queue_t queue = dispatch_get_global_queue(0,0);
dispatch_async(queue, ^{
[darkView addSubview:Label];
[Label setText:@"1:30"];
[darkView addSubview:continuousLabel];
});
}
}
UPDATE
-(void)recentTracksText {
textForBlog = [webViewForRecents stringByEvaluatingJavaScriptFromString:@"document.getElementById('current_song').textContent;"];
self.strippedTextForBlog = [self stringByStrippingHTMLFromString:textForBlog];
if (![self.strippedTextForBlog isEqualToString:continuousLabel.text])// this does not find when the text changes {
[self.pLabel setHidden:NO];
[self.pLabel setProgress:1
timing:TPPropertyAnimationTimingEaseOut
duration:150.0
delay:0.0]; //This does not go smoothy.
// this does not
}
continuousLabel.text = self.strippedTextForBlog;
}
[continuousLabel addObserver:self
forKeyPath:@"text"
options:0
context:NULL];
Make sure you implement the observing method
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
// Insert your code here to deal with the change.
}
和一點ARC管理品嘗:
- (void)dealloc {
NSLog(@"dealloc %@", [self class]);
if (continuousLabel) {
[continuousLabel removeObserver:self forKeyPath:@"text"];
}
}
好吧,你已經擁有每0.1秒發射一次的NSTimer
,所以你只需要檢查文本是否沒有改變。
-(void)recentTracksText {
textForBlog = [webViewForRecents stringByEvaluatingJavaScriptFromString:@"document.getElementById('current_song').textContent;"];
self.strippedTextForBlog = [self stringByStrippingHTMLFromString:textForBlog];
if (![self.self.strippedTextForBlog isEqualToString:continuousLabel.text]) {
//The text isn't the same. Do something.
}
continuousLabel.text = self.strippedTextForBlog;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.