I want to add a "read more"-button at the end of the textview text. The text is in a UITextView and the button is a UIButton. So first I would like to make the button align vertically with the textview last line.
I'm getting the size of the text views text container in layoutSubviews (this is happening in a UICollectionReusableView):
let containerSize = descriptionTextView.layoutManager.usedRectForTextContainer(descriptionTextView.textContainer).size
showAllButtonHeight.constant = containerSize.height
I'm using Autolayout. I have a constraint (showAllButtonHeight) defining the y-value of the button defined like this:
How can i make the button align perfectly with the text? Or is there a better way of making this kind of "read more"-button?
Related
I have button and a label in my table view cell placing one after other vertically.
In my xib design i placed my button then below it placed my label.
if the string for label is empty then i want to set the button as vertically center in table view cell and hide the label.
if have tried following code for my implementation but button does not come in vertical center position.
if([labelString isEqualToString:#""]){
_label.hidden = YES;
_button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
}
it would be helpful if get suggestion for fixing this problem.
One approach: use dynamic setting of constraints.
In InterfaceBuilder:
Put your button and label inside an enclosing wrapper view.
Constrain the wrapper view to be centered in the main view.
Constrain the button and the label to be of fixed height, and the top of the label to be constrained to the bottom of the button (with spacing if desired).
Constrain the top of the wrapper to the top of the button.
Constrain the bottom of the wrapper with two separate constraints: one to the bottom of the button (but make this inactive), and one to the bottom of the label (active.).
Make both of those constraints outlets so that you can access them in code.
Leave the wrapper height otherwise unconstrained. Thus it will "shrink-wrap" to the combined height of the button and label.
In code:
When the label is empty, activate the constraint that pins the wrapper to the button instead of to the label:
BOOL hideLabel = [labelString isEqualToString:#""];
_label.hidden = hideLabel;
_wrapperToButtonConstraint.active = hideLabel;
_wrapperToLabelConstraint.active = !hideLabel;
Now the shrink-wrap effect will make the wrapper have the same height as the button, and since the wrapper is centered, so will the button be centered.
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
I have a button placed beneath a label and want to make it look like it's a continuation of a label. I think I can achieve this effect by changing the height of the button to match the UILabel's line height.
How do I get the line height of a UILabel?
You can grab the height off of the UIFont:
label.font.lineHeight
I have a label with text:
However, I want it have more padding (and for the edges to be a little rounded if possible). But when I stretch the label in my storyboard so that it appears to have padding, and then I restart the simulator, it doesn't add padding to it.
How would I do this? BTW, I'm using auto layout + objective c
If you want to go the layout constraints route, this is probably the fitting solution for you:
You should use a container view and place the label inside of that. Then add constraints for the label, which will be equivalent to a padding.
Select the label and click the constraints panel at the bottom right in the Storyboard file.
With the label selected, apply layout constraints like seen above (make sure you select the red "lines" so that the constraints are actually applied).
The orange view is the container view. The label is right inside of that.
You can add round corners from storyboard select your UILabel (or any view) and go to inspector, on the identity inspector section add a value as shown in the picture.
You need to override UILabel's drawRect method
Sample code:
#import "OCPaddedLabel.h"
#define PAD 10.0f
#define PAD_VERT 6.0f
#implementation OCPaddedLabel
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {PAD_VERT, PAD, PAD_VERT, PAD};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
if you would like to add rounded corners, you need:
label.layer.cornerRadius = 15.0f;
label.layer.masksToBounds = YES;
To change the padding on a label with a background color where text is aligned left or right, first select the label then in the attributes inspector at the top and change text from Plain to Attributed in the dropdown menu
Select the alignment of the text (in this example it aligns right) click on the 3 dot menu button to the far right to open additional settings and change Text-Direction from Natural to Right To Left
Text in label should now have a bit of padding on the right side
You can add margin with
yourLabel.frame = CGRectMake(0,0,100,100)
I create all my constraints programmatically for my UI controls. So my button has a text: title
So if I don't set any restriction for width of the button, the app will create for me button based on text width, and this works fro me.
But I want to add few extra padding at left and right sides. How can I do it?
Use the contentEdgeInsets property of UIButton, e.g.:
`yourButton.contentEdgeInsets = UIEdgeInsetsMake(0.f, 10.f, 0.f, 10.f);`
will add 10 points to the left and right edges.