Objective C - IB - Vertically centering 3 multiline UILabels - ios

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;

Related

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?

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.

UITextField rightView overlaps right align text

I have a subclass of UITextField. In my instance the text is right align. I have rightView to show money sign at the end, so when user enters data the sign for money will be always visible. I decided that this solution is better than to take text and manually add money sign after each text change.
My problem is that the text and the rightView slightly overlap each other. How can I fix that?
Calculate width according to text and font size. Use this width as max width and than arrange money sign view.
Below is method to calculate max width for text.
- (CGFloat) widthWithFont:(NSString *)string font:(UIFont *)font {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width;
}

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.

Only some UIButtons work with customisation

I'm currently developing an iPad application and want to apply a custom font to the UIButtons on a certain screen. I have noticed similar problems with other screens, namely that the text on some (seemingly random) UIButtons disappears. In this case, the custom font is being applied to some buttons but not others, again there doesn't seem to be any pattern as to which buttons work and which don't. I've attached a screenshot below to try to give you an idea of what exactly I mean.
As I've mentioned, on some other screens I have noticed text completely disappearing from some buttons and have had to replace these with images featuring the text instead.
All buttons are created in Interface Builder. They use attributed text to allow multiple lines and centred alignment. Any help would be much appreciated.
edit - my code is like the following:
for (UIView *sub in view.subviews) {
UIButton *btn = (UIButton *) sub;
UILabel *lbl = [btn titleLabel];
[lbl setFont: myFont size: mySize];
}
To use attributed insure the IB items are set to use attributed text, not plain text.
To set the attributed title for a NSButton use:
- (NSAttributedString *)attributedTitleForState:(UIControlState)state
for a NSLabel use:
#property(nonatomic,copy) NSAttributedString *attributedText
Of course you may not need attributed text is all you are doing is just setting a font and alignment.
For multiple lines of text set:
#property(nonatomic) NSInteger numberOfLines
as appropriate.
From the comments:
You can achieve centered and multiline title labels in UIButtons without using NSAttributedStrings by adding the following lines to your for loop:
lbl.textAlignment = UITextAlignmentCenter;
lbl.numberOfLines = 0;

Resources