Created
April 9, 2015 04:22
-
-
Save NghiaTranUIT/dc9dab12e42b02dcdbb7 to your computer and use it in GitHub Desktop.
Center Image and Text in NSAttributedString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is ratio?