Skip to content

Instantly share code, notes, and snippets.

@Daij-Djan
Created October 4, 2013 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daij-Djan/6819943 to your computer and use it in GitHub Desktop.
Save Daij-Djan/6819943 to your computer and use it in GitHub Desktop.
This is for DTCoreText. I fiddled some time with this.. I wanted to get a custom view (not html based) into a label. I found I had forgotten setting the CTRunDelegate :/ So it wasn't resized ok. now it works ;)
@implementation NSAttributedString (DTTextAttachment)
+ (instancetype)attributedStringWithTextAttachment:(DTTextAttachment*)attachment {
NSMutableDictionary *mAttributes = [NSMutableDictionary dictionary];
[mAttributes setObject:attachment forKey:NSAttachmentAttributeName];
#if DTCORETEXT_SUPPORT_NS_ATTRIBUTES && TARGET_OS_IPHONE
// need run delegate for sizing
CTRunDelegateRef embeddedObjectRunDelegate = createEmbeddedObjectRunDelegate(attachment);
[mAttributes setObject:CFBridgingRelease(embeddedObjectRunDelegate) forKey:(id)kCTRunDelegateAttributeName];
#endif
NSString *s = [NSString stringWithCharacters:(unichar*)NSAttachmentCharacter length:1];
return [[NSAttributedString alloc] initWithString:s attributes:mAttributes];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment