Skip to content

Commit f8ebfb0

Browse files
committed
chore: refactoring
1 parent 05f5e3a commit f8ebfb0

File tree

4 files changed

+49
-40
lines changed

4 files changed

+49
-40
lines changed

plugin/platforms/ios/src/UIBezierPath+Text.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
#import "UIBezierPath+Elements.h"
1111
@interface UIBezierPath (TextUtilities)
1212
+ (UIBezierPath*) fromString: (NSString *) string withFont:(UIFont *) font;
13-
- (void) drawAttributedString: (NSAttributedString *) string;
14-
- (void) drawString: (NSString *) string withAttributes:(NSDictionary*)attributes;
13+
- (void) drawAttributedString: (NSAttributedString *) string withAlignment:(NSTextAlignment) alignment;
14+
- (void) drawString: (NSString *) string withAttributes:(NSDictionary*)attributes withAlignment:(NSTextAlignment) alignment;
1515
@end

plugin/platforms/ios/src/UIBezierPath+Text.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ + (UIBezierPath*) fromString: (NSString *) string withFont:(UIFont *) font {
1212
return BezierPathFromString(string, font);
1313
}
1414

15-
- (void) drawString: (NSString *) string withAttributes:(NSDictionary*)attributes {
16-
[self drawAttributedString:[[NSAttributedString alloc] initWithString:string attributes:attributes]];
15+
- (void) drawString: (NSString *) string withAttributes:(NSDictionary*)attributes withAlignment:(NSTextAlignment) alignment {
16+
[self drawAttributedString:[[NSAttributedString alloc] initWithString:string attributes:attributes] withAlignment:alignment];
1717
}
18-
- (void) drawAttributedString: (NSAttributedString *) string
18+
- (void) drawAttributedString: (NSAttributedString *) string withAlignment:(NSTextAlignment) alignment
1919
{
2020
if (!string) return;
2121
CGContextRef context = UIGraphicsGetCurrentContext();
@@ -45,7 +45,7 @@ - (void) drawAttributedString: (NSAttributedString *) string
4545
CGPoint targetPoint = [self pointAtPercent:percentConsumed withSlope:&slope];
4646

4747
// Accommodate the forward progress
48-
glyphDistance += bounding.size.width / 2;
48+
glyphDistance += bounding.size.width / 2;
4949
if (percentConsumed >= 1.0f) break;
5050

5151
// Calculate the rotation

src/canvas.ios.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ export * from './canvas.common';
1010

1111
const identity = CGAffineTransformIdentity;
1212

13+
function NSTextAlignmentFromAlign(align: Align) {
14+
if (align === Align.CENTER) {
15+
return NSTextAlignment.Center;
16+
} else if (align === Align.RIGHT) {
17+
return NSTextAlignment.Right;
18+
}
19+
return NSTextAlignment.Left;
20+
}
21+
1322
function DEGREES_TO_RADIANS(x) {
1423
return (Math.PI * x) / 180.0;
1524
}
@@ -1855,9 +1864,9 @@ export class Canvas implements ICanvas {
18551864
}
18561865
UIGraphicsPushContext(ctx);
18571866
if (text instanceof NSAttributedString) {
1858-
bPath.drawAttributedString(applyAttributesToNSAttributedString(text, paint.getDrawTextAttribs()));
1867+
bPath.drawAttributedStringWithAlignment(applyAttributesToNSAttributedString(text, paint.getDrawTextAttribs()), NSTextAlignmentFromAlign(paint.getTextAlign()));
18591868
} else {
1860-
bPath.drawStringWithAttributes(text, paint.getDrawTextAttribs());
1869+
bPath.drawStringWithAttributesWithAlignment(text, paint.getDrawTextAttribs(), NSTextAlignmentFromAlign(paint.getTextAlign()));
18611870
}
18621871
UIGraphicsPopContext();
18631872
}

src/typings/ios.d.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
declare namespace UIBezierPath {
2-
function fromStringWithFont(string: string, font: UIFont): UIBezierPath;
3-
function addLinesOffsetCountCloseToPath(points, offset, length, close, path)
4-
function addCubicLinesOffsetCountCloseToPath(points, offset, length, close, path)
2+
function fromStringWithFont(str: string, font: UIFont): UIBezierPath;
3+
function addLinesOffsetCountCloseToPath(points, offset, length, close, path);
4+
function addCubicLinesOffsetCountCloseToPath(points, offset, length, close, path);
55
}
66

77
declare namespace UIDrawingPath {
8-
function drawLineSegmentsCountInContextWithTransform(pts: any, count: number, context:any, _transform: CGAffineTransform)
8+
function drawLineSegmentsCountInContextWithTransform(pts: any, count: number, context: any, _transform: CGAffineTransform);
99
}
10-
declare namespace UIDrawingText{
11-
function drawStringXYFontColor(text: string, x: number, y:number, font: UIFont, color: UIColor)
12-
function drawAttributedStringXYFontColor(text: NSAttributedString, x: number, y:number, font: UIFont, color: UIColor)
13-
function drawAttributedStringInRectXYWidthHeightFontAlignmentColor(text: NSAttributedString, x: number, y:number, w: number, h:number, font: UIFont, alignment:NSTextAlignment, color: UIColor)
14-
15-
function drawStringXYWithAttributes(text: string, x: number, y:number, attributes: NSDictionary<any, any>)
16-
function drawAttributedStringXYWithAttributes(text: NSAttributedString, x: number, y:number, attributes: NSDictionary<any, any>)
17-
function getTextBoundsFromToAttributes(text: string, start: number, end: number,attributes:NSDictionary<any, any>): CGRect;
18-
function measureTextFromToAttributes(text: string, start: number, end: number,attributes:NSDictionary<any, any>): number;
10+
declare namespace UIDrawingText {
11+
function drawStringXYFontColor(text: string, x: number, y: number, font: UIFont, color: UIColor);
12+
function drawAttributedStringXYFontColor(text: NSAttributedString, x: number, y: number, font: UIFont, color: UIColor);
13+
function drawAttributedStringInRectXYWidthHeightFontAlignmentColor(text: NSAttributedString, x: number, y: number, w: number, h: number, font: UIFont, alignment: NSTextAlignment, color: UIColor);
14+
15+
function drawStringXYWithAttributes(text: string, x: number, y: number, attributes: NSDictionary<any, any>);
16+
function drawAttributedStringXYWithAttributes(text: NSAttributedString, x: number, y: number, attributes: NSDictionary<any, any>);
17+
function getTextBoundsFromToAttributes(text: string, start: number, end: number, attributes: NSDictionary<any, any>): CGRect;
18+
function measureTextFromToAttributes(text: string, start: number, end: number, attributes: NSDictionary<any, any>): number;
1919
}
2020

21+
// eslint-disable-next-line no-redeclare
2122
declare interface UIBezierPath {
22-
drawAttributedString(string: NSAttributedString);
23-
drawStringWithAttributes(string: string, attributes:NSDictionary<any, any>);
24-
addLinesCountClose(pts: any, count: number, close:boolean)
25-
addCubicLinesCountClose(pts: any, count: number, close:boolean)
23+
drawAttributedStringWithAlignment(str: NSAttributedString, alignment: NSTextAlignment);
24+
drawStringWithAttributesWithAlignment(str: string, attributes: NSDictionary<any, any>, alignment: NSTextAlignment);
25+
addLinesCountClose(pts: any, count: number, close: boolean);
26+
addCubicLinesCountClose(pts: any, count: number, close: boolean);
2627
}
2728

2829
declare function AddDashesToPath(path: UIBezierPath): void;
@@ -38,37 +39,36 @@ declare function AspectScaleFit(sourceSize: CGSize, destRect: CGRect): number;
3839
declare function BevelPath(p: UIBezierPath, color: UIColor, r: number, theta: number): void;
3940

4041
declare class BezierElement extends NSObject implements NSCopying {
42+
static alloc(): BezierElement; // inherited from NSObject
4143

42-
static alloc(): BezierElement; // inherited from NSObject
44+
static elementWithPathElement(element: CGPathElement): BezierElement;
4345

44-
static elementWithPathElement(element: CGPathElement): BezierElement;
46+
static new(): BezierElement; // inherited from NSObject
4547

46-
static new(): BezierElement; // inherited from NSObject
48+
controlPoint1: CGPoint;
4749

48-
controlPoint1: CGPoint;
50+
controlPoint2: CGPoint;
4951

50-
controlPoint2: CGPoint;
52+
elementType: CGPathElementType;
5153

52-
elementType: CGPathElementType;
54+
point: CGPoint;
5355

54-
point: CGPoint;
56+
readonly stringValue: string;
5557

56-
readonly stringValue: string;
58+
addToPath(path: UIBezierPath): void;
5759

58-
addToPath(path: UIBezierPath): void;
60+
copyWithZone(zone: interop.Pointer | interop.Reference<any>): any;
5961

60-
copyWithZone(zone: interop.Pointer | interop.Reference<any>): any;
62+
elementByApplyingBlock(block: (p1: CGPoint) => CGPoint): BezierElement;
6163

62-
elementByApplyingBlock(block: (p1: CGPoint) => CGPoint): BezierElement;
63-
64-
showTheCode(): void;
64+
showTheCode(): void;
6565
}
6666

6767
declare function BezierInflectedShape(numberOfInflections: number, percentInflection: number): UIBezierPath;
6868

69-
declare function BezierPathFromString(string: string, font: UIFont): UIBezierPath;
69+
declare function BezierPathFromString(str: string, font: UIFont): UIBezierPath;
7070

71-
declare function BezierPathFromStringWithFontFace(string: string, fontFace: string): UIBezierPath;
71+
declare function BezierPathFromStringWithFontFace(str: string, fontFace: string): UIBezierPath;
7272

7373
declare function BezierPathWithElements(elements: NSArray<any>): UIBezierPath;
7474

0 commit comments

Comments
 (0)