Parcourir la source

improved compatibility for iOS 7: #126

ibireme il y a 9 ans
Parent
commit
c4002c2699
1 fichiers modifiés avec 14 ajouts et 0 suppressions
  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];