Why NSTextField only shows the first word - objective-c

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.

Related

CATextLayer number of lines?

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

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.

Change Text Lable to Image in XCode

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

How to customize the background color of the standard label within a UITableViewCell?

I'm trying for the past several hours to figure out how to do this, but with no luck. There are several potential solutions for this when searching Google, but nothing seems to work.
I'm trying to customize the background color of the standard UILabel that goes in a UITableViewCell (since I already customized the background color of the cell itself), but nothing I do seems to work.
I'm creating my own UILabel to customize the colors in -tableView:cellForRowAtIndexPath:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor blueColor];
label.backgroundColor = [UIColor redColor];
label.opaque = YES;
[cell.contentView addSubview:label];
cell.text = #"Sample text here";
But that doesn't work, and the resulting table view still has a bunch of cells with labels with black text and white background in it.
Any clues on what I am doing wrong here?
UPDATE: If I try to do the following instead:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor blueColor];
label.backgroundColor = [UIColor redColor];
label.opaque = YES;
[cell.contentView addSubview:label];
label.text = #"Sample text here";
I get a bunch of UITableViewCells with no text at all.
It appears that you're assigning the text to the cell instead of the label. You probably want:
label.text = #"Sample text here";
Also, you'll need to set the label's frame to what you require:
label.frame = CGRectMake(10,10, 80, 40);
or directly in the constructor:
label = [[UILabel alloc] initWithFrame:myFrame];
I wouldn't do this in code. I would do it with a custom XIB file and then load it in your
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
delegate function. That way you can design the XIB file to your exact specs, and tweak it.
Good luck!
You have asked this question before, and received correct answers;
How to customize the background color of a UITableViewCell?