iOS 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.
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ó.
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