Calling method sizeToFit on a UILabel that has subscripts is not working - objective-c

I have a subclass of UILabel, which is supposed to update its text when the user types something. Naturally, as the length of text increases, the size of the label must adjust to accommodate the text. I called the sizeToFit method, and while the label adjusts its width correctly, the bottom of the text is cut off. The problem is that the text includes subscripts and superscripts , and the label is not adjusting itself with the subscripts in consideration (for example, with H₂O the bottom of the two is cut off).
Can I override sizeToFit or sizeThatFits: to increase the height of the label?
EDIT:
- (void) addCompound {
self.currentLabel = [[FormulaLabel alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
[self addSubview:self.currentLabel];
[self.currentLabel sizeToFit];
// Right now self.currentlabel.text = "". However, I've confirmed thru NSLogging that letters are added to self.currentLabel.text as the user types on the keyboard. Also, the text displays properly (as long as it's within the original frame) when I remove [sel.currentLabel sizeToFit]
}

You should override the UILabel method (CGSize)sizeThatFits:(CGSize)size in your subclass like example below. I just add 10pt to the height calculated by UILabel to accommodate the subscript.
#implementation ESKLabel
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize theSize = [super sizeThatFits:size];
return CGSizeMake(theSize.width, theSize.height + 10);
}
#end
Sample output:
self.eskLabel.text = #"Hello Long² Long\u2082 World";
NSLog(#"CGSize: %#", NSStringFromCGSize(self.eskLabel.frame.size));
[self.eskLabel sizeToFit];
NSLog(#"CGSize: %#", NSStringFromCGSize(self.eskLabel.frame.size));
From the NSLog:
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all Attaching to process 864.
2012-01-06 23:34:21.949 Stackoverflow4[864:f803] CGSize: {85, 61}
2012-01-06 23:34:21.951 Stackoverflow4[864:f803] CGSize: {302, 44}
kill
quit

This should to the trick:
self.eskLabel.adjustsFontSizeToFitWidth = YES;

Related

Not editable NSTextfiled, to make it auto resize when string is too long, like english word to French

As my question being posted, in my textfield, when I open my app in English, it works well, but when switching to French, we know French word is quite long sometimes and in that case, words showing in the textfield will be cut off.
I tried some customised way for textfield on stack overflow, but it works well for editable textfield, when I made it none-editable, the behaviour will be wired.
It's like when the word is too long, it just make textfield longer, meaning just increase width, not height. What I'm expecting is keeping width fix while changing height when word is too long.
intrinsicSize = [super intrinsicContentSize];
NSTextView *textView = (NSTextView *)fieldEditor;
NSRect usedRect = [textView.textContainer.layoutManager usedRectForTextContainer:textView.textContainer];
kind of two main func I used, when it's editable, everything went well and height changes while width fix, but when it's none editable, only width changes.
Any advice?
In your NSTextField subclass override following methods. Also make sure that NSTextField is set to wrap in IB. Add constraints to it.
-(NSSize)intrinsicContentSize
{
// If wrap is not enabled return the original size
if ( ![self.cell wraps] ) {
return [super intrinsicContentSize];
NSRect frame = [self frame];
CGFloat width = frame.size.width;
// This will allow to grow it in height and not width
frame.size.height = CGFLOAT_MAX;
CGFloat height = [self.cell cellSizeForBounds: frame].height;
// return the calculated height
return NSMakeSize(width, height);
}
// Listen to text change notification and invalidate the default size
- (void)textDidChange:(NSNotification *)notification
{
[super textDidChange:notification];
[self invalidateIntrinsicContentSize];
}

Editable transparent NSTextField text appears with white highlight

I am trying to create editable transparent NSTextField in a semi transparent window:
What I have noticed is that whenever the field is editable there is a white "selection like" background drawn even though the element is not actually selected.
Additional observable symptoms:
This highlight is not present when the field is set as non-editable.
If there are multiple fields only the first one has the highlight.
The highlight is not present if the text is not set programmatically
Following code was used to generate the field:
f = [[NSTextField alloc] initWithFrame:b2];
f.backgroundColor = [NSColor clearColor];
f.drawsBackground = YES;
f.bordered = NO;
f.bezeled = NO;
f.focusRingType = NSFocusRingTypeNone;
f.textColor = [NSColor whiteColor];
f.editable = YES;
f.selectable = YES;
f.backgroundColor = [NSColor clearColor];
f.allowsEditingTextAttributes = YES;
f.stringValue = #"Foo";
[self.contentView addSubview:f];
Additional observations (potentially a separate problem):
When field is not the first field on the screen and the initial text is set programmatically and removed by editing the field there is a shadow of the text:
I can't seem to find any documentation on this I wonder if any of you have had this happen and potentially have a solution or a pointer to docs I might have not stumbled upon.
part 1: removing highlight
there are two options here depending on the behavior you are looking for
option 1 - nil first responder
TextField is not first responder
No highlighted text
No Cursor at the end of text
Assuming you are using an NSWindow, set the first responder to nil after calling makeKeyAndOrderFront
[self.window makeKeyAndOrderFront:self];
[self.window makeFirstResponder:nil];
It appears as though makeKeyAndOrderToFront: looks for the first NSResponder in the window willing to accept first responder. Then becomeFirstResponder is called on that responder; leading to option 2
option 2 - override becomeFirstResponder
TextField is first responder
No highlighted text
Cursor appears at the trailing edge of text
Subclass NSTextfield and override it's becomeFirstResponder method
#implementation BPTextField
- (BOOL)becomeFirstResponder {
BOOL isResponder = [super becomeFirstResponder];
//Get Field editor, set selected range
NSText* fieldEditor = [[self window] fieldEditor:YES forObject:self];
[fieldEditor setSelectedRange:NSMakeRange(fieldEditor.string.length ,0)];
return isResponder;
}
#end
I prefer this option from a usability perspective
part 2: removing shadow
option 1 - add a solid background color
I'm not clear ; ) on why this is the case, but if you add a solid background color, the text will update.
option 2 - override textDidChange
override textDidChange:notification in your textfield
#implementation BPTextField
- (void)textDidChange:(NSNotification *)notification {
[super textDidChange:notification];
[self setNeedsDisplay:YES];
}
#end
Final notes
You'll notice that the text looks bad, or rigid. Adding a background color to the textfield, or to the superview's layer will fix this.
This is an answer to part 2 of the question.
The shadow artifact is from rendering window's shadow which is not updated when the text in the NSTextField changes.
If the window's hasShadow method returns "NO" the text's shadow will not create shadow for the text either.

How to find the position of the last text line in a multiline UILabel or otherwise have UILabel have 0 padding

I have a UILabel that has both -numberOfLines set to 3 and text-size auto shrink and I need to align another UIView to this UILabel's last line of text. That is, I might need to align to the y position of line 0, 1 or 2, depending on the text inside the label (and the distance between these lines of text may vary depending on whether the text is long enough that it triggered font resizing).
But:
UILabel doesn't expose a contentSize
the label's bounds extend past the last line of text (there seems to be a content inset), so aligning to the bounds won't work.
subclassing UILabel and doing something like this:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0., 0., -30., 0.};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
just happens to work for the case where I have 3 lines and the font size was auto shrunk, but I still can'r figure out a generic way of subtracting insets for the general case, regardless of text size. And I don't seem to be able to use -boundingRectWithSize:options:context: either: it either returns a single line equivalent rect or, If I play around with the options, a a rect the same size of the original label (that is, including the extra insets I'm trying to get rid of). Mind you, the idea behind removing any insets is that if I have no way of knowing where the last line of text is, at least I can remove any insets in the label so that the last line of text aligns with the label's bounds.origin.y + bounds.size.height.
Any thoughts?
I don't know if the problem was that originally I was using boundingRectWithSize on non-attributed text or what but now this seems to work:
NSString *text = <get text from CoreData>;
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:#{NSFontAttributeName: self.titleLabel.font}];
CGRect rect = [attributedText boundingRectWithSize:self.titleLabel.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
if (!rect.size.height || rect.size.height > self.titleLabel.frame.size.height) {
attributedText = [[NSAttributedString alloc] initWithString:text attributes:#{NSFontAttributeName: [UIFont boldSystemFontOfSize:self.titleLabel.font.pointSize * self.titleLabel.minimumScaleFactor]}];
rect = [attributedText boundingRectWithSize:self.titleLabel.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
}
self.titleLabel.frame = rect;
self.titleLabel.attributedText = attributedText;
While this doesn't really find the position of the bottom of the last line of text in the UILabel (the label still adds some padding at the bottom... not sure if to account for descenders), it adjusts the label's bounds close enough to the bottom that I can at least align based on bounds.origin.y + bounds.size.height and it looks good enough.

Autolayout: how does fittingSize work exactly?

I have created a subclass of NSTextField that changes its height according to the text it contains. I now want to insert it in another view (an NSTableCellView) and make the view resize according to the height of the text field.
I want to use the -(NSSize)fittingSize method of NSView but unfortunately it doesn't seem to call the fittingSize method of its subviews nor their intrinsicContentSize method.
Here is the code I use for the NSTableCellView subclass:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
self.expandingTextField = [[JSExpandingTextField alloc] init];
[self addSubview:self.expandingTextField];
[self removeConstraints:self.constraints];
NSDictionary *row = NSDictionaryOfVariableBindings(expandingTextField);
[self addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|-20-[expandingTextField]-28-|"
options:0 metrics:nil views:row]];
}
return self;
}
- (NSSize)fittingSize
{
return [super fittingSize];
}
I override the fittingSize method here only to put a breakpoint or an NSLog.
Here is the code of the table view delegate that provides the height of the table cell:
- (JSDynamicTableCellView *)dummyCell
{
if (!_dummyCell) {
_dummyCell = [[JSDynamicTableCellView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 100, 100)];
}
return _dummyCell;
}
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
self.dummyCell.expandingTextField.stringValue = #"Test";
NSLog(#"Cell size %#",NSStringFromSize([self.dummyCell fittingSize]));
return [self.dummyCell fittingSize].height;
}
All of this always returns an height for dummyCell of 69 independent of the size of the expanding textfield in the cell.
The question is: how does the 'fittingSize' method figure out the size of its subviews? Should it call their 'fittingSize' or 'ntrinsicContentSize' methods or is it something else?
fittingSize is conceptually simple. It collects all of the constraints that have been added to your view or one of its subviews (recursively), and then it determines the size of the view based on only those constraints. You can think of it as determining the smallest size that is big enough to show the contents of that view hierarchy.
Edit:We need to be quite clear here. fittingSize returns minimum values and will return 0 for any dimension that is not fully specified. For example, if the vertical constraint tying a view to the bottom of its superview is omitted then the fitted height will be 0.
Edit: I just realized what you're probably running into: fittingSize is computing the text field as if it were just one long line, that does not wrap. You can confirm this by giving it a string value with one or more newlines: now the height should be bigger!
So how to fix this? Well, you have a text field, and you give it contents, and you want to know its height. But if the text field wraps, the height is going to depend on the width: make it narrower, and the text field will wrap to more lines, so it will consume more height. So in order to measure the preferred height for a wrapping text field, you have to tell it the width to wrap at.
On OS X Mountain Lion and iOS 6, you can do that with the [NSTextField setPreferredMaxLayoutWidth:] method. For example, if you want to compute the height based on a width of 100, you would call [textField setPreferredMaxLayoutWidth:100]; now fittingSize will report a height for the text field based on it wrapping at a width of 100.
By the way, this is a bad idea:
[self removeConstraints:self.constraints];
Because it removes constraints that other parts of the system have added. You should only ever remove a constraint that you created, either in code or in IB.
Try this (to do this click on the background of the xib or storyboard)

Dynamic UIPopoverController Size?

I have a UIPopoverController in my app which simply displays two UILabels beside each other with a list of words in each of them. However sometimes there are only a couple of words in each list meaning there is tons of blank space in the popover view.
How can I make it so that the popover view in at least height dynamically adapts to how many lines of words there are in my label?
Any help always appreciated, thanks.
If text in label is specified before popover shows, you can achieve this by using similar code in viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
// ...
CGFloat height = [label.text sizeWithFont:label.font
forWidth:label.frame.size.width
lineBreakMode:label.lineBreakMode].height;
// This calculates only height of the label, you may want to add some margins, etc.
CGSize size = CGSizeMake(self.view.frame.size.width, height);
self.contentSizeForViewInPopover = size;
}