So you have this:
CFAttributedStringRef cfAttributedString = ...;
CTLineRef line = ...;
CFRange cfRange = CTLineGetStringRange(line);
Convert the CFRange
to an NSRange
and cast the CFAttributedStringRef
to an NSAttributedString *
:
NSRange nsRange = NSMakeRange(cfRange.location, cfRange.length);
NSAttributedString *richText = (__bridge NSAttributedString *)cfAttributedString;
Then you can use an Objective-C message to get the substring. If you want an attributed substring:
NSAttributedString *richSubtext = [richText attributedSubstringFromRange:nsRange];
If you want a plain substring:
NSString *substring = [richText.string substringWithRange:nsRange];
If you want to stick with Core Foundation functions for some reason (I wouldn't recommend it), you can get the attributed substring:
CFAttributedStringRef cfAttributedSubstring = CFAttributedStringCreateWithSubstring(
NULL, cfAttributedString, cfRange);
Or the plain substring like this:
CFStringRef cfString = CFAttributedStringGetString(cfAttributedString);
CFStringRef cfSubstring = CFStringCreateWithSubstring(NULL, cfString, cfRange);