NSMutableAttributedString设置图片与文本的垂直位置

- (void)setZiValue:(NSInteger)zhi {

    NSDictionary *attributeDict = [NSDictionary dictionaryWithObjectsAndKeys:

                                   [UIFont fontByName:nil fontSize:28.f],NSFontAttributeName,

                                   [UIColor colorWithNumber:MCOLOR],NSForegroundColorAttributeName,nil];

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%ld ",(long)zhi] attributes:attributeDict];

    [string addAttributes:@{NSBaselineOffsetAttributeName:@(8)} range:(NSRange){0,string.length}];

    NSInteger length = string.length;

    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];

    textAttachment.image = [UIImage imageWithContentsOfFile:[DeviceInfo AppBaseResourcePath:@"[email protected]"]];

    NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];

    [string insertAttributedString:textAttachmentString atIndex:string.length];

    [string addAttributes:@{NSBaselineOffsetAttributeName:@(4)} range:(NSRange){length,textAttachmentString.string.length}];

    _priceLabel.attributedText = string;

}


你可能感兴趣的:(IOS)