UITextField ignoring constraints and shifting text offscreen - ios

I have created a static table view in Storyboard with one cell that contains a UITextField for text entry. The text field has constraints to bind it to the cell for top, bottom, trailing, and leading so that it will have the right width on all resolutions.
In viewDidLoad, I enter some placeholder text for an empty state.
NSMutableAttributedString *placeholderString =
[[NSMutableAttributedString alloc]
initWithString:NSLocalizedString(#"Note", #"")];
[placeholderString beginEditing];
[placeholderString addAttribute:NSForegroundColorAttributeName value:
[PulseColor tableViewEmptyCellBackgroundColor] range:NSMakeRange(0,
placeholderString.length)];
[placeholderString endEditing];
self.txtNote.attributedPlaceholder = placeholderString;
self.txtNote.delegate = self;
The problem is that rather that sticking to the constraints, the width of the UITextField shrinks to the size of the placeholder text. As I type and the message reaches the end of the smaller text field, the text expands to the left and disappears before expanding to the right. (If I had the reputation, I'd post images)
Is there a setting that I am missing or some other way that will either cause the UITextField to keep its width across the cell, or cause the text to not disappear to the left as it reaches the initial length?

Related

make UitextView horizontally scrolling and not truncating the text.

I wish to make a horizontally scrollable uitextview in which a user can type text that is much longer than the width of the textview.
But currently, when i have implement a textfield, the text stops at the end of the textview, even when i continue to type and setting textContainer.maximumnumberoflines = 1.
I wish it to have the same vertical scrolling function where the height textview expands while users types more text onto the textview. But in my case, i want the width of textview to expand to accommodate words.
At the end of the long text, i wish to be able to scroll the textview horizontally to view the full text.
The current failed attempt by me looks like this.
currently, the word stops at "hahahh", no matter how much i type, nothing changes.
textContainer.maximumnumberoflines = 1 ----> This is not required (By default textField has scrolling property). You can initialize textField like this and it works. Here specify your textField frame, superView to which this textField has to be added, color and font.
UITextField *myTextField = [[UITextField alloc] initWithFrame:frame];
myTextField.font = giveFont;
myTextField.textColor = giveColor;
[superview addSubView:myTextField];
You can use TextView instead of TextField.
TextView is located just above the ScrollView in Object Library.
Check this out

Setting attributedstring to label with backgroundcolor

I have two lined Label and setting the backgroundColor with an attributedString.
NSString *string = #"This is a UILAbel with two lines";
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc]initWithString:
string];
[mutableString setAttributes:#{NSBackgroundColorAttributeName : [UIColor redColor]} range:NSMakeRange(0, string.length)];
If the text makes a line break it fills the backgroundColor to the right border and in the second line it stops at the last character.
But I want the backgroundcolor in the first line just set to the last character too.
The backgroundColor property is applied to the entire rect representing the label.
To resolve this you need to dynamically adjust your UILabel's frame depending on its content, in order to achieve the desired effect.
There is a method on UILabel called sizeToFit; try calling it.
Although I recommend you use autolayout and let the label resize itself based on constraints.

UILabel extra spaces before text

I have a label, if i add text to label it added one extra space at the beginning of the text, app supports only portrait orientation. tried following solutions but no use. attached images for reference.
I have created this label in xib with
number of lines 2
with constrains leading, trailing, bottom space to container, height is not fixed,
and set text like
dealName.text = "Cup of Coffee with A asdf asd asdf adsf ads fad"
UILabel extra spaces before and after text ios
UIlabel shows extra space before text
UILabel with multiple lines
UILabel with single line
try this may be there is an any white space before character this will remove this..
NSString *trimmed = [yourstring stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
yourlable.text = trimmed;
OR
Also see if you set attribute paragraph spacing from here..

Objective C - IB - Vertically centering 3 multiline UILabels

I have 3 UILabels stacked on top of each other like so:
The top label can be 2 lines, but isn't all of the time. I'm trying to vertically center all 3 labels between the top and bottom lines in picture. I'm just not sure how to go about doing it. I've tried setting the space between them equal and give ≥ margins on the top and bottom but it doesn't seem to work.
Trying to do this without code, all in the interface builder if possible.
You could try putting the three labels inside a UIView, and then assigning that UIView to align to the center of the UIImageView.
You can easily do this by selecting all three UILabels, and going to Editor->Embed In->UIView. Select the new view and Shift Click on the image and select Editor->Align->Vertical Centers
Forget about using different labels for this, it's nightmare. Use NSAttributedString with one UILabel it'll automatically center the text.
NSString *name = #"John\nMultiline\nSmith"; // your multiline string
NSString *title = #"Salesguy"; // some more strings
NSString *phone = #"555-55-55";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#\n%#", name, title, phone] attributes:#{NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue" size:17.0]}]; // construct the label with default parameters
[attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Bold" size:17.0] range:[attributedString.string rangeOfString:name]]; // add bold selection to the name part
myLabel.attributedText = attributedString;

UITextView not showing long text

My iOS app is not showing long attributed strings. I have a cell in a tableview which contains this textView. When the text is very long the tableview is unresponsive for a while but when it loads the text is not shown. All other cells are displayed fine. And the textView works fine with small text strings.
Here's the code:
descriptionCell = [tableView dequeueReusableCellWithIdentifier:#"CellAdDetailDescription"];
descriptionCell.bodyTextView.delegate = self;
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.ad.body];
UIFont *cellFont;
cellFont = [UIFont fontWithName:#"HelveticaNeue" size:16.0];
NSDictionary *attributesDictionary;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;
attributesDictionary = #{NSParagraphStyleAttributeName : paragraphStyle , NSFontAttributeName: cellFont};
[str addAttributes:attributesDictionary range:NSMakeRange(0, str.length)];
descriptionCell.bodyTextView.attributedText = str;
I pasted the long string here. I debugged and str is being loaded fine containing the desired text.
Whats wrong here?
What is the max allowed string length in UITextView?
EDIT: very odd, when trying selection in the textView, the text is being shown in the magnifying glass. I posted a video here.
Is it a bug in UITextView?
Here is the screenshot. The blank white at the bottom is the textView.
It could have something to do with the scrolling. If you are showing all the text (i.e. the text view is expanded to be as high as it needs to be, and so is the table view cell), the scrolling is done by the table view. If the text view is smaller, you have to scroll to see all the text - this might cause a conflict with the table view, which is also a scroll view.
It has been suggested that you disable the scrolling of the text view before adding the attributed text and reenable it afterwards. If you are showing the whole text in the table view, you can leave the text view scrolling disabled. In some cases, it will only work if scrolling is enabled. You should check this possibility as well.
I also had this issue (= very long attributed text didn't show up in the UITextView within an autosized UITableViewCell). I tried all accepted answers from here and from this question: UITextView not loading/showing the large text?. None did work.
However I then found out that - in my case - it just works fine on the device. So if you're still struggling with this kind of problem, don't rely on the iOS Simulator.
The reason that you UITextView not show all text because it Frame is too small so it truncates the text to fit with its frame. you can do follow step to show all text:
In your CustomUITableCell, override layoutSubView:
Use this function to calculate size of TextView that fit it content
[textView sizeThatFits:CGSizeMake(textView.frame.size.width, CGFLOAT_MAX)].height
After that, calculate and set new frame for TExtView (in uitableCell custom)
In the tableView:heightForCellAtIndexPath, calculate new size of textView (also height of cell) similar like above and return right height for cell.

Resources