我想把Marquee Label用不同的标签文字颜色

时间:2013-10-26 05:59:26

标签: ios xcode uitableview uilabel marquee

我想将Marquee Label放在UITableView单元格中,但是像标签文本那样花费不同的颜色

我正在使用MarqueeLabel Classes,我可以在UITableViewCell上显示Marquee标签,它非常有用。

我也尝试过NSAttributedString,但MarqueeLabel不支持不同颜色的标签文字

如果有人回答那么请给我

感谢。

这是我的代码

[cell.contentView addSubview:[self createMarqueeLabelWithIndex:indexPath.row]];

[cell.textLabel setTextColor:[UIColor redColor] range:NSMakeRange(4, 3)];

-(MarqueeLabel *)createMarqueeLabelWithIndex:(int)index
{

    MarqueeLabel *continuousLabel2 = [[MarqueeLabel alloc] initWithFrame:CGRectMake(10,0,300,30) rate:50.0f andFadeLength:10.0f];
    continuousLabel2.marqueeType = MLContinuous;
    continuousLabel2.continuousMarqueeSeparator = @"";
    continuousLabel2.animationCurve = UIViewAnimationOptionCurveLinear;
    continuousLabel2.numberOfLines = 1;
    continuousLabel2.opaque = NO;
    continuousLabel2.enabled = YES;
    continuousLabel2.shadowOffset = CGSizeMake(0.0, -1.0);
    continuousLabel2.textAlignment = UITextAlignmentLeft;
    continuousLabel2.backgroundColor = [UIColor clearColor];
    continuousLabel2.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.000];

    NSString *strText = [[arrTicker objectAtIndex:index] objectForKey:@"text"];
    NSString *strTime = [[arrTicker objectAtIndex:index] objectForKey:@"time"];
    NSString *strUser = [[arrTicker objectAtIndex:index] objectForKey:@"userid"];

    NSString *strTemp = [NSString stringWithFormat:@"%@ %@ %@    ",strText,strTime,strUser];

    continuousLabel2.text = [NSString stringWithFormat:@"%@",strTemp];

    return continuousLabel2;
}

2 个答案:

答案 0 :(得分:3)

在你的cellforrowatindexpath中创建uilabel并将你的marque标签分配给uilabel的文本集到属性字符串,然后将uilabel转换为marque标签,然后将子视图添加到你的单元格。

希望这会对你有所帮助。 感谢。

答案 1 :(得分:1)

不是最好的答案,而是一个肮脏的黑客。

经过快速调查后,我刚刚将[self setTextColor:[super textColor]];添加到forwardPropertiesToSubLabel

- (void)forwardPropertiesToSubLabel {
    // Since we're a UILabel, we actually do implement all of UILabel's properties.
    // We don't care about these values, we just want to forward them on to our sublabel.
    NSArray *properties = @[@"baselineAdjustment", @"enabled", @"font", @"highlighted", @"highlightedTextColor", @"minimumFontSize", @"shadowColor", @"shadowOffset", @"textAlignment", @"textColor", @"userInteractionEnabled", @"text", @"adjustsFontSizeToFitWidth", @"lineBreakMode", @"numberOfLines", @"backgroundColor"];
    for (NSString *property in properties) {
        id val = [super valueForKey:property];
        [self.subLabel setValue:val forKey:property];
    }
    [self setText:[super text]];
    [self setFont:[super font]];
    [self setTextColor:[super textColor]];
}

现在我可以通过情节提要编辑器

更改标签颜色

Editing MarqueeLabel in Storyboard

NB:正如您所看到的,我使用纯文本。对于属性文本,hack不起作用。