Providing *hint* in text field in ios 7 as edittext field in android - objective-c

I am creating a feedback form in an ios7 application and want to place few UITextFields which contain hints in the respective UITextField for name, phone number, email address, etc. I am using xcode 5 and ios7 for programming and creating application without use of storyboard.
The hint property is available in android for edit text field, but I am unable to find any such property in UITextField in ios 7.

Make use of the Placeholder text property.
Here's an example:
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 100, 50)];
textField.font = [UIFont fontWithName:#"Arial" size:20];
textField.placeholder = #"Your hint here.";
If you are using a storyboard or XIB, this property can be edited there also.
Also, if you want to use custom attributes for the placeholder, you can use the attributedPlaceholder property:
NSAttributedString *attribString = [[NSAttributedString alloc] initWithText:#"Your hint here." attributes:#{NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue" size:16.0]}];
textField.attributedPlaceholder = attribString;

Related

Xcode: Position Images Inside UITextView

I have a string that is added to the UITextView, and I would like to add images after a certain world in that string.
Example:
#property (strong, nonatomic) IBOutlet UITextView *textview;
textview.text = #"This is an icon of a cat. And this is an
icon of a car. Here is more random text.";
This is an example of what I want:
How do I append and position images after a certain word?
I have an array that detects certain words:
NSArray *detectWords = #[#"cat", #"car"];
So I can detect when certain words show up in the text of a textview, but I'm not sure how to add an image right after those words.
Also, the string of words will vary in different situations (not static), so pre-positioning everything on the interface builder is not an option for me.
Thanks in advance!
Check out this library it might help you
https://github.com/dzog/ImgGlyph
Like this way, you can also do this,I hope it will help u
Here is how to put an image in the right portion of a UITextField:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 500, 20)];
textField.rightViewMode = UITextFieldViewModeWhileEditing;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
imageView.image = [UIImage imageNamed:#"Dog.png"];
textField.rightView = imageView;

Changing the font of the selected text in Objective-C

I'm trying to create a custom “Change font” NSPopupButton for a Mac App (not an iOS App). I can detect a change in font selection:
long fontItemIndex = [fontPopup indexOfSelectedItem];
NSMenuItem *fontItem = [fontPopup itemAtIndex:(int)selectedFontItemIndex];
NSString *fontName = [selectedFontItem title];
Given this NSString of a font name, I cannot seem to find out how to actually change the selected text in my NSTextView textView to this new font.
I'm simply dazzled by the official documentation: it seems convertFont:toFamily: is what I need. When I do this:
NSFont *font = [NSFont fontWithName:fontName size:12.0];
[textView setFont:font];
It sets all text in the text view, not just the selected text. But when I do this:
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[fontManager convertFont:[fontManager selectedFont] toFamily:fontName];
it doesn't do a thing. What am I missing?
Inside a NSTextView is a NSTextStorage (a subclass of NSAttributedString) and you’ll have to modify the attribute named NSFontAttributeName.
First get the range where you want to change the font attribute:
NSRange selection = textView.selectedRange;
Now add the font attribute to the selection:
NSFont *font = [NSFont fontWithName:fontName size:12.0f];
[self.textView.textStorage addAttributes:#{NSFontAttributeName: font}
range:selection];
Depending on the contents of your NSPopUpButton it should be enough to call fontWithName:size: with title as the font name to get the just selected font. But if the method you already do doesn’t work, you’ll probably have to get a specific font from the font family name. availableMembersOfFontFamily: on NSFontManager will give you a list of all available fonts. You can use one of them to initialize a specific font.
Take a look at the setFont:range: method on NSText, the superclass of NSTextView.
(The ranges, of course, come from the selectedRanges property on NSTextView.)
This was all I needed to change all the text in my textview.
[textview setFont:[NSFont fontWithName:#"Courier" size:14]];

NSTextField add line spacing

I use NSTextField not NSTextView to receive the user input, but I need to custom the font and textColor and line spacing. I use the code below, it's ok for font and color but I don't know how to set a line spacing.
[self.titleField setTextColor:textColor];
[self.titleField setFont:bold14];
And I also use a NSAttributedString to solve the problem:
NSFont *bold14 = [NSFont boldSystemFontOfSize:14.0];
NSColor *textColor = [NSColor redColor];
NSMutableParagraphStyle *textParagraph = [[NSMutableParagraphStyle alloc] init];
[textParagraph setLineSpacing:10.0];
NSDictionary *attrDic = [NSDictionary dictionaryWithObjectsAndKeys:bold14, NSFontAttributeName, textColor, NSForegroundColorAttributeName, textParagraph, NSParagraphStyleAttributeName, nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:title attributes:attrDic];
[self.titleField setAllowsEditingTextAttributes:YES];
[self.titleField setAttributedStringValue:attrString];
the code above is ok to show a attributed string, but when I delete the string in the textfield and start to input, the words come without any attribute.
How can I input a string in NSTextField with custom font, color and line spacing?
It's best to stay with NSTextField's attribute setting methods instead of an NSAttributedString because then it can send the settings to the field editor. Every text field has an NSTextView (most of the time) "Field Editor"; and the field editor is what is doing the editing.
Your NSAttributedString isn't sticking because you're only telling the textfield to temporarily display that one string. When the field editor pops up the text field (cell) passes on its own attributes like textField.font and textField.textColor but never the NSAttributedString's attributes.
It would be best to use an NSTextView to be able to use -setDefaultParagraphStyle because you're editing multiple lines anyways, from what I see. If you can't, because of performance problems or something else, then:
Subclass NSTextFieldCell, because that's what does all the NSTextField work, and override
- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj
(declared in NSCell) to set up attributes for your field editor the way you want it, so you can send it a line height value through -setDefaultParagraphStyle (and font etc.) yourself. (textObj is the field editor to be set up).

UIButton with two lines of text in the title (numberOfLines=2)

I'm trying to make a UIButton that has two lines of text in its titleLabel. This is the code I'm using:
UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
[titleButton setTitle:#"This text is very long and should get truncated at the end of the second line" forState:UIControlStateNormal];
titleButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleButton.titleLabel.numberOfLines = 2;
[self addSubview:titleButton];
When I try this, the text only appears on one line. It seems the only way to achieve more than one line of text in UIButton.titleLabel is to set numberOfLines=0 and use UILineBreakModeWordWrap. But this doesn't guarantee the text to be exactly two lines.
Using a plain UILabel, however, does work:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = #"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:titleLabel];
Does anyone know how to make the UIButton work with two lines? Is the only solution to create a separate UILabel to hold the text, and add it as a subview of the button?
You don't need to add a UILabel to the UIButton. That's just extra objects and work.
Set these properties on the titleLabel of your button.
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 2;//if you want unlimited number of lines put 0
Swift:
button.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
button.titleLabel!.numberOfLines = 2//if you want unlimited number of lines put 0
Updated answer for more recent iOS versions
Since this is the accepted answer, added #Sean's answer here:
Set these properties on the titleLabel of your button.
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 2; // if you want unlimited number of lines put 0
Swift 3 and 4:
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.numberOfLines = 2 // if you want unlimited number of lines put 0
Original answer for an older version of iOS
If you want 2 lines of text on top of your UIButton you should add a UIlabel on top of it that does precisely that.
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = #"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[myButton addSubview:titleLabel]; //add label to button instead.
Updated for interface builder solution
Added #Borut Tomazin's answer for a more complete answer.
Updated this part again since the answer of #Borut Tomazin was improved.
You can do this much easier, with no code required. In Interface Builder set Line Break on UIButton to Word Wrap. Than you can insert multiple lines of title. Just hit Option + Return keys to make new line. You will also need to add this to the User Defined Runtime Attribute in Interface Builder:
titleLabel.textAlignment Number [1]
You can do this much easier, with no code required. In Interface Builder set Line Break on UIButton to Word Wrap. Than you can insert multiple lines of title. Just hit Option + Return keys to make new line.
You will also need to add this to the User Defined Runtime Attribute in Interface Builder:
titleLabel.textAlignment Number [1]
It's that simple. Hope it helps...
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[button setTitle: #"Line1\nLine2" forState: UIControlStateNormal];
To avoid completely the need to edit code, and thus the need to subclass your view, in Xcode5 and greater you can follow Borut Tomazin suggestion:
In Interface Builder (or storyboard) set Line Break to
Word Wrap. Than you can insert multiple lines of title. Just hit
Option + Return keys to make new line.
and then, in the User Defined Runtime Attributes you can add
Key path: titleLabel.textAlignment
Type: Number
Value: 1
Note: this may be not completely "future proof" since we are translating the UITextAlignmentCenter constant into its numerical value (and that constant may change as new iOS versions are released), but it seems safe in the near future.
You can modify the needed value directly from Storyboard.
Select the button, go to the identity inspector and add the following key-value pair in the "User defined runtime attributes" section:

Change font color of UITextView

I have a UITextView where the user can input text. Say the user has already inputted a value. How can I change the font color of that already typed text and future typed text to a different one with the click of a button?
I already have it set up where I can change the color of the text but it only works if I choose a color BEFORE I start typing. After I start typing and I attempt to change the color, it doesn't do anything.
I have this:
-(IBAction)changeInkColor:(id)sender
{
[inkTextField setTextColor:[UIColor greenColor]];
inkTextField.text=#"text";
}
and that actually works and displays "text" in green only if there is no text already in the view. However if I type something in and then hit this button, nothing happens.
I just tried that and I had no problems. I set up a button that called [myTextView setTextColor:[UIColor redColor]];
After typing a bit with black text color, I pressed the button, and everything turned red. Then I continued typing, all in red.
Are you using setTextColor: to do this also?
In the end, setTextColor: is the answer, there's an important detail missing from the earlier answers: To get this to work in iOS 8, I had to set the color =after= I set the text.
Hence,
- (void)viewDidLoad
{
[super viewDidLoad];
_myTextView.textColor = [UIColor whiteColor];
_myTextView.text = #"yadda yadda yadda...";
// etc., snip
Did NOT work, while
- (void)viewDidLoad
{
[super viewDidLoad];
_myTextView.text = #"yadda yadda yadda...";
_myTextView.textColor = [UIColor whiteColor];
// etc., snip
DID work. This strikes me as a bug in iOS 8, which I will write up.
You can fix this (at least in Xcode 8.2) in IB by toggling the textColor button (6th button in the row). With this button selected, if you set the textColor programmatically before you enter text in the view, the color will "stick." I have not found an elegant way to set this on the text view programatically.
However you can also work around this in code:
textView.text = #" ";
textView.textColor = UIColor.redColor;
textView.text = #"";
I had the same problem as you, then I realised that the text was not changing colour, and was red by default because I was doing:
placeholder = #"Welcome";
instead of
WelcomeField.text = #"Welcome";
Hope it helps
use attributed text of UITextField
NSAttributedString *string = [[NSAttributedString alloc]initWithString:self.textField.text attributes:#{NSForegroundColorAttributeName:color}];
self.textField.attributedText = string;
Very interestingly, while
[_nameLabel setTextColor: [UIColor redColor]];
didn't work,
_nameLabel.textColor = [UIColor redColor]];
worked. Hope it helps for others. Cheers.