Skip to content

Instantly share code, notes, and snippets.

@NghiaTranUIT
Created April 9, 2015 04:22
Show Gist options
  • Save NghiaTranUIT/dc9dab12e42b02dcdbb7 to your computer and use it in GitHub Desktop.
Save NghiaTranUIT/dc9dab12e42b02dcdbb7 to your computer and use it in GitHub Desktop.
Center Image and Text in NSAttributedString
// Create text attachment
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:nameicon];
textAttachment.bounds = CGRectMake(0, 0, 20, 16);
// Attribute
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithAttributedString:attrStringWithImage];
// Change base line
[str addAttribute:NSBaselineOffsetAttributeName value:@(-1.5f * ratio) range:NSMakeRange(0, attrStringWithImage.length)];
NSRange rangeImage = [fullText rangeOfString:@"#image#"];
[attributedText replaceCharactersInRange:rangeImage withAttributedString:str];
@aalenliang
Copy link

what is ratio?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment