我正在尝试为图表创建一个键,我需要一个字符串后跟一个彩色框,然后是另一个字符串。但是,尝试此操作时文本未显示。我之前没有使用过属性字符串,所以它可能是显而易见的。谢谢!
UILabel *titleLabel = (UILabel*) [cell viewWithTag:101];
UIView *myBox = [[UIView alloc] initWithFrame:CGRectMake(1, 1, 30, 30)];
myBox.backgroundColor = [UIColor blueColor];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"test string"];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [self imageWithView:myBox];
CGFloat oldWidth = textAttachment.image.size.width;
CGFloat scaleFactor = oldWidth / (titleLabel.frame.size.width - 10);
textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:scaleFactor orientation:UIImageOrientationUp];
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(6, 1) withAttributedString:attrStringWithImage];
titleLabel.attributedText = attributedString;