2014-09-11 11 views
7

beforeiOS 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.

after

Kann mir jemand erklären, warum dies geschieht und wie man es beheben.

+0

Hallo Brache diesen Link seine sehr hilfreiche Sie http://StackOverflow.com/Questions/26105803/center-nneteattachment-image-next-to-single-line-uilabel – Vinayak

Antwort

5

ich es endlich lösen, indem ein anderes Attribut Hinzufügen NSParagraphStyleAttributeName

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
    paragraphStyle.minimumLineHeight = lineheight; //line height equals to image height 
    [attributes setValue:paragraphStyle forKey:NSParagraphStyleAttributeName]; 

ändern sich nicht den Offset des Bildes, stellen nur den Text versetzt wird.

Verwandte Themen