Ios NSMutableAttributedString通过动画完成 -(无效)设置行程标签:(BOOL)行程标签 { _strokeLabel=strokeLabel; if(_strokeLabel){ _计时器=[NSTimer scheduledTimerWithTimeInterval:0.4目标:自选择器:@selector(setStroketThrough)userInfo:nil repeats:NO]; }否则{ [自动取消行程]; } } -(空)定位行程 { NSMutableAttributedString*attributedString=[[NSMutableAttributedString alloc]initWithAttributedString:self.attributedText]; 对于(i=1;i
,这里有两个函数可以完成此任务。Ios NSMutableAttributedString通过动画完成 -(无效)设置行程标签:(BOOL)行程标签 { _strokeLabel=strokeLabel; if(_strokeLabel){ _计时器=[NSTimer scheduledTimerWithTimeInterval:0.4目标:自选择器:@selector(setStroketThrough)userInfo:nil repeats:NO]; }否则{ [自动取消行程]; } } -(空)定位行程 { NSMutableAttributedString*attributedString=[[NSMutableAttributedString alloc]initWithAttributedString:self.attributedText]; 对于(i=1;i,ios,objective-c,Ios,Objective C,,这里有两个函数可以完成此任务。 - (void)setStrokeLabel:(BOOL)strokeLabel { _strokeLabel = strokeLabel; if (_strokeLabel) { _timer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(setStrokeThrough) userInfo:nil repeats:NO
- (void)setStrokeLabel:(BOOL)strokeLabel
{
_strokeLabel = strokeLabel;
if (_strokeLabel) {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(setStrokeThrough) userInfo:nil repeats:NO];
} else {
[self cancelStrokeThrough];
}
}
- (void)setStrokeThrough
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
for (NSUInteger i = 1; i <= [attributedString length]; i++) {
[attributedString addAttribute:NSStrikethroughStyleAttributeName
value:[NSNumber numberWithInt:1]
range:NSMakeRange(0, i)];
self.attributedText = attributedString;
}
}
- (void)cancelStrokeThrough
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
[attributedString removeAttribute:NSStrikethroughStyleAttributeName
range:NSMakeRange(0, [attributedString length])];
self.attributedText = attributedString;
}
注释
这不是一个可设置动画的属性。你最好自己设置一个计时器,然后一个字母一个字母地写。谢谢@Borrden,尽管我为它设置了一个NSTimer,但结果并不明显。我不知道你所说的“结果不明显”是什么意思,我希望它一个字母一个字母地慢慢敲打,\u timer=[NSTimer scheduledTimerWithTimeInterval:0.5目标:自选择器:@selector(SetStroketThrough)userInfo:nil repeats:NO];出现错误?该计时器不会重复,但您应该使用所有相关代码更新您的问题。
BOOL setStrokethrough(UILabel *label, NSRange range)
{
if (range.location >= [label.attributedText length])
return FALSE;
if (range.location + range.length > [label.attributedText length])
range.length = [label.attributedText length] - range.location;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];
[attributedString addAttribute:NSStrikethroughStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:range];
label.attributedText = attributedString;
return TRUE;
}
-(void)animateSetStrokethroughDuration:(float)duration
{
__block float const stepDuration = 0.1;
float steps = duration / stepDuration;
__block NSRange range = NSMakeRange(0, ceil((float)[self.label.attributedText length] / steps));
void (^__block fn)();
void (^__block __weak weakfn)();
weakfn = fn = ^(){
if (!setStrokethrough(self.label, range))
return;
range = NSMakeRange(range.location + range.length, range.length);
[self performBlock:weakfn afterDelay:stepDuration];
};
fn();
}