How to set label and its dynamic text to center of uiview.
myView = [[UIView alloc] initWithFrame:CGRectMake(0,20,self.view.frame.size.width,32)];
[myView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubView:myView];
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(myView.frame.size.width/2,10,0, 12)];
[myLabel setFont:[UIFont Helvetica12]];
[myLabel setText:[listDiction objectForKey #"dynaimicText"];
[myLabel sizeToFit];
myLabel.center = myView.center;
[myView addSubview:myLabel];
Need your advice on this.
replace your lines with this order,
[myView addSubview:myLabel];
[myLabel sizeToFit];
myLabel.center = myView.center;
but wait, if your text is dynamic then, you've to add, myLabel.numberOfLines = 0; (for multiple line) also if your text size is > myView height then text's will be clipped when it'll increase myView height.
For that, you've to calculate label text size and resize your myView before creating myLabel.
This question Calculating number of lines of dynamic UILabel (iOS7) may help.
Related
My CATextlayer support only 1 line
otherwise the text is cut.
trying to set text content like UILabel Behaviour... is it possible?
set "number of lines"
adjust text size by static CATextLayer frame
CATextLayer *text_layer= [[CATextLayer alloc] init];
[text_layer setBackgroundColor:[UIColor clearColor].CGColor];
[text_layer setBackgroundColor:[UIColor blueColor].CGColor];
[text_layer setForegroundColor:layers.textColor.CGColor];
[text_layer setAlignmentMode:kCAAlignmentCenter];
[text_layer setBorderColor:layers.borderColor.CGColor];
[text_layer setFrame:CGRectMake(0,0,200,50)]; //note: frame must be static
[text_layer setString:#"thank you for your respond"];
text_layer.wrapped = YES;
[text_layer setAlignmentMode:kCAAlignmentCenter];
Your problem is this line right here, [text_layer setFrame:CGRectMake(0,0,200,50)];. I don't think a CATextLayer would lay itself out to accommodate multiple lines. It will only re-draw the text to wrap within the layer's bounds. Try adjusting your text layer's frame based on the text being set. You can create a UILabel instance, to calculate the frame for having multiline text with word wrapping and set it to your CATextLayer instance.
Here's a UILabel category to calculate text size for multiline text with word wrapping:
#interface UILabel (Height)
- (CGSize)sizeForWrappedText;
#end
#implementation UILabel (Height)
- (CGSize)sizeForWrappedText {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.bounds.size.width, CGFLOAT_MAX)];
label.numberOfLines = 0;
label.font = self.font;
label.text = self.text;
[label sizeToFit];
return label.frame.size;
}
#end
Create a UILabel instance, and use the sizeForWrappedText to get the size. Something like this:
// Make sure the someFrame here has the preferred width you want for your text_layer instance.
UILabel *label = [[UILabel alloc] initWithFrame:someFrame];
[label setText:#"My awesome text!"];
CGRect frame = text_layer.frame;
frame.size = [label sizeForWrappedText];
[text_layer setFrame:frame];
I have UILabel in my View that should resize itself based on the content, but it doesn't do so. I have multiline content in it. I tried [label sizeToFit] + label.numberOfLines= 0.
I don't have constraints for the UILabel.
I also tried to use UITextView instead, but there the font just stays on 13 and not as I want on 17. Can you help me?
This is the code that works now:
UILabel *textLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 158, self.view.bounds.size.width-40, 550)];
textLabel2.text = textString;
textLabel2.font = [UIFont fontWithName:#"Helvetica Neue" size:17];
textLabel2.numberOfLines = 0;
[textLabel2 sizeToFit];
[self.scroller addSubview:textLabel2];
If you are creating your UILabel programmatically:
This code first sets the UIlabel's text then get the width of the text
UILabel *label = [[UILabel alloc] init];
//set label backgroundColor so you can see the label width changes according to text
[label setBackgroundColor:[UIColor redColor]];
//edit you label text
[label setText:#"very longgggg text"];
//get width of you text and set you font size to 17.0
CGFloat width = [label.text sizeWithFont:[UIFont systemFontOfSize:17.0f]].width;
//set you label frame
label.frame = CGRectMake(10, 200, width, 30);
//add label to you view
[self.view addSubview:label];
If you're not creating you UILabel programmatically remove the alloc init line and connect you outlets to you xib or storyboard.
I want to resize my UILabel according to the UILabel text. The code I have does not make the
UILabel according to its width..
My current code:
UILabel *textLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 158, self.view.bounds.size.width-40, 550)];
textLabel2.text = textString;
textLabel2.font = [UIFont fontWithName:#"Helvetica Neue" size:17];
textLabel2.numberOfLines = 0;
[textLabel2 sizeToFit];
[self.scroller addSubview:textLabel2];
How can I do this?
Some thing like this?
UILabel *label = [[UILabel alloc] init];
//set label backgroundColor so you can see the label width changes according to text
[label setBackgroundColor:[UIColor redColor]];
//edit you label text
[label setText:#"very longgggg text"];
//get width of you text and set you font size to 17.0
CGFloat width = [label.text sizeWithFont:[UIFont systemFontOfSize:17.0f]].width;
//set you label frame
label.frame = CGRectMake(10, 200, width, 30);
//add label to you view
[self.view addSubview:label];
I have the following code in my XCode project for an iOS app I'm developing:
testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
UIFont *Font = [UIFont fontWithName:#"Arial" size:40];
[testLabel setFont:Font];
[testLabel setTextAlignment:UITextAlignmentCenter];
[testLabel setTextColor:[UIColor colorWithRed:(float) 55/255 green:(float) 41/255 blue:(float) 133/255 alpha:1.0]];
testLabel.text = #"Here We Go";
I am looking to put an image in that spot instead of the text. What do I need to replace this code with?
Either you make an image and put it in an UIImageView or you make a UIView subclass in which you will draw the text inside the drawRect method.
In the second case, in your drawRect you do this :
[self.yourStringProperty drawAtPoint:CGPointMake(100,150) withFont:[UIFont systemFontOfSize:14.0]];
or
[self.yourStringProperty drawInRect:rect withFont:[UIFont systemFontOfSize:14.0]];
Also, look HERE for a detailed explanation of these functions which can also take into account the available width, minimum sizes, line breaks, etc.
The answer above mine is the best with the second part: use a UIView and put either your label or a UIImageView inside it depending on what you want. Here's what it would look like with the image:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(<<your image frame here>>)];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
image.frame = CGRectMake(<<your image frame here>>);
[container addSubview:image];
[self.view addSubview:container];
I have a UIScrollView with an image that needs to be scalable. I want to have a "footer" with a black (opaque) background and white text. I wanted to have it be fixed as a footer. It will be opaque so you can see the image behind it.
I created a containing UIView for the scrollview and footer. I can get the scrollview to be smaller than the app frame and have a footer at the bottom filling in the extra space, but obviously I can't see the image behind the footer.
I also tried putting the UIScrollView and UIView (footer) inside the container and positioning them accordingly, but in this case I can't even see the footer. Any ideas?
Code I've gotten so far (executed in viewDidLoad of view controller):
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView* view = [[UIView alloc] initWithFrame:appFrame];
view.backgroundColor = [UIColor blackColor];
CGRect scrollViewFrame = CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height - 100);
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
[scrollView setCanCancelContentTouches:NO];
scrollView.clipsToBounds = YES;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[scrollView addSubview:imageView];
[scrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
scrollView.minimumZoomScale = 0.7;
scrollView.maximumZoomScale = 5;
scrollView.delegate = self;
[scrollView setScrollEnabled:YES];
UIView* textView = [[UIView alloc] initWithFrame:CGRectMake(0.0, scrollViewFrame.size.height, appFrame.size.width, 100)];
// Red for testing purposes
textView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
[view addSubview:scrollView];
[view addSubview:textView];
self.view = view;
I assume that with "image" you mean that there is an UIImage in the scroll view, and that you want to see it through a half transparent footer view.
In this case you should not put the views beneath each other, but the footer view on top of the scroll view. You can insure it is on top by calling bringSubviewToFront or using [containerView insertSubview:footer atIndex:0]; instead of addSubview:.
Another caveat might be that you call your view view. There might be some unpredictable behavior about which view it is, so perhaps it is better to call it something else and then assign it to self.view.
I have plenty of UIScrollViews with a small non-scroll view at the bottom or the top. In most cases I don't have any overlap, and they just sit side-by-side. I create them all in InterfaceBuilder, and assign them to IBOutlets.