I have used the following code to add icon to my UILabel
UIImage *image3 = [UIImage imageNamed:@"icon_comments.png"];
UIImage *myIcon3 = [self imageWithImage:image3 scaledToSize:CGSizeMake(20, 20)];
NSTextAttachment *attachment3 = [[NSTextAttachment alloc] init];
attachment3.image = myIcon3;
NSAttributedString *attachmentString3 = [NSAttributedString attributedStringWithAttachment:attachment3];
NSString *temp3 = [NSString stringWithFormat: @"%d", test.noComments];
NSAttributedString *titleString3 = [[NSAttributedString alloc] initWithString:temp3];
NSMutableAttributedString *myString3 = [[NSMutableAttributedString alloc] initWithString:@""];
[myString3 appendAttributedString:attachmentString3];
[myString3 appendAttributedString:titleString3];
cell.noComments.attributedText = myString3;
the problem now that the text is not aligned vertically with the icon, can any one tell me what is the problem and how can I solve it
This works in iOS >= 7.0 for some _label
NSRange range = [myString3.string rangeOfString:titleString3.string];
CGFloat dy = (_label.frame.size.height - _label.font.pointSize) / 2;
[myString3 addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithFloat:dy] range:range];
_label.attributedText = myString3;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments