2

我想将选取框标签放在 UITableView 单元格中,但是像标签文本一样定制的颜色不同

我正在使用 MarqueeLabel 类,我能够在 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;
}
4

2 回答 2

3

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

希望这对您有所帮助。谢谢。

于 2013-11-20T09:37:43.623 回答
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]];
}

现在我可以通过情节提要编辑器更改标签颜色

在 Storyboard 中编辑 MarqueeLabel

注意:如您所见,我使用纯文本。对于属性文本破解不起作用。

于 2013-11-09T22:23:42.817 回答