On iOS 7 you can do what you want with a subclass of NSLayoutManager
that override -drawUnderlineForGlyphRange:underlineType:baselineOffset:lineFragmentRect:lineFragmentGlyphRange:containerOrigin:
A little sample that create text system objects, and draw the wanted underline.
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// setup text handling with our subclass of NSLayoutManager
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:@"Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do;"];
MyLayoutManager *textLayout = [[MyLayoutManager alloc] init];
[textStorage addLayoutManager:textLayout];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];
[textLayout addTextContainer:textContainer];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0,20,self.view.bounds.size.width,self.view.bounds.size.height-20)
textContainer:textContainer];
[self.view addSubview:textView];
// setup test attributes
UIFont *font = [UIFont fontWithName:@"TimesNewRomanPSMT" size:15];
NSMutableDictionary *attributesToSetDict = [NSMutableDictionary dictionary];
[attributesToSetDict setObject:font forKey:NSFontAttributeName];
[attributesToSetDict setObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName];
[attributesToSetDict setObject:@1 forKey:NSUnderlineStyleAttributeName];
[textView.textStorage setAttributes:attributesToSetDict range:NSMakeRange(9, 28)];
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
attributesToSetDict = [NSMutableDictionary dictionary];
[attributesToSetDict setObject:[UIFont fontWithDescriptor:fontDescriptor size:25] forKey:NSFontAttributeName];
[attributesToSetDict setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
[attributesToSetDict setObject:@1 forKey:NSUnderlineStyleAttributeName];
[textView.textStorage setAttributes:attributesToSetDict range:NSMakeRange(26, 7)];
}
@end
@implementation MyLayoutManager
- (void)drawUnderlineForGlyphRange:(NSRange)glyphRange underlineType:(NSUnderlineStyle)underlineVal baselineOffset:(CGFloat)baselineOffset lineFragmentRect:(CGRect)lineRect lineFragmentGlyphRange:(NSRange)lineGlyphRange containerOrigin:(CGPoint)containerOrigin
{
/* For the sample we consider that underlineVal is equals to NSUnderlineStyleSingle */
/*
* We need to create a CTFontRef as UIFont doesn't provided 'Underline Position' and 'Underline Thickness'
*/
// get current UIFont for the glyphRange
NSRange characterRange = [self characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
UIFont *font = [[self.textStorage attributesAtIndex:characterRange.location effectiveRange:NULL] objectForKey:NSFontAttributeName];
CTFontRef ctfont = CTFontCreateWithName((CFStringRef)[font fontName], [font pointSize], NULL);
CGPoint startLocation = [self locationForGlyphAtIndex:glyphRange.location];
CGPoint endLocation = [self locationForGlyphAtIndex:NSMaxRange(glyphRange)];
CGContextRef ctx = UIGraphicsGetCurrentContext();
// -underlineGlyphRange:underlineType:lineFragmentRect:lineFragmentGlyphRange:containerOrigin: already set color and line width
// for the current context
// reset line width with current font underline thickness
CGContextSetLineWidth(ctx, CTFontGetUnderlineThickness(ctfont));
CGContextMoveToPoint(ctx, startLocation.x + containerOrigin.x, startLocation.y + containerOrigin.y - CTFontGetUnderlinePosition(ctfont));
CGContextAddLineToPoint(ctx, endLocation.x + containerOrigin.x, endLocation.y + containerOrigin.y - CTFontGetUnderlinePosition(ctfont));
CGContextStrokePath(ctx);
CFRelease(ctfont);
}