2014-09-11 38 views
7

beforeiOS Tại sao NSTextAttachment biến mất khi thiết lập NSBaselineOffsetAttributeName?

Tôi muốn align NSTextAttachment đến trung tâm của văn bản, vì vậy tôi đặt NSBaselineOffsetAttributeName để thay đổi cơ bản về NSTextAttachment.

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]; 

Sau đó, tôi tính kích thước cho UILabel và đặt attributedText.

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]; 

Cuối cùng hình ảnh cuối cùng biến mất.

after

bất cứ ai có thể giải thích lý do tại sao điều này xảy ra và làm thế nào để sửa chữa nó.

+0

Xin chào liên kết này rất hữu ích cho bạn http://stackoverflow.com/questions/26105803/center-nstextattachment-image-next-to-single-line-uilabel – Vinayak

Trả lời

5

cuối cùng tôi giải quyết nó bằng cách thêm một thuộc tính NSParagraphStyleAttributeName

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

không thay đổi của hình ảnh bù đắp, chỉ cần thiết lập các văn bản bù đắp.

Các vấn đề liên quan