I want to add a UIButton at the end of a sentence in UIView.
Presently I have a UILabel to display text and a button next to it. Both are subviews to UIView. I get the desired o/p if the sentence spans to 2 lines.
How should I modify it, for it to work properly even if the text changes to 3 or 4 lines given that I cannot change the size of the UIView or the UILabel?
It's not really clear to me what you are trying to achieve... anyway maybe with autolayout you could solve your problem:
The button will be placed always on the bottom right corner of the label and the width of the label itself will span from 42 to the max available space...
Related
I have a situation where a button title has two parts with different strings, and need to display one on the right and one on the left. Text will change dynamically every time based on response. I have to place this button in a tableview cell. Attaching an image below for reference.
The requirement is as follows:
.
But I managed to do the following:
I am unable to place text on two different sides of a button.
The easiest solution for you may be to place a UILabel on the left and anchor it to the left with a constraint, then place a UILabel on the right, and anchor it to the right with a constraint. - this is mentioned in the comments by #Alejandro Iván.
I would then choose a set value for the right label width and set that label. Add a constraint from the left label to the right label, so it will dynamically fill as much space as is available.
Last I would place a clear, no text button with the border on top, wrapping both labels. You can then confidently change the left label content and know that it will be encompassed in the button border.
Image attached to help you see what I'm saying although I've left the "button" text and added coloration to demonstrate.
I have a view of limited width, lets say it 100.
I have multiple labels of same width inside that view and its text is centre aligned. So that text seems to be center aligned always.
Problem is when I got long text label it shows ellipses at the end of the text but I want to show whole text in one time while keeping superview width unchanged. Is it possible to attain this? If not then show how can I wrap that text if it is larger than its width. I'm new to IOS programming.
There is an issue with UILabel and autolayout. If you using it - check this answer: iOS: multiline uilabel only shows one line with autolayout
Also, check actual size of label and insure clipsToBound of superview is set to NO.
I have a UILabel that covers most of the view. When I place text in the label, the text is center in the middle of the label. I have tried everything by playing around with the options in the attributes inspector, however the text wont start in the top area of the label, instead it appears in the middle of the label.
Is there any way to places the text in the top part of the label??
(If someones is wondering, yes I want the UILabel to cover most of the view because some texts are longer then others, and some are shorter, and I would prefer the text to be placed on the top part and not the middle of the UIlLabel.)
UILabel has an intrinsicContentSize matching the text contents. If you don't constrain the height, then it will automatically adjust. So you don't need to make it cover the entire view.
Instead, tag it onto the top with a fixed distance. Specify the bottom distance with greater-than-or-equal. This way the label can grow until it reaches the lower limit and then the text will begin to be truncated.
I have a UIButton with the text "Hello".
I'm using autolayout, I didn't set any width or height for the button as I want it to be as big as its content as this one might change.
And I want to add a gap of let's say 10 pts on each edge.
All the topics I have found so far talk about adding some gap by using titleEdgeInsets. I cannot use it as it will just move the title but won't resize the actual button.
Is there an easier way than:
- get the text, calculate the size of its bounding rectangle
- set the button's frame based on the previous size + any gap
- play with titleEdgeInsets
?
If you want to add gaps outside the button then just wrap it inside a custom view, which implements systemLayoutSizeFittingSize function and returns the value returned from UIButton.systemLayoutSizeFittingSize + gap.
To add the gaps inside the button, subclass UIButton and override systemLayoutSizeFittingSize.
I have a cell prototype that i'm trying to add two labels to. I have the two labels right next to each other, but the size of the labels are dynamic, so I want the second label to be able to shift depending on the size of the first label.
Basically, I want there to be a fixed gap between the two labels, but the two labels' sizes are dynamic.
How do I do that?
EDIT:
Actually I found out how to do it via Storyboard. If you select the two labels you want to have a fixed gap between, just command select both of them and then go to the corner of storyboard and click the pin menu, which is that little 'H' looking thing in the group of buttons near zoom in/zoom out in the bottom right corner of the storyboard screen.
Get the label size by this method:
- (CGSize)sizeWithFont:(UIFont *)font
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html.
Then set label's textAlignment to NSTextAlignmentLeft and NSTextAlignmentRight, and set frames by the string size and other offset.
UIView -sizeThatFits: and -sizeToFit will allow you to manually calculate a position for the second label. This is slightly more accurate than using the NSString method, as there's more to a UILabel than just the text — this will respect content insets, etc.