RCTBorderDrawing.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTBorderDrawing.h"
  8. #import "RCTLog.h"
  9. static const CGFloat RCTViewBorderThreshold = 0.001;
  10. BOOL RCTBorderInsetsAreEqual(UIEdgeInsets borderInsets)
  11. {
  12. return ABS(borderInsets.left - borderInsets.right) < RCTViewBorderThreshold &&
  13. ABS(borderInsets.left - borderInsets.bottom) < RCTViewBorderThreshold &&
  14. ABS(borderInsets.left - borderInsets.top) < RCTViewBorderThreshold;
  15. }
  16. BOOL RCTCornerRadiiAreEqual(RCTCornerRadii cornerRadii)
  17. {
  18. return ABS(cornerRadii.topLeft - cornerRadii.topRight) < RCTViewBorderThreshold &&
  19. ABS(cornerRadii.topLeft - cornerRadii.bottomLeft) < RCTViewBorderThreshold &&
  20. ABS(cornerRadii.topLeft - cornerRadii.bottomRight) < RCTViewBorderThreshold;
  21. }
  22. BOOL RCTBorderColorsAreEqual(RCTBorderColors borderColors)
  23. {
  24. return CGColorEqualToColor(borderColors.left, borderColors.right) &&
  25. CGColorEqualToColor(borderColors.left, borderColors.top) &&
  26. CGColorEqualToColor(borderColors.left, borderColors.bottom);
  27. }
  28. RCTCornerInsets RCTGetCornerInsets(RCTCornerRadii cornerRadii, UIEdgeInsets edgeInsets)
  29. {
  30. return (RCTCornerInsets){{
  31. MAX(0, cornerRadii.topLeft - edgeInsets.left),
  32. MAX(0, cornerRadii.topLeft - edgeInsets.top),
  33. },
  34. {
  35. MAX(0, cornerRadii.topRight - edgeInsets.right),
  36. MAX(0, cornerRadii.topRight - edgeInsets.top),
  37. },
  38. {
  39. MAX(0, cornerRadii.bottomLeft - edgeInsets.left),
  40. MAX(0, cornerRadii.bottomLeft - edgeInsets.bottom),
  41. },
  42. {
  43. MAX(0, cornerRadii.bottomRight - edgeInsets.right),
  44. MAX(0, cornerRadii.bottomRight - edgeInsets.bottom),
  45. }};
  46. }
  47. static UIEdgeInsets RCTRoundInsetsToPixel(UIEdgeInsets edgeInsets)
  48. {
  49. edgeInsets.top = RCTRoundPixelValue(edgeInsets.top);
  50. edgeInsets.bottom = RCTRoundPixelValue(edgeInsets.bottom);
  51. edgeInsets.left = RCTRoundPixelValue(edgeInsets.left);
  52. edgeInsets.right = RCTRoundPixelValue(edgeInsets.right);
  53. return edgeInsets;
  54. }
  55. static void RCTPathAddEllipticArc(
  56. CGMutablePathRef path,
  57. const CGAffineTransform *m,
  58. CGPoint origin,
  59. CGSize size,
  60. CGFloat startAngle,
  61. CGFloat endAngle,
  62. BOOL clockwise)
  63. {
  64. CGFloat xScale = 1, yScale = 1, radius = 0;
  65. if (size.width != 0) {
  66. xScale = 1;
  67. yScale = size.height / size.width;
  68. radius = size.width;
  69. } else if (size.height != 0) {
  70. xScale = size.width / size.height;
  71. yScale = 1;
  72. radius = size.height;
  73. }
  74. CGAffineTransform t = CGAffineTransformMakeTranslation(origin.x, origin.y);
  75. t = CGAffineTransformScale(t, xScale, yScale);
  76. if (m != NULL) {
  77. t = CGAffineTransformConcat(t, *m);
  78. }
  79. CGPathAddArc(path, &t, 0, 0, radius, startAngle, endAngle, clockwise);
  80. }
  81. CGPathRef RCTPathCreateWithRoundedRect(CGRect bounds, RCTCornerInsets cornerInsets, const CGAffineTransform *transform)
  82. {
  83. const CGFloat minX = CGRectGetMinX(bounds);
  84. const CGFloat minY = CGRectGetMinY(bounds);
  85. const CGFloat maxX = CGRectGetMaxX(bounds);
  86. const CGFloat maxY = CGRectGetMaxY(bounds);
  87. const CGSize topLeft = {
  88. MAX(0, MIN(cornerInsets.topLeft.width, bounds.size.width - cornerInsets.topRight.width)),
  89. MAX(0, MIN(cornerInsets.topLeft.height, bounds.size.height - cornerInsets.bottomLeft.height)),
  90. };
  91. const CGSize topRight = {
  92. MAX(0, MIN(cornerInsets.topRight.width, bounds.size.width - cornerInsets.topLeft.width)),
  93. MAX(0, MIN(cornerInsets.topRight.height, bounds.size.height - cornerInsets.bottomRight.height)),
  94. };
  95. const CGSize bottomLeft = {
  96. MAX(0, MIN(cornerInsets.bottomLeft.width, bounds.size.width - cornerInsets.bottomRight.width)),
  97. MAX(0, MIN(cornerInsets.bottomLeft.height, bounds.size.height - cornerInsets.topLeft.height)),
  98. };
  99. const CGSize bottomRight = {
  100. MAX(0, MIN(cornerInsets.bottomRight.width, bounds.size.width - cornerInsets.bottomLeft.width)),
  101. MAX(0, MIN(cornerInsets.bottomRight.height, bounds.size.height - cornerInsets.topRight.height)),
  102. };
  103. CGMutablePathRef path = CGPathCreateMutable();
  104. RCTPathAddEllipticArc(
  105. path, transform, (CGPoint){minX + topLeft.width, minY + topLeft.height}, topLeft, M_PI, 3 * M_PI_2, NO);
  106. RCTPathAddEllipticArc(
  107. path, transform, (CGPoint){maxX - topRight.width, minY + topRight.height}, topRight, 3 * M_PI_2, 0, NO);
  108. RCTPathAddEllipticArc(
  109. path, transform, (CGPoint){maxX - bottomRight.width, maxY - bottomRight.height}, bottomRight, 0, M_PI_2, NO);
  110. RCTPathAddEllipticArc(
  111. path, transform, (CGPoint){minX + bottomLeft.width, maxY - bottomLeft.height}, bottomLeft, M_PI_2, M_PI, NO);
  112. CGPathCloseSubpath(path);
  113. return path;
  114. }
  115. static void
  116. RCTEllipseGetIntersectionsWithLine(CGRect ellipseBounds, CGPoint lineStart, CGPoint lineEnd, CGPoint intersections[2])
  117. {
  118. const CGPoint ellipseCenter = {CGRectGetMidX(ellipseBounds), CGRectGetMidY(ellipseBounds)};
  119. lineStart.x -= ellipseCenter.x;
  120. lineStart.y -= ellipseCenter.y;
  121. lineEnd.x -= ellipseCenter.x;
  122. lineEnd.y -= ellipseCenter.y;
  123. const CGFloat m = (lineEnd.y - lineStart.y) / (lineEnd.x - lineStart.x);
  124. const CGFloat a = ellipseBounds.size.width / 2;
  125. const CGFloat b = ellipseBounds.size.height / 2;
  126. const CGFloat c = lineStart.y - m * lineStart.x;
  127. const CGFloat A = (b * b + a * a * m * m);
  128. const CGFloat B = 2 * a * a * c * m;
  129. const CGFloat D = sqrt((a * a * (b * b - c * c)) / A + pow(B / (2 * A), 2));
  130. const CGFloat x_ = -B / (2 * A);
  131. const CGFloat x1 = x_ + D;
  132. const CGFloat x2 = x_ - D;
  133. const CGFloat y1 = m * x1 + c;
  134. const CGFloat y2 = m * x2 + c;
  135. intersections[0] = (CGPoint){x1 + ellipseCenter.x, y1 + ellipseCenter.y};
  136. intersections[1] = (CGPoint){x2 + ellipseCenter.x, y2 + ellipseCenter.y};
  137. }
  138. NS_INLINE BOOL RCTCornerRadiiAreAboveThreshold(RCTCornerRadii cornerRadii)
  139. {
  140. return (
  141. cornerRadii.topLeft > RCTViewBorderThreshold || cornerRadii.topRight > RCTViewBorderThreshold ||
  142. cornerRadii.bottomLeft > RCTViewBorderThreshold || cornerRadii.bottomRight > RCTViewBorderThreshold);
  143. }
  144. static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCornerRadii cornerRadii)
  145. {
  146. if (drawToEdge) {
  147. return CGPathCreateWithRect(rect, NULL);
  148. }
  149. return RCTPathCreateWithRoundedRect(rect, RCTGetCornerInsets(cornerRadii, UIEdgeInsetsZero), NULL);
  150. }
  151. static CGContextRef
  152. RCTUIGraphicsBeginImageContext(CGSize size, CGColorRef backgroundColor, BOOL hasCornerRadii, BOOL drawToEdge)
  153. {
  154. const CGFloat alpha = CGColorGetAlpha(backgroundColor);
  155. const BOOL opaque = (drawToEdge || !hasCornerRadii) && alpha == 1.0;
  156. UIGraphicsBeginImageContextWithOptions(size, opaque, 0.0);
  157. return UIGraphicsGetCurrentContext();
  158. }
  159. static UIImage *RCTGetSolidBorderImage(
  160. RCTCornerRadii cornerRadii,
  161. CGSize viewSize,
  162. UIEdgeInsets borderInsets,
  163. RCTBorderColors borderColors,
  164. CGColorRef backgroundColor,
  165. BOOL drawToEdge)
  166. {
  167. const BOOL hasCornerRadii = RCTCornerRadiiAreAboveThreshold(cornerRadii);
  168. const RCTCornerInsets cornerInsets = RCTGetCornerInsets(cornerRadii, borderInsets);
  169. // Incorrect render for borders that are not proportional to device pixel: borders get stretched and become
  170. // significantly bigger than expected.
  171. // Rdar: http://www.openradar.me/15959788
  172. borderInsets = RCTRoundInsetsToPixel(borderInsets);
  173. const BOOL makeStretchable =
  174. (borderInsets.left + cornerInsets.topLeft.width + borderInsets.right + cornerInsets.bottomRight.width <=
  175. viewSize.width) &&
  176. (borderInsets.left + cornerInsets.bottomLeft.width + borderInsets.right + cornerInsets.topRight.width <=
  177. viewSize.width) &&
  178. (borderInsets.top + cornerInsets.topLeft.height + borderInsets.bottom + cornerInsets.bottomRight.height <=
  179. viewSize.height) &&
  180. (borderInsets.top + cornerInsets.topRight.height + borderInsets.bottom + cornerInsets.bottomLeft.height <=
  181. viewSize.height);
  182. UIEdgeInsets edgeInsets =
  183. (UIEdgeInsets){borderInsets.top + MAX(cornerInsets.topLeft.height, cornerInsets.topRight.height),
  184. borderInsets.left + MAX(cornerInsets.topLeft.width, cornerInsets.bottomLeft.width),
  185. borderInsets.bottom + MAX(cornerInsets.bottomLeft.height, cornerInsets.bottomRight.height),
  186. borderInsets.right + MAX(cornerInsets.bottomRight.width, cornerInsets.topRight.width)};
  187. if (hasCornerRadii) {
  188. // Asymmetrical edgeInsets cause strange artifacting on iOS 10 and earlier.
  189. edgeInsets = (UIEdgeInsets){
  190. MAX(edgeInsets.top, edgeInsets.bottom),
  191. MAX(edgeInsets.left, edgeInsets.right),
  192. MAX(edgeInsets.top, edgeInsets.bottom),
  193. MAX(edgeInsets.left, edgeInsets.right),
  194. };
  195. }
  196. const CGSize size = makeStretchable ? (CGSize){
  197. // 1pt for the middle stretchable area along each axis
  198. edgeInsets.left + 1 + edgeInsets.right,
  199. edgeInsets.top + 1 + edgeInsets.bottom
  200. } : viewSize;
  201. CGContextRef ctx = RCTUIGraphicsBeginImageContext(size, backgroundColor, hasCornerRadii, drawToEdge);
  202. const CGRect rect = {.size = size};
  203. CGPathRef path = RCTPathCreateOuterOutline(drawToEdge, rect, cornerRadii);
  204. if (backgroundColor) {
  205. CGContextSetFillColorWithColor(ctx, backgroundColor);
  206. CGContextAddPath(ctx, path);
  207. CGContextFillPath(ctx);
  208. }
  209. CGContextAddPath(ctx, path);
  210. CGPathRelease(path);
  211. CGPathRef insetPath = RCTPathCreateWithRoundedRect(UIEdgeInsetsInsetRect(rect, borderInsets), cornerInsets, NULL);
  212. CGContextAddPath(ctx, insetPath);
  213. CGContextEOClip(ctx);
  214. BOOL hasEqualColors = RCTBorderColorsAreEqual(borderColors);
  215. if ((drawToEdge || !hasCornerRadii) && hasEqualColors) {
  216. CGContextSetFillColorWithColor(ctx, borderColors.left);
  217. CGContextAddRect(ctx, rect);
  218. CGContextAddPath(ctx, insetPath);
  219. CGContextEOFillPath(ctx);
  220. } else {
  221. CGPoint topLeft = (CGPoint){borderInsets.left, borderInsets.top};
  222. if (cornerInsets.topLeft.width > 0 && cornerInsets.topLeft.height > 0) {
  223. CGPoint points[2];
  224. RCTEllipseGetIntersectionsWithLine(
  225. (CGRect){topLeft, {2 * cornerInsets.topLeft.width, 2 * cornerInsets.topLeft.height}},
  226. CGPointZero,
  227. topLeft,
  228. points);
  229. if (!isnan(points[1].x) && !isnan(points[1].y)) {
  230. topLeft = points[1];
  231. }
  232. }
  233. CGPoint bottomLeft = (CGPoint){borderInsets.left, size.height - borderInsets.bottom};
  234. if (cornerInsets.bottomLeft.width > 0 && cornerInsets.bottomLeft.height > 0) {
  235. CGPoint points[2];
  236. RCTEllipseGetIntersectionsWithLine(
  237. (CGRect){{bottomLeft.x, bottomLeft.y - 2 * cornerInsets.bottomLeft.height},
  238. {2 * cornerInsets.bottomLeft.width, 2 * cornerInsets.bottomLeft.height}},
  239. (CGPoint){0, size.height},
  240. bottomLeft,
  241. points);
  242. if (!isnan(points[1].x) && !isnan(points[1].y)) {
  243. bottomLeft = points[1];
  244. }
  245. }
  246. CGPoint topRight = (CGPoint){size.width - borderInsets.right, borderInsets.top};
  247. if (cornerInsets.topRight.width > 0 && cornerInsets.topRight.height > 0) {
  248. CGPoint points[2];
  249. RCTEllipseGetIntersectionsWithLine(
  250. (CGRect){{topRight.x - 2 * cornerInsets.topRight.width, topRight.y},
  251. {2 * cornerInsets.topRight.width, 2 * cornerInsets.topRight.height}},
  252. (CGPoint){size.width, 0},
  253. topRight,
  254. points);
  255. if (!isnan(points[0].x) && !isnan(points[0].y)) {
  256. topRight = points[0];
  257. }
  258. }
  259. CGPoint bottomRight = (CGPoint){size.width - borderInsets.right, size.height - borderInsets.bottom};
  260. if (cornerInsets.bottomRight.width > 0 && cornerInsets.bottomRight.height > 0) {
  261. CGPoint points[2];
  262. RCTEllipseGetIntersectionsWithLine(
  263. (CGRect){
  264. {bottomRight.x - 2 * cornerInsets.bottomRight.width, bottomRight.y - 2 * cornerInsets.bottomRight.height},
  265. {2 * cornerInsets.bottomRight.width, 2 * cornerInsets.bottomRight.height}},
  266. (CGPoint){size.width, size.height},
  267. bottomRight,
  268. points);
  269. if (!isnan(points[0].x) && !isnan(points[0].y)) {
  270. bottomRight = points[0];
  271. }
  272. }
  273. CGColorRef currentColor = NULL;
  274. // RIGHT
  275. if (borderInsets.right > 0) {
  276. const CGPoint points[] = {
  277. (CGPoint){size.width, 0},
  278. topRight,
  279. bottomRight,
  280. (CGPoint){size.width, size.height},
  281. };
  282. currentColor = borderColors.right;
  283. CGContextAddLines(ctx, points, sizeof(points) / sizeof(*points));
  284. }
  285. // BOTTOM
  286. if (borderInsets.bottom > 0) {
  287. const CGPoint points[] = {
  288. (CGPoint){0, size.height},
  289. bottomLeft,
  290. bottomRight,
  291. (CGPoint){size.width, size.height},
  292. };
  293. if (!CGColorEqualToColor(currentColor, borderColors.bottom)) {
  294. CGContextSetFillColorWithColor(ctx, currentColor);
  295. CGContextFillPath(ctx);
  296. currentColor = borderColors.bottom;
  297. }
  298. CGContextAddLines(ctx, points, sizeof(points) / sizeof(*points));
  299. }
  300. // LEFT
  301. if (borderInsets.left > 0) {
  302. const CGPoint points[] = {
  303. CGPointZero,
  304. topLeft,
  305. bottomLeft,
  306. (CGPoint){0, size.height},
  307. };
  308. if (!CGColorEqualToColor(currentColor, borderColors.left)) {
  309. CGContextSetFillColorWithColor(ctx, currentColor);
  310. CGContextFillPath(ctx);
  311. currentColor = borderColors.left;
  312. }
  313. CGContextAddLines(ctx, points, sizeof(points) / sizeof(*points));
  314. }
  315. // TOP
  316. if (borderInsets.top > 0) {
  317. const CGPoint points[] = {
  318. CGPointZero,
  319. topLeft,
  320. topRight,
  321. (CGPoint){size.width, 0},
  322. };
  323. if (!CGColorEqualToColor(currentColor, borderColors.top)) {
  324. CGContextSetFillColorWithColor(ctx, currentColor);
  325. CGContextFillPath(ctx);
  326. currentColor = borderColors.top;
  327. }
  328. CGContextAddLines(ctx, points, sizeof(points) / sizeof(*points));
  329. }
  330. CGContextSetFillColorWithColor(ctx, currentColor);
  331. CGContextFillPath(ctx);
  332. }
  333. CGPathRelease(insetPath);
  334. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  335. UIGraphicsEndImageContext();
  336. if (makeStretchable) {
  337. image = [image resizableImageWithCapInsets:edgeInsets];
  338. }
  339. return image;
  340. }
  341. // Currently, the dashed / dotted implementation only supports a single colour +
  342. // single width, as that's currently required and supported on Android.
  343. //
  344. // Supporting individual widths + colours on each side is possible by modifying
  345. // the current implementation. The idea is that we will draw four different lines
  346. // and clip appropriately for each side (might require adjustment of phase so that
  347. // they line up but even browsers don't do a good job at that).
  348. //
  349. // Firstly, create two paths for the outer and inner paths. The inner path is
  350. // generated exactly the same way as the outer, just given an inset rect, derived
  351. // from the insets on each side. Then clip using the odd-even rule
  352. // (CGContextEOClip()). This will give us a nice rounded (possibly) clip mask.
  353. //
  354. // +----------------------------------+
  355. // |@@@@@@@@ Clipped Space @@@@@@@@@|
  356. // |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
  357. // |@@+----------------------+@@@@@@@@|
  358. // |@@| |@@@@@@@@|
  359. // |@@| |@@@@@@@@|
  360. // |@@| |@@@@@@@@|
  361. // |@@+----------------------+@@@@@@@@|
  362. // |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
  363. // +----------------------------------+
  364. //
  365. // Afterwards, we create a clip path for each border side (CGContextSaveGState()
  366. // and CGContextRestoreGState() when drawing each side). The clip mask for each
  367. // segment is a trapezoid connecting corresponding edges of the inner and outer
  368. // rects. For example, in the case of the top edge, the points would be:
  369. // - (MinX(outer), MinY(outer))
  370. // - (MaxX(outer), MinY(outer))
  371. // - (MinX(inner) + topLeftRadius, MinY(inner) + topLeftRadius)
  372. // - (MaxX(inner) - topRightRadius, MinY(inner) + topRightRadius)
  373. //
  374. // +------------------+
  375. // |\ /|
  376. // | \ / |
  377. // | \ top / |
  378. // | \ / |
  379. // | \ / |
  380. // | +------+ |
  381. // | | | |
  382. // | | | |
  383. // | | | |
  384. // |left | |right|
  385. // | | | |
  386. // | | | |
  387. // | +------+ |
  388. // | / \ |
  389. // | / \ |
  390. // | / \ |
  391. // | / bottom \ |
  392. // |/ \|
  393. // +------------------+
  394. //
  395. //
  396. // Note that this approach will produce discontinuous colour changes at the edge
  397. // (which is okay). The reason is that Quartz does not currently support drawing
  398. // of gradients _along_ a path (NB: clipping a path and drawing a linear gradient
  399. // is _not_ equivalent).
  400. static UIImage *RCTGetDashedOrDottedBorderImage(
  401. RCTBorderStyle borderStyle,
  402. RCTCornerRadii cornerRadii,
  403. CGSize viewSize,
  404. UIEdgeInsets borderInsets,
  405. RCTBorderColors borderColors,
  406. CGColorRef backgroundColor,
  407. BOOL drawToEdge)
  408. {
  409. NSCParameterAssert(borderStyle == RCTBorderStyleDashed || borderStyle == RCTBorderStyleDotted);
  410. if (!RCTBorderColorsAreEqual(borderColors) || !RCTBorderInsetsAreEqual(borderInsets)) {
  411. RCTLogWarn(@"Unsupported dashed / dotted border style");
  412. return nil;
  413. }
  414. const CGFloat lineWidth = borderInsets.top;
  415. if (lineWidth <= 0.0) {
  416. return nil;
  417. }
  418. const BOOL hasCornerRadii = RCTCornerRadiiAreAboveThreshold(cornerRadii);
  419. CGContextRef ctx = RCTUIGraphicsBeginImageContext(viewSize, backgroundColor, hasCornerRadii, drawToEdge);
  420. const CGRect rect = {.size = viewSize};
  421. if (backgroundColor) {
  422. CGPathRef outerPath = RCTPathCreateOuterOutline(drawToEdge, rect, cornerRadii);
  423. CGContextAddPath(ctx, outerPath);
  424. CGPathRelease(outerPath);
  425. CGContextSetFillColorWithColor(ctx, backgroundColor);
  426. CGContextFillPath(ctx);
  427. }
  428. // Stroking means that the width is divided in half and grows in both directions
  429. // perpendicular to the path, that's why we inset by half the width, so that it
  430. // reaches the edge of the rect.
  431. CGRect pathRect = CGRectInset(rect, lineWidth / 2.0, lineWidth / 2.0);
  432. CGPathRef path = RCTPathCreateWithRoundedRect(pathRect, RCTGetCornerInsets(cornerRadii, UIEdgeInsetsZero), NULL);
  433. CGFloat dashLengths[2];
  434. dashLengths[0] = dashLengths[1] = (borderStyle == RCTBorderStyleDashed ? 3 : 1) * lineWidth;
  435. CGContextSetLineWidth(ctx, lineWidth);
  436. CGContextSetLineDash(ctx, 0, dashLengths, sizeof(dashLengths) / sizeof(*dashLengths));
  437. CGContextSetStrokeColorWithColor(ctx, [UIColor yellowColor].CGColor);
  438. CGContextAddPath(ctx, path);
  439. CGContextSetStrokeColorWithColor(ctx, borderColors.top);
  440. CGContextStrokePath(ctx);
  441. CGPathRelease(path);
  442. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  443. UIGraphicsEndImageContext();
  444. return image;
  445. }
  446. UIImage *RCTGetBorderImage(
  447. RCTBorderStyle borderStyle,
  448. CGSize viewSize,
  449. RCTCornerRadii cornerRadii,
  450. UIEdgeInsets borderInsets,
  451. RCTBorderColors borderColors,
  452. CGColorRef backgroundColor,
  453. BOOL drawToEdge)
  454. {
  455. switch (borderStyle) {
  456. case RCTBorderStyleSolid:
  457. return RCTGetSolidBorderImage(cornerRadii, viewSize, borderInsets, borderColors, backgroundColor, drawToEdge);
  458. case RCTBorderStyleDashed:
  459. case RCTBorderStyleDotted:
  460. return RCTGetDashedOrDottedBorderImage(
  461. borderStyle, cornerRadii, viewSize, borderInsets, borderColors, backgroundColor, drawToEdge);
  462. case RCTBorderStyleUnset:
  463. break;
  464. }
  465. return nil;
  466. }