I would like to change the color only of particular substrings (keywords) while the user is typing in a UITextField.
In the documentation I saw the property textColor but it does change the color of the entire text string, while I would like to highlight only some keywords.
Is it possible?
The short answer is no.
It's possible if you create your own textfield from scratch using NSAttributedStrings and CATextLayers or Core Text, but this is an incredibly complex and difficult problem.
If you are mirroring what the user types in a preview box of some sort it is possible to create multiple labels or text fields that stay next to each other and have different colors or fonts or whatever. Otherwise, I'm afraid Nick's answer is accurate - within a single textField it ain't happening.
Good luck,
Damien
Related
I want to show some texts in my application. There are some articles in two languages. For each article, odd paragraphs are comments in first language and even paragraphs are translated comments. Actually it is one of the limits for the specific app that both languages should be available in the same page. Language A should be red and language B should be with blue color.
Is it possible for me to use one UITextView and set different colors for different paragraphs?
I have seen a few topics about text views with multiple colors but none of them were clear enough and I could not find any other helpful posts.
Is it possible for me to use one UITextView and set different colors for different paragraphs
Yes, it is perfectly possible. Here's some sample code of mine:
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch10p503attributedString/ch23p771attributedStringInLabel/ViewController.m
In that code (set which to 4, I think), we create two paragraphs with two different styles and two different colors, and draw them in a label and a text view.
Attributed text is what you're looking for, see here: http://www.ioscreator.com/tutorials/format-text-in-ios6-attributed-strings
NSAttributedStrings are the tool for the job. They are strings that are specially designed for your user interface, and with that come some special capabilities. You can access the attributed string of your UITextView through its attributedText property.
NSAttributedString *attStr = myTextView.attributedText;
Once you have acess to this, you can start messing around with the colors. To do this, you have to apply a special attribute to a certain range.
[myTextView.attributedText addAttribute:NSForegroundColorAttributeName
value:<firstParagraphColor>
range:<firstParagraphRange>];
[myTextView.attributedText addAttribute:NSForegroundColorAttributeName
value:<secondParagraphColor>
range:<secondParagraphRange>];
I'm currently trying to learn all I can about Cocoa's text "architecture." I've painstakingly learned all about the NSFontDescriptor, NSTextTab, and NSTextView classes, and I think I'm really understanding how it all works. However, there's one thing I can't seem to figure out: NSNaturalTextAlignment. I mean, I get it. I understand that when the font's "script" is RTL, the natural alignment is right, and when it's LTR, the natural alignment is left. All of that makes sense. However, how does one determine the natural direction of the font's script? I ask because of how my little app is set up. I currently have a mini formatting bar with the usual suspects:
NSPopUpButton to choose a font family
NSPopUpButton to choose a typeface
ACustomComboBox to choose a font size
NSSegmentedControl with segments for B, I, U, etc.
After those elements, I have my alignment control, which is also an NSSegmentedControl. In it, I have four options: left, centered, right, and justified. Again, pretty basic stuff. The neat thing about the alignment control is that I've set the tag for each segment to correspond to an NSTextAlignment value. Thus, when the "left" segment is pressed, the selected text aligns to the left. The same thing is true for "centered," "right," and "justified."
The problem is that a lot of the time an attributed string's NSParagraphStyle will return an NSNaturalTextAlignment value.
The documentation states:
The returned value is one of the alignments described in “NSTextAlignment.”
Text using NSNaturalTextAlignment is actually displayed using one of the other alignments, depending on the natural alignment of the text’s script.
Well, I'm glad Apple knows the natural alignment of the text's script, but how I do I find that out? I've currently implemented a really bad solution, but I honestly can't figure out any way around it. The bad solution looks like this:
- (void)updateTextAlignmentDisplayWithParagraphStyle:(NSParagraphStyle *)pStyle
{
NSTextAlignment alignment = pStyle.alignment;
if ( alignment == NSNaturalTextAlignment ) {
// This seems like a horrible way to determine the correct alignment ...
BOOL isRTL = ( self.textEditor.baseWritingDirection == NSWritingDirectionRightToLeft );
if ( isRTL ) {
alignment = NSRightTextAlignment;
} else {
alignment = NSLeftTextAlignment;
}
}
[self.alignmentControl selectSegmentWithTag:alignment];
}
The text view's "base" direction doesn't really tell me anything about the direction of the font itself, but at least it hints at what direction the font might go. I don't know. I just have a feeling there's a super secret NSFontDescriptor attribute which has this information, but I haven't found it. Cocoa gurus, any advice? Thanks in advance!
In the file NSText.h defines
NSNaturalTextAlignment = 4 // Indicates the default alignment for script
I think script is meant in the sense of written text.
You could change your code by using switch (pStyle.alignment or self.textEditor.baseWritingDirection) and add case: for every alignment.
I'm not an expert, but I think, that's what you need to know, to go forward. The info about the alignment maybe part of the Font, but this is speculative! It's also possible, that it is taken from user setting default language for the keyboard. NSLocale in the documentation or Internet might allow to go deeper in this theme.
I am trying to put a background image into a text-field (it's just a search icon). And it is tiling the image in the text field. Is it possible to turn off the repeating of the image? i don't see anything like that, i'm seeing padding and position for background images but nothing to turn off the tiling of the image. is this not possible in pixate yet?
thanks!
You should be able to set background-size to the size of your text-field so it won't have anything to repeat.
I battled this same problem, and in the end, I found I got what I wanted by using multiple objects. Even if you get the search icon to work right, you're probably going to notice the UITextField doesn't pad the text away from the icon.
Anyway, here's how I ended up doing it so that I had full control over the style.
So you can put your search icon in the UIImageView and then define your text field's style (border, background color, etc.) with the UIView. The UITextField ends up being plain white/transparent, and positioned so that its text doesn't overrun the search icon.
I hope that helps.
I'm creating a view which provides some fields for the user to fill in - address, phone number etc.
I've seen apps that have populated fields with grey text like 'Fill in your name here' on the Name field, and when the user taps on it the text is gone, a keyboard appears and the view is zoomed in to the textfield(or whatever it is). Also, after filling up that field tapping the 'Next' button on the keyboard brings the user to the next field. I'm trying to do the same but I have no idea where to get sources on this. Pardon my poor googling skills ;p
Tried UITextView and UITextField but there isn't anything related to this on the Interface Builder. Figured it lies with the codes?
It'd be great if I can get some explanation or some links on how this works (: Thanks!
EDIT: okay I played around with the Interface Builder a lil more and realized I could set placeholder as the grey text.
It is in fact a UITextField.
You can get the greyed out text by setting its placeholder property.
I am not sure what you mean by zooming but usually they do use a scroll view or adjust the frame so that it isn't blocked by the keyboard.
The Next button is made available using a UIToolbar instance as the text field's inputAccessoryView. However the implementation is one's own and is not framework provided. You can look at this example to get started.
I am trying to implement a control to edit text that will display the text in multiple colors. None of the solutions I have attempted yet have been good enough.
UITextView cannot accomplish this. All of the text must be the same color.
Using CoreGraphics to draw the text does not allow the text to be selected.
Using a UIWebView, DIV and PRE tags cannot be set to contentEditable on Mobile Safari.
Currently playing with using an off-screen TEXTAREA and an on-screen DIV to show the rendered text. This works pretty well, except supporting all of these at the same time seems impossible: click-to-type, click-to-move-cursor, click-and-hold-select/copy/paste.
Anyone have any tips on this predicament?
I've been trying to find any preexisting library out there that will accomplish this in a good way, to no luck. I'm open to any ideas!
Well, just pulling an idea out of my... let's say hat.
Could you put a transparent UITextfield over a view that draws the text? If the background was clear and the text color was clear the user could not perceive it but it should still respond to all commands. As the user enters and edits text you could draw the results on the view underneath.
I think the selection would work without any modification at all. When the user selected the clear text, it should create the illusion of selecting the drawn text automatically.
Like this one? StyledText http://three20.info/gfx/overview/styledtext.png It's in Three20 .
Here is an idea. I have no idea if it would work.
If you are only using colors, and not styles, the a UIWebView with colored text might layout text in exactly the same way as a UITextView. You could put a UITextView with invisible ink (text and background fully transparent) over a UIWebView and mirror the contents with colors in the html. I assume you can do scrolling with javascript along with the colored layout.