Skip to content

Commit d60efb5

Browse files
author
JimmyDaddy
committed
fix: android type convert error and code opt
1 parent 70b9baf commit d60efb5

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

.release-it.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"git": {
3+
"commitMessage": "chore: release v${version}"
4+
},
5+
"github": {
6+
"release": true
7+
},
8+
"npm": {
9+
"publish": false
10+
},
11+
"plugins": {
12+
"@release-it/conventional-changelog": {
13+
"preset": "angular",
14+
"infile": "CHANGELOG.md"
15+
}
16+
}
17+
}

CHANGELOG.md

Whitespace-only changes.

android/src/main/java/com/jimmydaddy/imagemarker/ImageMarkerManager.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.graphics.Canvas;
1111
import android.graphics.Color;
1212
import android.graphics.Paint;
13+
import android.graphics.Rect;
1314
import android.graphics.Typeface;
1415
import android.text.Layout;
1516
import android.text.StaticLayout;
@@ -222,7 +223,6 @@ private void markImageByBitmap(
222223
System.gc();
223224
}
224225

225-
226226
// 保存
227227
// canvas.save(Canvas.ALL_SAVE_FLAG);
228228
canvas.save();
@@ -239,8 +239,6 @@ private void markImageByBitmap(
239239
promise.resolve("data:image/png;base64,".concat(result));
240240
} else {
241241
bos = new BufferedOutputStream(new FileOutputStream(dest));
242-
243-
// int quaility = (int) (100 / percent > 80 ? 80 : 100 / percent);
244242
icon.compress(getSaveFormat(opts.saveFormat), opts.quality, bos);
245243
bos.flush();
246244
bos.close();
@@ -328,9 +326,17 @@ private void markImageByText(
328326
textPaint.setTextSize(fSize);
329327
textPaint.setColor(Color.parseColor(transRGBColor(opts.color)));
330328

329+
StaticLayout textLayout;
331330
// ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE
332-
StaticLayout textLayout = new StaticLayout(opts.text, textPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
333-
331+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
332+
StaticLayout.Builder builder = StaticLayout.Builder.obtain(opts.text, 0, opts.text.length(), textPaint, canvas.getWidth());
333+
builder.setAlignment(Layout.Alignment.ALIGN_NORMAL);
334+
builder.setLineSpacing(0.0f, 1.0f);
335+
builder.setIncludePad(false);
336+
textLayout = builder.build();
337+
} else {
338+
textLayout = new StaticLayout(opts.text, textPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
339+
}
334340
int textHeight = textLayout.getHeight();
335341
int textWidth = 0;
336342
int count = textLayout.getLineCount();
@@ -352,6 +358,9 @@ private void markImageByText(
352358
}
353359
}
354360

361+
// textPaint.setUnderlineText(true);
362+
// textPaint.setTextSkewX(-0.25f);
363+
355364
float x = position.X;
356365
float y = position.Y;
357366

@@ -495,7 +504,7 @@ public void addTextByPosition(
495504
ReadableMap opts,
496505
final Promise promise
497506
) {
498-
MarkTextOptions markOpts = (MarkTextOptions) Options.checkParams(opts, promise);
507+
MarkTextOptions markOpts = MarkTextOptions.checkParams(opts, promise);
499508
if (null == markOpts) return;
500509
try {
501510

android/src/main/java/com/jimmydaddy/imagemarker/base/Position.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ static public Position getTextPosition(PositionEnum position, int width, int hei
6363
case CENTER:
6464
return new Position((width - textWidth) / 2, (height - textHeight) / 2);
6565
case BOTTOM_LEFT:
66-
return new Position(0, height - textHeight - margin);
66+
return new Position(margin, height - textHeight - margin);
6767
case BOTTOM_CENTER:
6868
return new Position((width - textWidth) / 2, height - textHeight);
69-
default:
69+
case BOTTOM_RIGHT:
7070
return new Position(width - textWidth - margin, height - textHeight - margin);
71+
default:
72+
return new Position(margin, margin);
7173
}
7274
}
7375

ios/RCTImageMarker/RCTImageMarker.m

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,18 @@ @implementation ImageMarker
6262
int w = image.size.width;
6363
int h = image.size.height;
6464

65-
UIGraphicsBeginImageContextWithOptions(image.size, NO, opts.scale);
66-
[image drawInRect:CGRectMake(0, 0, w, h)];
67-
NSDictionary *attr = @{
65+
NSDictionary *attributes = @{
6866
NSFontAttributeName: opts.font, //设置字体
6967
NSForegroundColorAttributeName : opts.color, //设置字体颜色
7068
NSShadowAttributeName : opts.shadow
7169
};
70+
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:opts.text attributes:attributes];
71+
CGSize maxSize = CGSizeMake(w, CGFLOAT_MAX); // 最大宽度和高度
72+
CGRect textRect = [attributedText boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
73+
CGSize size = textRect.size;
7274

73-
CGSize size = [opts.text sizeWithAttributes:attr];
75+
UIGraphicsBeginImageContextWithOptions(image.size, NO, opts.scale);
76+
[image drawInRect:CGRectMake(0, 0, w, h)];
7477
TextBackground* textBackground = opts.textBackground;
7578
if (textBackground != nil) {
7679
CGContextRef context = UIGraphicsGetCurrentContext();
@@ -87,7 +90,7 @@ @implementation ImageMarker
8790
CGRect rect = (CGRect){ CGPointMake(opts.X, opts.Y), size };
8891

8992
// CGRect position = CGRectMake(X, Y, w, h);
90-
[opts.text drawInRect:rect withAttributes:attr];
93+
[opts.text drawInRect:rect withAttributes:attributes];
9194
UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
9295
UIGraphicsEndImageContext();
9396
return aimg;

0 commit comments

Comments
 (0)