繁体   English   中英

检测UILabel文本的变化

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM