A word-wrapping text field with an image background on the iPhone? - iphone

Does anyone know how to create a text field that has a UIImage background and does word-wrapping?
It appears that word-wrapping is not available on UITextField, while a background image is not available for UITextView! Also, it should change the size of the control or at least alert that the number of lines changed so that a delegate could change its size..
An example of such control is the input field for messages in the SMS application.
Edit: the code should also get the text field to always show the text being edited. Apparently, when UITextView changes size while editing it somehow loses focus on the current text.
Thanks ahead!
Aviad.

Word-wrapping is not available on UITextField because UITextField only supports single-line text.
Use a UITextView. Make textView.backgroundColor = [UIColor clearColor]. Add the UIImage first, then the UITextView on top of it. Make sure the sizes are correct, and you should be fine.
As for changing the size of the UITextView, in your UITextViewDelegate, do something like this:
- (void)textViewDidChange:(UITextView *)textView
{
NSString *s = textView.text;
CGSize testSize = CGSizeMake(textView.frame.size.width, NSIntegerMax);
CGSize neededSize = [s sizeWithFont: textView.font constrainedToSize: testSize lineBreakMode: UILineBreakModeWordWrap]; // or UILineBreakModeCharacterWrap?
if (neededSize.height > testSize.height)
{
// grow textView
textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, neededSize.width, neededSize.height);
// then adjust the image size -- something like this
imageView.frame = textView.frame
}
}

Eventually I wrote my own code, which worked to some degree with a lot of workarounds (changing the focus for example, resizing when removing all the text again, etc). If anyone's interested, I'll post the code.
In other news, just today I saw in a different question around here something called IFVerticallyExpandingTextField. The name was promising, the code is here, and I'll give it a look. Anyone with a similar problem, you might want to see this too.
My mistake, I didn't read it carefully enough: IFVerticallyExpandingTextField is for the Mac, not the iPhone.

Related

UILabel does not wrap (if I change default font size)

I am working on a simple storyboard prototype. My TableViewController uses Dynamic Prototype as Content.
I have a cell with 4 label of which two will be set in code (the label text). The height of the cell will be calculated in code too. The Line Breaks are set to Word Wrap and everything's working fine with the default values (System 17.0):
see here:
..but if I change the Font Size of the "Fantasy Street..." label it will not break any more instead it just will be cut off!
see here: with System Font 16
Lines are set to 0
Word Wrap is still active
.. I also tried to do it manually in code but no change.
Does anyone have an explanation for that?
****edited:** when I add
myLabel.frame = CGRectMake(t.origin.x, t.origin.y, t.size.width, t.size.height *2);
to the cellForRowAtIndexPath I still see the cut off label. But if I then scroll the table view so the label is outside the viewable area shortly it will be displayed with the complete text when it is visible again.
By the way, I am working with viewTags, so I don't have a dedicated Cell Class e.g. UILabel *myLabel = (UILabel *)[cell viewWithTag:2];
You should check UILabel width; the width should be less than that of the value. Then like this:
(void) viewWillLayoutSubviews {
_landPhoneTips.preferredMaxLayoutWidth = _landPhoneTips.bounds.size.width;
}
I spent hours dealing with this identical problem before finally sorting it out last evening. After changing the font size, you must select the UILabel within the storyboard and select Edit > Size to Fit Content, even if you had already previously done so! In doing so you apparently reset some setting that gets messed up when changing the font size. Once done, the UILabel will wrap as it did previously.

Assign Some Text to particular line in UITextView Programmatically

I create an instance of UITextView programmatically. I want to assign some text to particular line in UITextView programmatically. Here is my code to create UITextView.
UITextView *textView =[[UITextView alloc]init];
textView.frame=CGRectMake(0,0,282,210);
[textView setReturnKeyType:UIReturnKeyDone];
[self.view addSubview:textView];
For example I want to add some text to particular line (at the end of line 5).
Programmatically, how is it possible to do this?
UITextView *textView =[[UITextView alloc]init];
textView.frame=CGRectMake(0,0,282,210);
textView.text = #"xxxxxx";
...
You can't edit lines directly in the text view because the number of lines depends on the content size and the size of the font.
Check this question as a reference:
How to Read Number of lines in UITextView
You could do some calculation to determine where to insert the text, but I'm nor sure if that's the best way to go around. There is probably a better way to accomplish what you are trying to do.
you can use the following:
NSString *str=yourTextview.text;
[str stringByAppendingString:yourNewString];
Then add it to textview
yourTextView.text=str;
check it and let me know if any clarification you need

How to bold text in UITextView? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UITextView formatting with fontname and bold
I am trying to Bold selected text of UITextView.I don't want to use UIWebView in my application.
How can I do that programmatically in UITextView????
Try -
textViewInstance.font = [UIFont boldSystemFontOfSize:14];
Edit 1: I didn't notice your statement of selected text. This just bolds the entire text. Sorry ;)
I was facing a similar issue. According to the documentation and the lack of answers from google, I doubt there's a solution as UITextView has only one format setting for all its text. It's all or nothing for UILabels and UITextView. The only way I see it, is to use UIWebView which gives you more freedom of styling individual words and text in the view. Good luck. :)
In UITextView i dont have any idea but i am done this in UILabel.
First set text in UILabel and setFrame of that label according to the text Height. After that use the regular Expression for the get frame for text which you want to make bold and add new label on that frame with bold font and text.
If you want to demo for above explanation then see the below link. It's have nice implementation.
FancyLabel_1.0
Edit: Now you can make selected text bold using "NSMutableAttributedString". See below peace of code.
NSString * string = #"<Your Full String>";
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:string];
[attStr addAttributes:#{NSFontAttributeName:[UIFont systemFontOfSize:13]} range:NSMakeRange(0, string.length)];
NSString * subString = #"<String that you want to make bold>";
[attStr addAttributes:#{NSFontAttributeName:[UIFont boldSystemFontOfSize:13], NSForegroundColorAttributeName:[UIColor redColor]} range:[string rangeOfString:subString]];
[<Your UILabel Object> setAttributedTitle:attStr forState:UIControlStateNormal];
Thanks,
MinuMaster

assign text of a UIlabel to a text field

I am quite new to iphone development. I have a situation here. I have some labels which can be dragged across the screen.
What I want is when any of these labels are dragged to some textfield and released over a textfiels UIlabel test is assigned to that text field.
The crux in this is I have to check for a condition when UILabel is inside UITextfield.
Can you help me to fabricate this condition.
Thanx in advance
This might not be the best way, but it should work:
CGRect textfieldFrame = textfield.frame;
CGRect labelFrame = label.frame;
if (!CGRectIsNull(CGRectIntersection(textfieldFrame, labelFrame))) {
// the two objects' frames are overlapping
}
This only works I believe if the two objects are attached to the same view, but it should get you started.

Word wrap not happening in UITextView

I'm doing something that requires storing some text entered in a UITextView. It all works fine until the text is restored, then it is not wrapped in the UITextView (like it did when it was entered) but is truncated (just like a UITableViewCell does by default, tail truncation).
Once the text is in the UITextView, it is saved to a Core Data managed object with
self.note.text = textNoteTableView.text;
On creating the UITextView (which lives in a custom UITableViewCell)
UITextView *textView = (UITextView*)[cell viewWithTag:1];
textView.text = self.note.text;
The text seems fine and I can catch it being saved and being restored OK in the debugger. What I need to have happen is for the text to be displayed in the UITextView just like it was when originally entered, wrapped and not truncated. I have not set any properties on this object so I can't work out why it is not just behaving the way it would when a keyboard is the data source, or when it is restored from xib if I leave in the
Lorem ipsum dolor default text.
- (void)textViewDidChange:(UITextView *)textView
{
textView.frame = CGRectMake(....); //set textView's frame to your original size
}
Above code will fix this problem.
The behavior comes from having the UITextView inside the UITableViewCell - I chose to do it a different way as I saw no solutions in all the reports I read.