Explorar o código

improved compatibility for iOS 7: #126

ibireme %!s(int64=9) %!d(string=hai) anos
pai
achega
c4002c2699
Modificáronse 1 ficheiros con 14 adicións e 0 borrados
  1. 14 0
      YYText/Component/YYTextLayout.m

+ 14 - 0
YYText/Component/YYTextLayout.m

@@ -2767,6 +2767,20 @@ static void YYTextDrawDecoration(YYTextLayout *layout, CGContextRef context, CGS
             if (glyphCount == 0) continue;
             
             NSDictionary *attrs = (id)CTRunGetAttributes(run);
+            
+            /*
+             Sometimes CoreText may convert CGColor to UIColor for `kCTForegroundColorAttributeName`
+             attribute in iOS7. This should be a bug of CoreText, and may cause crash. Here's a workaround.
+             */
+            NSObject *tmpColor = attrs[(id)kCTForegroundColorAttributeName];
+            if ([tmpColor respondsToSelector:@selector(CGColor)]) {
+                CGColorRef cgColor = ((UIColor *)tmpColor).CGColor;
+                if (!cgColor) cgColor = [UIColor blackColor].CGColor;
+                NSMutableDictionary *tmpAttrs = attrs.mutableCopy;
+                tmpAttrs[(id)kCTForegroundColorAttributeName] = (__bridge id)(cgColor);
+                attrs = tmpAttrs;
+            }
+            
             YYTextDecoration *underline = attrs[YYTextUnderlineAttributeName];
             YYTextDecoration *strikethrough = attrs[YYTextStrikethroughAttributeName];