CATextLayer number of lines? - objective-c

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];

Related

How to set label and its dynamic text to center of uiview

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.

UILabel won't resize

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.

Resize UILabel according to text

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];

Why NSTextField only shows the first word

When I include an NSTextField to a view, the added data is not completely shown. When the same configuration is applied to an NSTextField added with the .xib the text is shown completely.
Here is the code:
// Generated programmatically
NSTextField *label = [[NSTextField alloc] initWithFrame:self.bounds];
[label setBezeled:NO];
[label setDrawsBackground:NO];
[label setEditable:NO];
[label setSelectable:NO];
label.alignment = NSCenterTextAlignment;
label.backgroundColor = [NSColor clearColor];
label.textColor = [NSColor redColor];
label.font = [NSFont boldSystemFontOfSize:16.f];
[label setStringValue:str];
[self addSubview:label];
NSSize s3 = [str sizeWithAttributes:#{NSFontAttributeName:label.font}];
NSLog(#"S3 size: %f", s3.width);
[label setFrameSize:s3];
And the result is this:
Black text is the label configured in xib file. White text is the label created in xib file and configured programmatically. Red text is the label created and configured programmatically
Any idea?
The code is available here
Using NSString's sizeWithAttributes: only calculates the size of the text string itself. An NSTextField and its NSTextFieldCell also require some room themselves to handle their part of the drawing. So when you set the cell size to the size of the text alone, it's too small for the cell to draw all of the text and so the text is truncated.
Instead of trying to measure the size of the text yourself, you might try:
[label setStringValue:str];
[label sizeToFit];
[label setNeedsDisplay:YES];
NSTextField inherits NSControl's sizeToFit method which will automatically resize the text field to the appropriate size.

UITextField Not Allowing Input

I am programatically adding a UITextField into a UIImageView. When I set the text property, I can see the text.
The issue is, it's not letting me input text into it like an input field when I run the app.
This is my code for initializing the TextField:
UITextField *textField = [[UITextField alloc] init];
textField.frame = CGRectMake(38.0f, 0.0f, self.view.frame.size.width - 38.0f, 40.0f);
textField.text = #"Hello";
textField.background = [[UIImage imageNamed: #"field.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:19];
textField.backgroundColor = [UIColor whiteColor];
textField.userInteractionEnabled = YES;
[bar addSubview: textField];
Do I need to do something special to allow for text input?
If bar is an UIImageView, try to set
bar.userInteractionEnabled = TRUE;
Text Field not added into imageview . You can add textfield on view,for user interaction enabled.
First you can add imageview and then add text field on the view.
Like This--
[self.view addSubview:imgV];
[self.view addSubview: textField];