react-native-highlight-text-view 0.1.27 → 0.1.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -206,35 +206,40 @@ class HighlightTextView : AppCompatEditText {
206
206
  charPaddingBottom = bottom
207
207
  updateViewPadding()
208
208
  applyLineHeightAndSpacing()
209
- invalidate()
209
+ requestLayout()
210
+ post { invalidate() }
210
211
  }
211
212
 
212
213
  fun setCharPaddingLeft(padding: Float) {
213
214
  charPaddingLeft = padding
214
215
  updateViewPadding()
215
216
  applyLineHeightAndSpacing()
216
- invalidate()
217
+ requestLayout()
218
+ post { invalidate() }
217
219
  }
218
220
 
219
221
  fun setCharPaddingRight(padding: Float) {
220
222
  charPaddingRight = padding
221
223
  updateViewPadding()
222
224
  applyLineHeightAndSpacing()
223
- invalidate()
225
+ requestLayout()
226
+ post { invalidate() }
224
227
  }
225
228
 
226
229
  fun setCharPaddingTop(padding: Float) {
227
230
  charPaddingTop = padding
228
231
  updateViewPadding()
229
232
  applyLineHeightAndSpacing()
230
- invalidate()
233
+ requestLayout()
234
+ post { invalidate() }
231
235
  }
232
236
 
233
237
  fun setCharPaddingBottom(padding: Float) {
234
238
  charPaddingBottom = padding
235
239
  updateViewPadding()
236
240
  applyLineHeightAndSpacing()
237
- invalidate()
241
+ requestLayout()
242
+ post { invalidate() }
238
243
  }
239
244
 
240
245
  private fun updateViewPadding() {
@@ -315,7 +320,10 @@ class HighlightTextView : AppCompatEditText {
315
320
 
316
321
  this.typeface = typeface
317
322
  applyLineHeightAndSpacing()
318
- invalidate()
323
+ // Request layout to ensure proper measurement with new font before redrawing
324
+ requestLayout()
325
+ // Post invalidate to ensure layout is complete before drawing
326
+ post { invalidate() }
319
327
  }
320
328
 
321
329
  fun setVerticalAlign(align: String?) {
@@ -359,7 +367,8 @@ class HighlightTextView : AppCompatEditText {
359
367
  fun setCustomLineHeight(lineHeight: Float) {
360
368
  customLineHeight = lineHeight
361
369
  applyLineHeightAndSpacing()
362
- invalidate()
370
+ requestLayout()
371
+ post { invalidate() }
363
372
  }
364
373
 
365
374
  fun setLetterSpacingProp(points: Float) {
@@ -372,6 +381,8 @@ class HighlightTextView : AppCompatEditText {
372
381
  super.setTextSize(unit, size)
373
382
  applyLineHeightAndSpacing()
374
383
  applyLetterSpacing()
384
+ requestLayout()
385
+ post { invalidate() }
375
386
  }
376
387
 
377
388
  fun setTextProp(newText: String) {
@@ -34,12 +34,28 @@ using namespace facebook::react;
34
34
  continue;
35
35
  }
36
36
 
37
- CGRect boundingRect = [self boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];
37
+ // Get the glyph's position and line metrics
38
+ CGPoint glyphLocation = [self locationForGlyphAtIndex:glyphRange.location];
39
+ NSUInteger lineIndex = [self lineFragmentRectForGlyphAtIndex:glyphRange.location effectiveRange:NULL].origin.y;
40
+ CGRect lineRect = [self lineFragmentRectForGlyphAtIndex:glyphRange.location effectiveRange:NULL];
38
41
 
39
- // Ensure the glyph has reasonable dimensions and isn't a line fragment artifact
40
- // Also check that it's not spanning the full container width (which indicates line wrapping issues)
41
- if (boundingRect.size.width > 1.0 && boundingRect.size.height > 1.0 &&
42
- boundingRect.size.width < (textContainer.size.width * 0.8)) {
42
+ // Calculate precise character width using the font's attributes
43
+ // This avoids the extra spacing that boundingRectForGlyphRange includes with lineHeight
44
+ NSDictionary *attributes = [textStorage attributesAtIndex:i effectiveRange:NULL];
45
+ UIFont *font = attributes[NSFontAttributeName];
46
+ NSString *charString = [textStorage.string substringWithRange:NSMakeRange(i, 1)];
47
+ CGSize charSize = [charString sizeWithAttributes:@{NSFontAttributeName: font}];
48
+
49
+ // Build the character rect with precise width
50
+ CGRect boundingRect = CGRectMake(
51
+ glyphLocation.x,
52
+ lineRect.origin.y,
53
+ charSize.width,
54
+ lineRect.size.height
55
+ );
56
+
57
+ // Ensure the glyph has reasonable dimensions
58
+ if (boundingRect.size.width > 1.0 && boundingRect.size.height > 1.0) {
43
59
 
44
60
  // Apply background insets first (shrinks from line box)
45
61
  boundingRect.origin.y += self.backgroundInsetTop;
@@ -439,7 +455,6 @@ using namespace facebook::react;
439
455
  if (newViewProps.autoFocus && _textView.isEditable) {
440
456
  dispatch_async(dispatch_get_main_queue(), ^{
441
457
  [self->_textView becomeFirstResponder];
442
- // Move cursor to the end of the text
443
458
  NSUInteger textLength = self->_textView.text.length;
444
459
  self->_textView.selectedRange = NSMakeRange(textLength, 0);
445
460
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-highlight-text-view",
3
- "version": "0.1.27",
3
+ "version": "0.1.29",
4
4
  "description": "A native text input for React Native that supports inline text highlighting",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -18,7 +18,7 @@
18
18
  "android",
19
19
  "ios",
20
20
  "cpp",
21
- "*.podspec",
21
+ "*.podspec",
22
22
  "react-native.config.js",
23
23
  "!ios/build",
24
24
  "!android/build",