检测UILabel文本的变化

时间:2014-05-05 23:37:03

标签: ios objective-c uilabel

我有一段时间每隔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];
                });
            }

        } 

更新

-(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;
}

2 个答案:

答案 0 :(得分:2)

Use Key-value observing if you want to observe the property directly

[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"];
    }
}

答案 1 :(得分:0)

嗯,你已经有NSTimer每0.1秒触发一次,所以你只需要检查文本是否没有改变。

-(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;

}