您的位置:首页 > 其它

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

2017-01-08 11:04 471 查看
- (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:@"DS_zi_icon@3x.png"]];

    NSAttributedString *textAttachmentString = [NSAttributedString
attributedStringWithAttachment:textAttachment];

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

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

    _priceLabel.attributedText = string;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  NSMutableAttributedS