Adding multiple lines to a UILabel that is updated by a UITextField - ios

So i'm making an application where the user converts the data from a UITextField into a UILabel. However, when updating a large amount of text instead of overflowing into another line, it simply displays '...' when there are too many words. I've tried changing the number of lines in the UILabel, even changing it to '0' which should mean the text displayed should fit the words in the UITextField, but this isn't the case. The following code I have for the UITextField to the UILabel is:
- (IBAction)add:(id)sender {
NSString *input = text1.text;
label1.text = input;
NSString *Input = text2.text;
label2.text = Input;
I'm new to developing and I've checked documentation and have found nothing. I did notice someone saying I should use a UITextField, however this doesn't suit my needs as to what i require the UILabel to display.

Maybe try setting the lineBreakMode property of the UILabel to NSLineBreakByWordWrapping, as the default value for this property is actually NSLineBreakByTruncatingTail which could explain the '...' that you're experiencing when the label has too much text. Also make sure the adjustsFontSizeToFitWidth property is set to NO.
textLabel.adjustsFontSizeToFitWidth = NO; // this is default value
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;

Options for UITextView that you may have not seen
UITextView * _descriptionText = [UITextView new];
[_descriptionText setTextAlignment:NSTextAlignmentLeft];
[[_descriptionText textContainer] setMaximumNumberOfLines:11];
[[_descriptionText textContainer] setLineBreakMode:NSLineBreakByTruncatingTail];
Not sure if this helps, but combining this with Bamsworlds answer should do it

Related

How to keep a space at the end of the string in ios, objective C without changing its UI(position)

I have created a label programmatically.it's with is equal to the device width and I have aligned it to right.so it shows the text from right.
like this
titleLabel.textAlignment = NSTextAlignmentRight;
then I gave a text to the label.
titleLabel.text = #"Flight Summary";
but I want to keep a space after y letter in summary, without decreasing the width of the label.I tried with using string format like this.
titleLabel.text =[NSString stringWithFormat:#"%# ", #"Flight Summary "];
but nothing happned.how can I do that.hope your help for this.thanx.
To reduce the complexity of subclassing, you can take UIView and UIlabel, set frame of UIView to screen width and take UILabel frame as screenwidth - 8 (or whatever pixels is appropriate for you). Manage the frames and add both to mainview, this way you will be able to achieve the look.
Try this
titleLabel.text = #"abcd exadgdf \u{200c}""
You can also do one more thing. You can use attributed string and set attributed text to titlelabel without formatting string like this
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:#"ABCD "];
titleLabel.attributedText = attrString;

Why is my NSAttributed String losing its attributes when the view reappears?

I am using UIAppearanceProxy in my app, like so:
UILabel *label = [UILabel appearance];
label.font = myAppFont;
label.textColor = myAppTextColor;
Sometimes, though, I want to override the default font to use glyphs from Awesome Font. I do that like this:
NSString *basePos = [NSString stringWithFormat:#"%d %#", (int)self.user.positives, kUpArrow];
NSMutableAttributedString *posString = [[NSMutableAttributedString alloc] initWithString:basePos];
[posString setAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"FontAwesome" size:18]} range:NSMakeRange([posString length] - 1, 1)];
[self.positivesLabel setAttributedText:posString];
This works great when the view is built and appears, but if I navigate to another page (I am using UIPageViewController) and back, my attributes are gone.
I've tried re-setting-up the attributes in:
viewWillAppear:
viewDidAppear:
viewWillLayoutSubviews
viewDidLayoutSubviews
...to no avail. When is this string getting reset to the default? And how can I override that?
A similar thing was happening in to me.
The reason that this was happening was because I had set the font property on the appearance proxy of UILabel, and for whatever reason that overtook the attributes of the attributed string when the view re-appeared.
I fixed this by setting the font property on the UILabel to nil before setting the attributedText. Note that setting the font to any font also fixed this issue; it does not necessarily need to be set to nil.

How can I make a UITextView layout text the same as a UILabel?

I have a UILabel that I need to convert to a UITextView because reasons. When I do this, the text is not positioned the same, despite using the same (custom) font.
I found that if I set:
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;
This gets the text very close, but if I superimpose the UITextView over top of the UILabel, I see the text positioning get farther apart with each new line.
The UILabel is green, the UITextView is black. This is using NSParagraphStyle to set min and max line height to 15.
I've played with setting the paragraph style and min/max line height, but I haven't been able to match it exactly. I'm not a printer, so I don't necessarily understand all of the font related terms in the documentation for NSLayoutManager and NSTextContainer and all that.
I only need to support iOS 7 and up.
I'm not going to switch to some crazy CoreText-based custom widget or use some random third party library. I'm okay with close enough if I have to. But it seems like there should be some combination of random properties to make them layout the same.
I took the solution for line spacing found at this link and applied it to your issue. I managed to get it incredibly close by adjusting the lineSpacing property. I tested with HelveticaNeue size 13 and managed to get it to line up as shown in the screen shot below.
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = -0.38;
NSDictionary *attrsDictionary =
#{ NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue" size:13.0f],
NSParagraphStyleAttributeName: paragraphStyle};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attrsDictionary];
I've been able to successfully 'impersonate' a non-editable multiline UILabel (as it happens, in a UITableViewCell subclass) with an equivalent editable multiline UITextView using the following :
_textView = UITextView.new;
_textView.font = _label.font;
_textView.textColor = _label.textColor;
_textView.textAlignment = _label.textAlignment;
_textView.backgroundColor = UIColor.clearColor;
_textView.textContainer.lineFragmentPadding = 0;
_textView.textContainerInset = UIEdgeInsetsZero;
and to make it behave well when doing actual edits, add the following to your UITextViewDelegate:
- (void)textViewDidChange:(UITextView *)textView
{
...
[textView scrollRangeToVisible:NSMakeRange(textView.text.length, 0)];
[textView scrollRectToVisible:[textView caretRectForPosition:textView.endOfDocument] animated:NO];
}

Calculate part of the text truncating the UILabel

I have a UILabel that contains some text that is more than the size of UILabel. I don't want to change UILabel size i only want one line in it. So my question is how can i skip(remove) the text from end that is causing the UILabel to truncate tail?
I'm afraid there is no a direct method to calculate. But as you can get the size or bounds of a text using boundingRectWithSize:options:attributes: you can iterate using a dichotomic search algorithm and find the string position to cut from.
Use this line to set up the line break mode of your UILabel:
self.lineBreakMode = UILineBreakModeWordWrap
Try this,
I think you are asking for character wrapping trick.
First try in XIB
yourLabel.numberOfLines = 1;
yourLabel.lineBreakMode = NSLineBreakByCharWrapping;
//or
yourLabel.lineBreakMode = NSLineBreakByClipping;
I was doing a similar thing on UITableView Cell recently. This approach is flexible, as you can set any number of lines depending on height you have available. Everything else will be truncated. Check it out:
CGSize maxNameLabelSize = CGSizeMake(350,60); //setting height, you can limit it to one line depending on the font
UILabel *eventName = (UILabel*) ([activityCell.contentView viewWithTag:EVENT_TITLE]);
//Set value here
eventName.text = event.name;
CGSize expectedNameLabelSize = [eventName.text sizeWithFont:eventName.font constrainedToSize:maxNameLabelSize lineBreakMode:eventName.lineBreakMode];
CGRect newNameFrame = eventName.frame;
newNameFrame.size.height = expectedNameLabelSize.height;
eventName.frame = newNameFrame;
Does it makes sense?

UITextview typingAttributes not working

I have UITextView and I want to set it's line height to 50.0f so I'm using typingAttributes, but nothing works, my code goes like this in ViewDidAppear Method
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = 50.0f;
paragraphStyle.lineHeightMultiple = 50.0f;
paragraphStyle.maximumLineHeight = 50.0f;
NSDictionary *textViewAttributeDic = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
textView.typingAttributes = textViewAttributeDic;
text doesn't effected by setting typingAttributes,and I tried to changed the color and font using typingAttributesbut nothing works
i've read all stack answers and documentation
what i'm doing wrong :(
update:
i even tried
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = 50.0f;
paragraphStyle.lineHeightMultiple = 50.0f;
paragraphStyle.maximumLineHeight = 50.0f;
NSDictionary *textViewAttributeDic = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
textView.attributedText = [[NSAttributedString alloc] initWithString:#"" attributes:textViewAttributeDic];
when I tried
textView.attributedText = [[NSAttributedString alloc] initWithString:#"blahblah" attributes:textViewAttributeDic];
It worked, but i need empty textView with no spaces or 'blah' characters
The documentation clearly states that typingAttributes is for the editing mode of the text field.
typingAttributes
The attributes to apply to new text being entered by the user.
...
If the text field is not in editing mode, this property contains the value nil. Similarly, you cannot assign a value to this property unless the text field is currently in editing mode.
Instead, you should assign attributedText instead of the text property. The mechanism to specify the attributes is via NSAttributedString that you assign.
You may configure paragraph style in IB using temporary text in text view and setting all required attributes. Then you clear your text view on launch textView.text = nil. Text attributes should remain the same.
in ios6 I solved it like this , in textView delegate I wrote:
- (void)textViewDidChange:(UITextView *)textView{
// 1st save current selected range ... we gonna use this value in 5th step
NSRange textViewCurrentRange = textView.selectedRange;
// 2nd disable scrolling in textview
[textView setScrollEnabled:NO];
// 3rd set the new enterd text as attributed string to textview
[textView setAttributedText:[[NSAttributedString alloc]initWithString:textView.text attributes:self.textAttributes]];
// 4th enable scrolling
[textView setScrollEnabled:YES];
// 5th re set selected range so text inidicator will get back to it's place
textView.selectedRange = textViewCurrentRange;
}
Programmatically setting attributedText resets the typingAttributes. Set typingAttributes last.
Here's one gotcha - as mentioned in other answers, the UITextField must be in editing mode for this to work. If you are running in the simulator, and the keyboard is hidden (cmd+k), the textfield is not in editing mode if you type using your computer's keyboard. Make sure the simulator's keyboard is displayed.
I took a hint from #Mundi's answer.
In my case, I set typingAttributes when the viewController was created, and it did not work
I was entering code according to placeholder in textViewShouldBeginEditing, so I reset typingAttributes in that method, but it didn't work the same way.
When I saw #Mundi's answer and resetting typingAttributes in textViewDidBeginEditing, it worked fine.

Resources