How to make a new line in UITextView in nib/storyboard? - ios

I am trying to make a new line in Xcode 4.2 UITextView, and when I do alt+return, it goes to the next line, but does not show up when built and ran.

This works, I'm unsure what you are doing differently.
Drag out a UITextView.
Double-click it to edit text, place cursor where you want the paragraph break.
Option-Enter a couple of times to create a blank line & paragraph.
The paragraph shows at runtime. There's also a Text attribute that can be edited in the Attributes inspector.

ALT + Enter makes a new line in storyboard

In the attributes inspector, there is property 'Lines' to set how many lines of text.
Change it to 2.

This is a workaround, but it works everytime and also for UILabels which is more interesting:
Just open a text editor (e.g. textEdit or XCode itself), write the text with the new lines as desired, and copy paste into the Label text property in Interface builder. It shows in both, interface builder and runtime.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Note: the \n created a new line.
//This piece of code assumes there IS ALREADY text in the theTextView
self.theTextView.text = [[NSString alloc] initWithFormat:#"%# \n\n a line appart",self.theTextView.text];
}
Assuming you created the "theTextView" in the (.h) file and synthesized it in the (.m) file and made the appropriate connection.
I can give a more clear answer if you tell al wen exactly the new line must be made... Programmatically of when the user presses buttons?

Drag a view (resize to a line) and add it a background color.
I'm not sure if there's another way.

Related

UITextField Alignment : Placeholder right aligned & Actual text gets left aligned

I have a UITextField set in xib. Text Alignment is set to right. When app is running, I see textfield which shows placeholder with right aligned text. When I start typing it still aligned to right. As soon as I move the cursor from that textfield to some other UI component, typed text gets left aligned.
EDIT :
I have resolved it partially in one scenario while editing in textField. But direct text assignment from local data source having same problem.
Editing Text Field Solution
- (void)textFieldDidEndEditing:(UITextField *)textField{
textField.text = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
Still facing alignment issue if I say,
textField.text = #"my Test";
Applied same logic before assigning text to textField as did in editing mode but still it gets left aligned automatically :(
Actual Output Problem
XIB Setting for UITextField
I have been using a UILabel category class for vertical alignment adjustment. In that there was a method +(void)loadand inside that method i was using following line
method_exchangeImplementations(class_getInstanceMethod(self, #selector(textRectForBounds:limitedToNumberOfLines:)), class_getInstanceMethod(self, #selector(verticalAlignTextRectForBounds:limitedToNumberOfLines:)));
method_exchangeImplementations(class_getInstanceMethod(self, #selector(drawTextInRect:)), class_getInstanceMethod(self, #selector(verticalAlignDrawTextInRect:)));
This particular lines of code caused the problem for me. It was overriding the existing default behaviour of the UITexfield. Commenting this worked for me.
So something similar would be there in your code also.
Hope this will help you :)
You can do the following:
1- set the textAligment to the right
2- add UITextFieldDelegate to your.h
3- yourTextField.delegate = self
4- Add the delegate method 'willBeginEditing' and set the textfield aligment to the left inside it
5- Add the delegate method 'didEndEditing' and set the textfield aligment to the right inside it IF the textfield.text is = #""
Happy Coding!
I have tried as same as you performed and it's working fine.
I think You have to delete that textfield and put it again with new identity.
Then Clean and Rebuild your project.
Make sure You are not giving any values or parameters to textfield programatically.

Swift iOS localization - not all strings in main.strings (xcode 6.3)

I try to localize my iOS app. Unfortunately I run into trouble with the localization. My problem is by adding the additional language, not all strings are listed in the main.strings file for the additional language. I have an scroll view and an label in my ViewController. The scroll view itself also has many label. Wenn I go into the main.string file, I miss the separat label as well as the first label in the scroll view.
Because the controller is the second view controller working as a description view, I've not added IBOutlets to the label. Just use them as text.
Does anyone know how to add these two label to the main.string file or maybe just give me a hint what I can check to see what's maybe wrong?
Thanks in advance
Ok, after two additional days I've found the reason why it was not working. At all I hope my question was not to silly and all member just thought RTFM.
I've solved my following problems:
1) How can I add additional Label-Text to the Localization file "main.strings" and
2) By adding an Language not all text are entered by Xcode to the main.stings file.
1)
In the document block from the identity inspector is a filed called Object ID. This is the Xcode generated reference to the Label (Text). Adding this to the specific language main.strings file with, close the line with an ";" and it works.
2)
Well, as far as I could investigate, localization just works with plain text (Attributes Inspector - Block Label). It does not work with attributed text. Maybe I did something wrong, but changing to "Plain" and problem was solved.
Thanks for all who spend some time to read my question.

Clear Label text used in Storyboard

Wondering if anyone know hows to complete the following...
In a Storyboard I set my Labels with dummy text, they are not Static text. For example 'User1 Username'.
What I would to know if there is a setting to clear the value of this label when it is loaded by the view. I have some other code that runs off and collects the relevant information. However, it might take a few seconds so a HUD is shown to the user whilst it loads.
Of course in the background of the HUD you can see the example text shown. I know in viewDidLoad I could simply clear all the label texts setting them back to #"", but is there no setting in the storyboard or anything for this?
You can use User Defined Runtime Attributes to achieve this. Simply set "text" attribute for any UILabel, UITextField or UITextView to get what you want:
KVC and runtime attributes are really powerfull when working with Storyboard (e.g. did you know that you can set "layer.cornerRadius" attribute to any UIView to get rounded corners?).
Unfortunately, there is no other way to do it.
You would have to set the text of the UILabel manually in the ViewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
self.myLabel.text = #"";
}

How to display more than one line in UIMenuItem?

I'm trying to display more than one line of text in a custom UIMenuItem.
I've tried using a simple "\n" in the title property of the UIMenuItem but with no luck.
Example:
UIMenuItem *menuItem; //Is then allocated properly....
// Before the Menu is displayed
menuItem.title = #"This is a first line.\nThis is a second line.";
Unfortunately I just end up with one line being displayed...
What I want to achieve is something similar to what you can see upon a LongPress on a row in the Apple iPod/Music app.
I've just found out about this github project as a solution:
https://github.com/questbeat/QBPopupMenu
You can display whatever custom UIView within a MenuItem. So I inserted a multi-line UILabel and that works.
However I would rather use the native Apple UIMenutItem approach if this is possible. Any ideas? Thanks in advance.
You would need to change some internal property of the UIMenuItem, but unfortunately Apple does not provide a way to do it.
So, as for today, there's not way to change the display of a UIMenuItem.
Have you tried '\r' instead of '\n'?

How to write multiple lines in a label

I have a XIB file of type View XIB. In it I have a View controller, and in the View controller I have a label.
I have long text that I want to break, but it doesn't - it gets truncated.
I tried this solution - I did it in the attribute window - but it doesn't help.
You could use a TextView and disable the Editable and User Interaction Enabled options in IB.
use this
myLabel.lineBreakMode = UILineBreakModeWordWrap;
if you are worried about backwards compatibility use this because the comand i mentioned before was introduced in iOS 6.0
myLabel.numberOfLines = 0;
I have been running into a similar problem. I started out with a label, but wanted multi-lines and a UITextView worked great for this, at first. Because I am using a third party library (MBProgressHUD) to handle stuff like progress messages and what not, I had thread problems when trying to update a UITextView and show the progress message at the same time.
So I went back to UILabel which actually didn't have thread problems. I found a property to allow a specific number of lines of my choosing, and had to create the label big enough to display those lines. The downfall to this approach is that I don't get the context menus like Copy, Paste, etc. but more importantly I'm not running into thread problems. I can always embed this into a UIScrollView in the future if I so choose.
You could check out my category for UILabel on GitHub. https://gist.github.com/1005520
This lets you resize the height of the label to accommodate the content.
From the Attiribute inspector, choose LineBreaks->Word Wrap option when you have selected the lablel. Also increase the number of lines Label->Lines.
Try Following steps to solve issue :
Drag out a UITextView.
Double-click it to edit text, place cursor where you want the
paragraph break.
Option-Enter a couple of times to create a blank line & paragraph.

Resources