I am currenlty working on an ios project where I need to have a text field that can have the placeholder wrapped in it whenever needed
The placeholder is being taken as a user input so at times the text can be long and can extend to a couple of lines.
I tried this UITextView.placeholderLabel.numberOfLines = 0; which doesnt seem to do the job.
Any good documentation would or references would be helpful too
Related
Android
In my app I need to visually style and highlight long texts that come from a DB. The texts are written as following:
Accept the <f>current</f> situation and <f>then</f> try to build a better one on top of it. Only then will you catch a break from all that getting away and will be able to think about how to actually improve.
<h>Resistance deepens the negative thoughts, acceptance releases them.</h>
On Android, I take these texts and parse them using Regex to replace the tags with something that works with the library. I'm using the SRML library to display it like this:
iOS
Now my question is: how do I reach a somewhat similar experience on iOS?
I'm using SwiftUI so far, but I can see that it's pretty limited in this specific regard. All I've found so far are libraries that helps you make it bold, italic or something other very minor:
https://github.com/kyle-n/HighlightedTextEditor
https://github.com/indragiek/MarkdownTextView
But I specifically need to highlight phrases, meaning color their background yellow. I then found this library, that offers some pretty complex functionality, but is written in Objective-C and I don't know how well that will work with my SwiftUI basis: https://github.com/ibireme/YYText
Is there any chance for me to get it working with SwiftUI components?
My best guess would be to do something like this (very basic pseudo code):
text = DB.getText
parser = StringParser(text)
content = ViewGroup()
while(parser.hasNextLine) { nextLine in
if(nextLine.contains("<h>")) {
var text = Text(nextLine)
.background(Color.yellow)
content.add(text)
} else if(...) {
...
}
// etc. etc.
}
And kind of build the resulting Text step by step from multiple pieces. But this looks kind of ugly to be honest. Any other ideas? And if not, how should I go about including that Cbjective-C library into a SwiftUI project?
With the help of a freelancer on Fiverr I solved it in the following way:
https://stackoverflow.com/a/69871902/1972372
Basically parsed the tags manually and kept track of the content types and order in an array, while keeping formatted Texts in another array. Then I could take that content and plant it into a ViewBuilder body function.
Unfortunately, SwiftUI doesn't let you change a Text's background color yet, without turning it into some View. So I had to make do with other, similar formattings for now.
I faced an issue with rendering Urdu on UITextView. I have used NSAttributedString to render text, but some characters are not appearing well using Jameel-Noori-Nastaleeq styled Regular and AlviLahoriNastaleeqfont. You can see in first picture how a character on the second line is not correctly rendered
Here is another reference screenshot, please see lines 5 and 13 where some characters are not rendered correctly.
What I have Tried
1-
There are some characters that do not appear well i.e. ["بڑ", "ئز", "یز", "ڈ", "ز"] I
tried to find all the occurrences of these characters using this tutorial [Find and Return the Ranges of All the Occurrences of a Given String in Swift][3]
Then change the font of all these occurrences to Jameel-Noori-Nastaleeq-Kasheeda because this little change will not impact overall looking, but finding all occurrences of a single character in a string does not work well in urdu, but only the case when we find the first occurrence this will work well using string.range(of: "ئز").
I have tested finding all occurrences of a substring in English that work well using the above tutorial method.
.
[3]: https://betterprogramming.pub/find-and-return-the-ranges-of-all-the-occurrences-of-a-given-string-in-swift-2a2015907a0e
2- Text is rendered well with Jameel-Noori-Nastaleeq-Kasheeda, but I have to use Jameel-Noori-Nastaleeq styled Regular
Is there any workaround that displays all characters well for Urdu in Jameel-Noori-Nastaleeq styled Regular.
Finally get solved, use UILabel instead of UITextView
Actually, UILabel renders Jameel Noori Nastaleeq without any issue but in the same context, UITextView causes issues.
According to my experience, issue is with UITextView compatibility with Jameel-Noori-Nastaleeq and AlviLahoriNastaleeq also Apple should see into this issue.
I'm developing an extension for Firefox which searches terms in a page. And I'd like to change found text highlight color and background. For example, I search for a letter "s" and by default it's selected with a blue rectangle with white text color. So I want to change the blue to the red.
How could I do this via JS?
Edit0:
To select a found text I use document.createRange() and selection.addRange() methods.
I don't know how the default finder selects a found term and applies background to it.
So maybe the 'range' method is not the best.
But I think I'm searching a way to highlight this created range...
Edit1:
Now I've partially resolved the color-changing preoblem. Just add a CSS rule with ::-moz-selection and red background when a text is found and selected. Then for document 'onmousedown' I remove this rule not to leave the default selection as red.
But a new problem is when I find say a digit and it gets a selection the background of that selection is gray (so it looks like a text selection of an inactive window). Then when I click with my mouse somewhere in the document text and press F3 the extension finds the next digit and selects it with the red background. And next findings work right (with red background).
So my purpose is change that initial gray background to red.
Maybe I should change the inactive selection color...
Edit2:
Now I updated my JS code:
var selection=w.getSelection()
var range=w.document.createRange()
range.setStart(foundNode,foundOffset)
range.setEnd(foundNode,foundOffset+foundLength)
selection.removeAllRanges()
selection.addRange(range)
var controller=gBrowser.docShell.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsISelectionDisplay)
.QueryInterface(Ci.nsISelectionController);
controller.setDisplaySelection(controller.SELECTION_ATTENTION)
controller.repaintSelection(controller.SELECTION_NORMAL)
Thanks to Noitidart's answer I found some information on how to use nsISelectionController XPCOM interface to select found text with background. Still I can't set a custom color for this background so that it be different from the default color of found text in Firefox. But setting the ui.textSelectBackgroundAttention preference in about:config to desired color will work with both my extension and default find engine.
I've found that SELECTION_ATTENTION constant is responsible for that background color and the setDisplaySelection method links the color to the selected text. But I couldn't find any implementation of this method. I saw only nsISelectionController idl file with its structure but no correspondent .cpp or .js file implementing this .idl. So I don't have information on how the color is set.
Edit3:
Recently I added the "Highlight All" functionality to my extension. And a new question about color of this highlight has rised. Using the above tecnique will show all the matches with green find color (by default). But it's more comfortable to use a different color to distinguish the current match and others.
So I couldn't find another helpful nsISelectionController constant for the "Highlight All" selection. I simply set this selection to 'DISABLED' type and changed the ui.textSelectBackgroundDisabled about:config pref. This pref is obviously for the selected text background of an inactive window. And it worked for me.
controller.setDisplaySelection(controller.SELECTION_DISABLED)
Another thing is that I'm not sure that the controller.repaintSelection() in the previous Edit is necessary. I guess the selection didn't work without it when I started my experiments with this stuff. But now I removed that line and all still work.
Plus:
And some additional links if somebody will need:
nsISelectionController Reference
Selection Reference
Forum question about highlight
about:config prefs for highlight
An Add-on using a similar tecnique
Finder.jsm and other sources
Also I used some files from Firefox source archive: Firefox 33 Source:
- nsISelectionController.idl [\content\base\public\]
- nsTypeAheadFind.cpp [\toolkit\components\typeaheadfind\]
- Finder.jsm [\toolkit\modules\]
- findbar.xml [\toolkit\content\widgets\]
I asked this question to quicksilver via email and this is what he told me:
You might find this one helpful: https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsISelectionController
I'm hardly the master you think I am, actually. :) To change those colors I just change the values for preferences:
ui.textHighlightBackground
ui.textHighlightForeground
ui.textSelectBackgroundAttention -> SELECTION_ATTENTION, it's not a highlight, it's a normal selection (as you would select some text with your mouse and it would turn the regular blue blackground, in windows at least) but it's given "attention", so it has the green background that the find operation reports. Basically it's a way of showing the user "Here I am!!" after firefox automatically selecs the text he searched for.
And I really don't know most of those contants, SELECTION_NORMAL is for normal text selection, like it would be when you select text with your mouse, SELECTION_FIND is for the highlights, and I only know the ON/HIDDEN/OFF/DISABLED ones which are self-explanatory. SELECTION_SPELLCHECK is probably for the auto-correct when you are typing in an editable content node, but I'm just guessing that one from the name.
Also, as far as I know, it's not possible to just create custom selection ranges/contants, as the code simply won't recognize them without editing the C++ code as well. Which is actually one of the reasons I haven't implemented https://github.com/Quicksaver/FindBar-Tweak/issues/76 yet.
I've got two UITextViews containing data that should be recongised by the data detection, however whilst one works fine on both device and simulator there's one that only works under Simulator. I've attempted trashing the build from my device, cleaning the product down, removing derived data and nothing seems to resolve the inconsistency.
Link detection was enabled within Interface Builder, the data is passed in with a NSString stringWithFormat: formatted string and set with UITextView setText:. Set the same way for both, so there's no difference there, but it just doesn't seem to work correctly for one of them.
EDIT: On the device if I tap on one of the items that should detect as a link, it'll then turn blue and do link detection. I'm not setting any custom fonts or colours that could have an impact.
It appears that the trick is to setScrollable:NO. Seems to fix the problem, although if you need scrolling, I'm not sure what the answer will be...
Apparently this issue is caused by how iOS is currently handling the UITextView links. It is creating an NSAttributedString that turns sections of the text blue ( when the view contains a link ). So I've figured out that this bug only occurs when a link is the first text in the AttributedString, i.e. the first text in the text view. So it's easily fixed by prepending an whitespace to your text before setting it. Or overriding setText to " " + text;
Hope this helps guys
I have the following code snippet for creating an input field for entering colors:
DataSourceTextField colorField =
new DataSourceTextField(ZoneDto.ATTR_COLOR, "*localized name*", 7, true);
colorField.setEditorType(new ColorPickerItem());
colorField.setPrompt("*localized instructions*");
This works quite well, since the input field has the localized instructions in its tooltip, but the small square that opens the color picker window has the original english tooltip ("Click to select a new color").
How could I change this message to a localized one?
Is this even possible to accomplish? I read that setEditorType only sets a template, from which instances are generated whenever needed. This means it's not going to work if I add setPrompt("localized instructions") to the ColorPickerItem given to the setEditorType().
Thanks in advance!
If i get you right, why not fill a variable 'localizedStringForColorPicker' with the current selected localization upon startup. And change it, when another localization is selected?