Hi have some product description, and data coming from the API,i want to show the data as dynamically. Dependents on this height i need to add some other UIElements please help me
Thanks in Advance
UILabel and UITextView has a method: "sizeThatFits", it can return the size fit the text base on a given size, so all you need to do is:
UITextView *textView = [[UITextView alloc] init];
textView.text = yourContent;
CGSize fitSize = [textView sizeThatFits:CGSizeMake(contentWidth, 10000)];
CGFloat contentHeight = fitSize.height;
Try this
UITextView *textView = [[UITextView alloc] initWithFrame:<set frame>];
textView.text = <text>
[textView sizeToFit]; // calls sizeThatFits: with current view bounds and changes bounds size.
If you don't need text editing and aim code purity, you can use UILabel instead of UITextView, just use AutoLayout and set constraints as shown below. Set Lines to 0 for Expanding Label. If you do everything right, resizing and offsetting of controls will occur automatically, you just can change text in Expanding Label
Related
How get UITextView contentView height, if Iam using sizeToFit property ?
if you are using,
int numLines = txtview.contentSize.height / txtview.font.lineHeight;
to get the number of lines in the textView then you don't need sizeToFit or not required to set textView's frame as per content size.
txtview.contentSize.height will give you content view's height and you can get number of lines.
But make sure that you are doing this in viewDidAppear(or any where after your view is appeared) not in viewDidload because in viewDidload your textview is not loaded completely.
UITextView itself has a function called sizeThatFits: which will return the smallest size needed to display all contents of the UITextView inside a bounding box, that you can specify.
The following will work equally for both iOS 7 and older versions and as of right now does not include any methods, that are deprecated.
- (CGFloat)textViewHeightForAttributedText: (NSAttributedString*)text andWidth: (CGFloat)width {
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText : text];
CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;
}
Reference link UITableViewCell with UITextView height in iOS 7?
I config a textView in a UIViewController like following:
textView configuration
but when controller viewDidAppear I found that UITextView's contentSize = {375, 242} and UITextView can not scroll.
But if i tap the textView, let the textView begin editing (but edit nothing), then i touch the controller's view let textView endEditing, log the textView, this time contentSize = {375, 361} and UITextView can scroll.
Is anybody know why? Thanks.
You can add textView something like,
UITextView *standardTextView = [[UITextView alloc]initWithFrame:CGRectMake(20, 50, self.view.frame.size.width - 40, 120)];
standardTextView.layer.borderWidth = 1.0;
standardTextView.layer.borderColor = [[UIColor lightGrayColor]CGColor];
standardTextView.delegate = self;
standardTextView.font = [UIFont fontWithName:#"Helvetica" size:17.0];
[self.view addSubview:standardTextView];
then when your content(i.e text) will be bigger than textview's height it will enable scroll otherwise it remains disable!!
NSTextContainer has a property called heightTracksTextView which may be interfering with your setup here, as the default is false.
I would double check the height of the NSTextContainer after you initialize the UITextView and after you add the UITextView to the view hierarchy.
Check the documentation here: https://developer.apple.com/reference/uikit/nstextcontainer/1444559-heighttrackstextview
If that doesn't help, let me know, I know I've solved this issue before, but I'm not at my computer right now.
I am finding it surprisingly hard to resize a label containing newlines based on the quantity of lines and text. It displays fine in a large enough textview. However, I'd like the economy of sizing the label--or I'd be happy with resizing a textview--exactly.
This is the code I am using from an answer on SO but it is having no effect on the size of the label. Would appreciate any suggestions on how to make this work:
NSString *list = self.list.list;
// use font information from the UILabel to calculate the size
UITextView *view=[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 280, 10)];
//make random size view
view.text=list;
CGSize size=[view sizeThatFits:CGSizeMake(280, CGFLOAT_MAX)];
// create a frame that is filled with the UILabel frame data
CGRect newFrame = _listLabel.frame;
// resizing the frame to calculated size
newFrame.size.height = size.height;
// put calculated frame into UILabel frame
_listLabel.frame = newFrame;
Why are you setting the frame of your label with reference of a newly created UITextView, it will create a useless object in your memory, to set the label frame according to your text just use this 2 line of code
lbl.numberOfLines=0;
[lbl sizeToFit];
It will make the label as large as your text.
You really should use autolayout.
Just constrain the label where you need and let UIKit do it's job.
Here an example:
I set a top space and a leading margin constraints
Then I added a width constraint and then I added some more text
As you can see the label resized itself as it knows how much text it has inside and how much space it occupies.
I am using UITextView to display the NSAttributedString (Which Contains NSTextAttachment and HTML tables using NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType).
When I scrolling the UITextView at half of the screen the texts are disappearing.
Can any one explain how it happens? and how to resolve it?
UITextView *textView = [[UITextView alloc]init];
textView.frame = CGRectMake(0, 0, 768, 1024);
textView.editable = NO;
textView.selectable = NO;
textView.attributedText = attributedString; //My AttributedSting
[self.view addSubview:textView];
This is probably because of incorrect size of the textview frame. Change the background colour of the textview to see if the frame has been set appropriately.
Try making sure no constraints are created automatically by adding:
[textView setTranslatesAutoresizingMaskIntoConstraints:NO];
Ideally you would be best to use constraints so that the text view is pinned on its leading edge and trailing edge to the containing view and on its bottom edge and top edge to the bottom layout guide and top layout guide respectively.
I have used UILabel in my app. It is working properly in portrait mode. But when I open my app in landscape mode it shows content in center of UIlabel. I have tried sizeToFit but it is not working. As soon as I increase uilabel's width spacing starts to arrive in uilabel.
My code:
self.contentLabel.text = labeltext;
[self.contentLabel setNumberOfLines:0];
[self.contentLabel sizeToFit];
I suspect your UILabel itself and not the text within it is actually aligning incorrectly upon rotation. Make sure the label stays aligned to the top of the view. Try:
self.contentLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
or
self.contentLabel.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
Edit: One other thought.
If auto layout's enabled and you're calling sizeToFit in viewDidLoad, perhaps the view is resizing after auto layout lays out the subviews. Try putting your code in viewDidLayoutSubviews instead.
If you add a UILabel with height bigger than the height of the text, it's normal if that happened and there is no way to change this alignement (Vertical center).
I have two solutions for this problem:
Work with constraint :
This constraint Greater than or equal is just magic.
If you create the label with the code I suggest to work with that:
UIFont *fontReceive = [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGSize sizeText = [text sizeWithFont:fontReceive constrainedToSize:CGSizeMake(260, 9999) lineBreakMode:NSLineBreakByWordWrapping];
Hope that will help!
The best solution is to change the height of your cell according the the amount of text your are populating it with.
Please see this code below as an example.
NSString *content = **YOUR_STRING_LABEL_INTENDED_CONTENT**
CGSize maximumLabelSize = CGSizeMake(390, 1000); //For example - Put in your desired label width here and maximum size etc.
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey: NSFontAttributeName]; //This allows a calculation to be made of the space taken up, so if you're using a custom or large font it will calculate accordingly.
CGSize newExpectedLabelSize = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;
Now you can change the height of your label by using this next line, where label is the name of the label you made.
GCRect frame = label.frame;
frame.size.height = newExpectedLabelSize.height;
label.frame = frame;
I hope this helps, cheers, Jim.