RegexHighlightView icon indicating copy to clipboard operation
RegexHighlightView copied to clipboard

Linebreak mode in CoreText works differently when comparing to UITextView

Open cyndibaby905 opened this issue 12 years ago • 3 comments

Thanks for posting your RegexHighlightView! Really helpful. I read your code, found your are using CoreText to display the stylized content, and make the textColor in UITextView clear to make sure it won't draw any text, so we can reuse UITextView's other behavior such as cursor, text selection. But there is a problem(I think it should be a bug for CoreText): the linebreak mode in CoreText works a little different to UITextView. I think both CoreText and UITextView are using the same linebreak mode 'LineBreakByWordWrapping', but they define 'word' differently. Please see the attached image for example. screenshot So in some condition, the cursor seems 'off' the content. To resolve this issue, I think we have 3 approaches:

  1. Extend the UITextView, leverage the attributedString related properties(Only works for iOS 6):

    @property(nonatomic) BOOL allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0); // defaults to NO @property(nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); // default is nil @property(nonatomic,copy) NSDictionary *typingAttributes NS_AVAILABLE_IOS(6_0); // automatically resets when the selection changes

  2. Extend the UIView, use CoreText to display the content, and add a subView to display the cursor and the selected range.

  3. Set the linebreak mode to LineBreakByCharWrapping. However, the UITextView didn't expose any interfaces to allow us do that, we may have to use some private APIs.

cyndibaby905 avatar Feb 06 '13 08:02 cyndibaby905

As I would like to use RegexHighlightView in my project as well and I just discovered the same issue, I'd like to ask if you could explain you possible solutions... I don't get what you mean with possibility one.

Has anybody found a proper solution that might already be working?

georgbachmann avatar May 23 '13 14:05 georgbachmann

I have also noticed this when I used it in my project. I cannot seem to find what is wrong with it.

Andrew-Dev avatar Jun 01 '13 03:06 Andrew-Dev

I've found exactly the same issue - the wrapping is different and so when a line breaks you have unpredictable results - any thoughts about how we could fix this?

TwentyThreeDegrees avatar Jun 10 '13 18:06 TwentyThreeDegrees