UITextField - UIView Resize Programmatically - ios

Am programmatically creating a UIView, setting the frame for the view, then adding a UITextField inside the view. I've been stuck on this one for a while, I'm trying to get the whole UIView to resize based on the amount of words in the text field if that makes sense?
Thanks,
Declan

Assuming you have a given width, you can use sizeThatFits:
Here below is code I use for UITextViews. Similar code can be used for UITextFields.
Example:
UITextView *myTextView = [[UITextView alloc] init];
[myTextView setAttributedText:myAttributedString];
CGSize size = [myTextView sizeThatFits:CGSizeMake(width, FLT_MAX)];
CGRect frame = CGRectMake(0, 0, width, size.height);
myTextView.frame = frame;

Related

Adding a UILabel as a subview on top of a UITextView

I am trying to make a fake placeholder for a UITextView with a UILabel. Let's say there's a UITextView inside ViewController and this is what I do:
CGFloat frameWidth = self.postInputField.frame.size.width;
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
textViewPlaceholder.text = POST_PLACEHOLDER;
textViewPlaceholder.textColor = [UIColor lightGrayColor];
[self.view addSubview:textViewPlaceholder];
[super viewWillAppear:animated];
}
And there's an unnecessary margin on top of the UILabel as shown below:
I could set the y value to be negative something but I want to make sure why this is happening. One thing that I do when the view controller is loaded is self.automaticallyAdjustsScrollViewInsets = NO; to get rid of the margin inside the UITextView, but it shouldn't matter.
The exact x y values that work are (4, -16) for the UILabel.
I think the problem is the height of UILabel:
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
The height is too large. Try to reduce it. And using background color to see more.

How to overrride mediaViewDisplaySize for a custom media item in JSQMessagesViewController?

I want to customize the size of a custom media item in JSQMessagesViewController. I am adding a label to a UIView and trying to set the size by setting the frame size of my container view.
UIView* containerView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 100) ]
containerView.frame = CGRectMake(0.0f, 0.0f, 315.0f, 100.0f);
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 50, 50)];
label.text = #"vendor name";
[containerView addSubview:label];
Setting frame size works for image view but now UIView. Even then, there is a white space for unfilled content in case of image view.
Also, I can resize the placeholder for all the views by mediaViewDisplaySize but then I cannot customize each subclass differently.
How do I selectively set the size for placeholderview depending on the subclass?
In your mediaItem subclass, you'll need to override mediaViewDisplaySize. If you need different sizes for each type of mediaItem, then subclassing each will be easiest.

UIScrollView not scrollable using autolayout and dynamic constraints

I have a view which contains a scrollview, a containerview and 3 subviews (topview, textview and bottomview). See the view's hierachy below. The height of the textview is defined based on its content. I defined a dynamic constraint for its height and I change it in viewDidLayoutSubviews. The size of the textview is correct but the problem is that my scrollview is not scrolling. What I am doing wrong? Perhaps I need to add/modify some of the other constraints?
-(void)viewDidLayoutSubviews
{
NSAttributedString * string = [[NSAttributedString alloc] initWithString:self.game.description];
CGFloat heightTV = [self textViewHeightForAttributedText:string andWidth:260];
self.dynamicTVHeight.constant = heightTV;
[self.view layoutIfNeeded];
}
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
UITextView *textView = [[UITextView alloc] init];
[textView setAttributedText:text];
CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;
}
The UIScrollView scroll only if it has contentSize bigger than bounds. Content size of UIScrollView can be set automatically by calculating frames of subviews (calculated from constraints) or manually in code (usually in viewDidLoad).
Double check your UIScrollView's contentSize.

UITextView within UIScrollView - Dynamic Heights?

I have a UIView which contains a UIScroller - which in turn contains a couple of labels, one image and a dynamic text view.
The text view can contain anything from a few characters to 2000 words - how can I automatically apply the relevant heights to the UITextView and the parent scroller?
I have got as far as the following -
//Set Scroller
[self.scrollerArticle setScrollEnabled:YES];
[self.scrollerArticle setContentSize:CGSizeMake(320, 750)];
[self.articleUITextView sizeToFit];
[self.articleUITextView layoutIfNeeded];
But don't know how to apply the sizetofit method to the scroller?
Try like this,
[self.scrollerArticle setContentSize:CGSizeMake(320, CGRectGetMaxY(self.articleUITextView.frame))];
UITextView is a subclass of UIScrollView and you should use content size after you set up a text to get the size:
self.articleUITextView.text = #"YOUR TEXT";
CGRect rec = self.articleUITextView.frame;
rec.size.height = self.articleUITextView.contentSize.height;
textView.frame = rec;
After that you can just chance contentSize of your scroll view.
If you want to use sizeWithFont: it works fine for UILabels but not necessary with UITextView.
You can get the height of the content using the below method.
-(CGFloat)getContentHeight{
NSString *aMessage = #"My Name is ABC"; // Can be anything between 0 to 2000 chars
CGSize maximumSize = CGSizeMake(320, 9999);
// Use the font you are willing to use for TexTView.
UIFont *textFont = [UIFont fontWithName:#"Helvetica-Bold" size:MessageFontSize];
CGSize myStringSize = [aMessage sizeWithFont:textFont
constrainedToSize:maximumSize
lineBreakMode:NSLineBreakByWordWrapping];
return myStringSize.height;
}
And then can set the ContentSize of Scrollview accordingly.

How to keep the text of cell label at top when cell's height increases?

I am increasing cell's height in the method heightForRowAtIndexPath, as a result of it, the label is coming centered.
How do make sure that label is still at top with the text irrespective of the height of the row?
While allocating UITableViewCell, you need to do like this..
UITableViewCell * cell;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:string];
At the time of initialization with style: UITableViewCellStyleSubtitle will come.
Hope this will work..
You can do it in two ways.
Create a custom label and set it's frame.
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
[cell.contentView addSubView:lbl];
Use a UITableViewCell subclass and override -layoutSubviews. In this method, you'll want to call [super layoutSubviews].
- (void)layoutSubviews {
[super layoutSubviews];
CGSize size = self.bounds.size;
CGRect frame = CGRectMake(0.0f, 0.0f, size.width, size.height);
self.textLabel.frame = frame;
self.textLabel.contentMode = UIViewContentModeScaleAspectFit;
}
Write Following Code:
UILabel *myLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, height)];
[cell.contentView addSubView: myLable];
If Also You want myLable fram is similare to myLable.text then write..
[myLable sizeToFit];
You cannot change the position of textlabel as it is auto-adjusted.
First option is you need to subclass UITableViewCell and customize textLabel frame.
Another is that you create your own custom label and make textlabel nil. So whenever cell's height will change, your label position will not change.
Set the autolayout Top property of UILabel.
try this:
set False to "Autoresize Subviews" property of your custom cell...

Resources