iOS Warum verschwindet NSTextAttachment beim Festlegen von NSBaselineOffsetAttributeName?
Ich mag die NSTextAttachment in der Mitte des Textes auszurichten, so dass ich NSBaselineOffsetAttributeName die Basislinie von NSTextAttachment zu ändern.
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"STHeitiSC-Light" size:17]}];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"micon"];
NSMutableAttributedString *ats = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];
[ats addAttributes:@{NSBaselineOffsetAttributeName:@(-5),NSFontAttributeName: [UIFont fontWithName:@"STHeitiSC-Light" size:17]} range:(NSRange){0,ats.length}];
[attrString appendAttributedString:s];
Dann berechne ich die Größe für UILabel und setze attributierteText.
CGRect textFrame = [attrString boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
UILabel *label = [[UILabel alloc] initWithFrame:textFrame];
label.lineBreakMode = NSLineBreakByCharWrapping;
label.numberOfLines = 0;
label.attributedText = attributed;
label.backgroundColor = [UIColor clearColor];
Endlich verschwand das letzte Bild.
Kann mir jemand erklären, warum dies geschieht und wie man es beheben.
Hallo Brache diesen Link seine sehr hilfreiche Sie http://StackOverflow.com/Questions/26105803/center-nneteattachment-image-next-to-single-line-uilabel – Vinayak