From 41108de5d6d9afe3b4353c9daebdee0b9c8cd1fc Mon Sep 17 00:00:00 2001 From: Yoshikazu Oda Date: Fri, 1 May 2015 11:25:05 +0900 Subject: [PATCH] making it able to set NSBackgroundColorAttributeName attribute on text. --- Lib/SETextView.m | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Lib/SETextView.m b/Lib/SETextView.m index 2e6e644..61bc3ac 100644 --- a/Lib/SETextView.m +++ b/Lib/SETextView.m @@ -671,6 +671,37 @@ - (void)drawTextDecorations } #endif +#if TARGET_OS_IPHONE +- (void)drawTextBackgroundColorRect +{ + [self.attributedText enumerateAttribute:NSBackgroundColorAttributeName inRange:NSMakeRange(0, self.attributedText.length) options:kNilOptions usingBlock:^(id value, NSRange range, BOOL *stop) { + if (!value) { + return; + } + + for (SELineLayout *lineLayout in self.textLayout.lineLayouts) { + CGRect rect = [lineLayout rectOfStringWithRange:range]; + if (!CGRectIsEmpty(rect)) { + UIColor *color = (UIColor *)value; + if (![color isKindOfClass: [UIColor class]]) { + return; + } + UIBezierPath *path = [UIBezierPath bezierPath]; + + [path moveToPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect))]; + [path addLineToPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect))]; + + path.lineWidth = rect.size.height; + [color setStroke]; + + [path stroke]; + } + } + }]; +} +#endif + + - (void)drawTextAttachmentsInContext:(CGContextRef)context { NSMutableSet *attachmentsToLeave = [[NSMutableSet alloc] init]; @@ -934,6 +965,10 @@ - (void)drawRect:(CGRect)dirtyRect [self drawTextDecorations]; #endif +#if TARGET_OS_IPHONE + [self drawTextBackgroundColorRect]; +#endif + [self drawTextAttachmentsInContext:context]; #if TARGET_OS_IPHONE