RegexHighlightView
RegexHighlightView copied to clipboard
Linebreak mode in CoreText works differently when comparing to UITextView
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.
So in some condition, the cursor seems 'off' the content. To resolve this issue, I think we have 3 approaches:
-
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
-
Extend the UIView, use CoreText to display the content, and add a subView to display the cursor and the selected range.
-
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.
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?
I have also noticed this when I used it in my project. I cannot seem to find what is wrong with it.
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?