UILabel extra spaces before text - ios

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..

Related

How do you tighten UILabel's sizeToFit to use smaller margins?

I'm creating a UILabel, adding some text, and then calling [myLabel sizeToFit], which resizes the label to fit the text. However, I'm noticing some margin -- or edge insets -- to the left and right, almost as if it's purposely buffering my text.
I'm very short on space, and I'd like to run sizeToFit without these margins, i.e. I want the last pixel of my text to run up against the bounds on left & right. How do I accomplish this?
- (void) layoutGroupTabLabeled:(NSString*)title {
// Determine how long a label we need:
UILabel* label = [[UILabel alloc] init];
label.text = NSLocalizedString(title, #"");
[label sizeToFit];
}
The label should be tight, but has space to left & right.
The answer to your problem is using AutoLayout constraints. In your case it should be covered by adding leading and trailing constraints to the label.
If you don't know your way around AutoLayout I recommend checking these answers.
Adding constraints programmatically in Objective-C

UITextField ignoring constraints and shifting text offscreen

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?

Texts between Two UILabels don't align

I have 4 UILabels aligning in a scroll view embeded in a view. I'am not using anto-layout. The 4 UILabels will display different strings. Sometimes only the first 2 or 3 labels show strings which is the way it works.
However, the text in the first UILabel always ident by 1 character. See the screenshot below. Horiaontally or vertically, texts in 4 aligned UILabels could not align and text in the first label always ident.
In main storyboard, the 4 UILabels are set to the same height, width, X and Y. Other config settings are alignment to left, content-mode to top, baseline to none.
In my code, I only set text, font(name and size), text color for the UILabels.
I tried to set firstLineIdent to 0 for attributed String of the first label, but it does not work.
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
CGFloat lengthForIndentation = 0.0;
style.firstLineHeadIndent = lengthForIndentation;
NSAttributedString *firstLine = [[NSAttributedString alloc]initWithString:[lines objectAtIndex:0] attributes:#{NSParagraphStyleAttributeName:style}];
self.firstLineBottomVerseLabel.attributedText = firstLine;
I want to know, how to stop the text in the first label from auto indentation and align the texts between labels.
Thanks in advance.
With the reminder of the comment below, I found my mistake in the plist where all strings start with space. I am so sorry to bother you all.

How can I set a multiline UILabel inside UITableCell on Swift 3?

I am trying to set a multiline UILabel, inside an UITableCell.
That UILabel has a proportional width taking as reference the screen of the device (with a multiplier). The text inside of it can change so I need to fit on the UILabel being able to multiline when the width of the text is higher than the space of the UILabel width.
I tried using:
myLabel.lineBreakMode = .byWordWrapping
myLabel.numberOfLines = 0
myLabel.sizeToFit()
but it is always displayed on one line truncating the text that overflows the UILabel width.
How can I make my UILabel to be multiline?
EDIT: I cannot put breaklines to my text because I retrieve my texts from my database.
Thanks in advance!
These two lines are necessary for calculating the cell height automatically. Put these in viewDidLoad.
self.tableView.estimatedRowHeight = 50.0
self.tableView.rowHeight = UITableViewAutomaticDimension
If you want a label to switch from one line to multiple lines, you either have to specify line breaks in the text of the string with \n or newline char, or you can set constraints for the width of the label, or you can set leading and trailing constraints for the label. If one of those methods is complete, the label will go to as many lines as it takes to hit the max number of lines or infinite if set to 0.
Finally I was able to make my multiline UILabel adding this code:
self.tableView.estimatedRowHeight = 50.0
and reducing the multiplier value. It seems that the text could not fit on the height of the UITableCell.

How place big text in UILabel or UITextView

I have UILabel (in UIScrollView) with
self.testlabel.numberOfLines = 0;
self.testlabel.sizeToFit()
and text with 26455 characters, when I set text to label :
self.testlabel.text = content
I don't see my text, but if text is much smaller than 26455 characters then all fine.
So, how to place a big text in XCode?
Check whether the label height constraint is fixed.
Try adjusting the scrollview content size depending on the text height.

Resources