Resize UIFont Size - resize

I'm using the OHAttributedLabel as an UILabel replacement.
I'd like to calculate the size of my UIFont relying on the length of the text and the height of the OHAttributedLabel.
label.adjustsFontSizeToFitWidth = YES; doesn't work, OHAttributedLabel doesn't support it and I don't know how I could fix it.
Is there any way to calculate the size of the font?
Thanks in advance,
Nicolai

A potentially simple solution is to use the TTTAttributedLabel library instead of OHAttributedLabel. It supports the adjustsFontSizeToFitWidth property as long as the number of lines is set to anything greater than 0.
If you need something from OHAttributedLabel that TTTAttributedLabel doesn't provide, perhaps the source can give you some ideas on how to implement it.

Related

Autoresizing NSTextView and it's Font Size

I'm trying to make my NSWindow autoresizable. I've gotten most of my UI items to resize, but one of the few remaining objects that pose issues are NSTextViews: indeed, I can't find a way to calculate the new font size once the NSTextView has been resized.
For NSTextFields, I had found a method that would find the font size based on the length of the text. Apparently, there doesn't seem to be an equivalent method for multi-line text containers (unless I just haven't found it).
The only actual place I found that mentionned something of the sort is here: http://boutcher.tumblr.com/post/7842216960/nstextview-auto-resizing-text
However, I wasn't able to implement this code into my application, as there seems to be an error I can't fix with the way an NSLayoutManager is created.
Has anyone done this in the past? I'm considering just allowing the user to resize to just one size, so I can hardcode the font size... It's a real pain dealing with these NSTextViews !
See the sizeWithAttributes: method in “NSString Application Kit Additions Reference.”
It returns an NSSize, which you can compare to the textview’s current frame.size.
For the “Attributes” arg make an NSDictionary with an NSFont as the object and NSFontAttributeName as the key. (Don't be confused by that constant. It looks like it's a key for a string, but it is not; it is a key for the font itself.)
Get the string from the textview: [[yourTextView textStorage] string].
Get the familyName of the font you are using and its current point size, a CGFloat. Compose fonts to test using the constructor [NSFont fontWithName:familyName size:floatChanged].
Once you've arrived at the correctly sized font, use it to make a new NSAttributedString out of the old string. Just use the "attributes" dictionary you made above (the one that produced the correct size) and feed it to NSAttributedString's initWithString:attributes constructor.
Assign that attributed string to the textStorage (itself a subclass of NSMutableAttributedString): [[yourTextView textStorage] setAttributedString:thatYouJustMade].
Note: If the textview employs attributes like underlining or fore/background coloring, or italicized/bold fonts, you must get that information from the textStorage and incorporate it into the new attributed string. It's doable. Look at the class refs for NSAttributedString and NSMutableAttributedString.

NSTableView setRowSizeStyle

I guess setRowSizeStyle is supposed to change the font size of my NSTableView, and make everything smaller.
Is this correct ?
- If so, of you know an alternative method for Leopard ? (prev OSX 10.7)
If it is not correct, what could I use instead ?
thanks
No, setRowSizeStyle method is used to define the size of the rows in the table view. for font size refer https://stackoverflow.com/q/8257631/944634.

UILabel get CGRect for substring of text

My objective is simple: From a UILabel get the rect of a substring in the text of the label. From extensive searching there doesn't appear to be anything built in to handle this. Other people have asked similar questions, but none have been answered fully here on Stackoverflow.
// Something like this perhaps (added as a category)
CGRect rect = [myLabel rectForRange:NSMakeRange(3, 5)];
An example of what it could be used for (just to clarify what I'm searching for):
This was mostly needed for one character rects, so I went crazy and wrote some code that could accurately calculate the rect for the most basic UILabel. Standard UILineBreakMode and text alignment.
I was hoping that if I release it to the public people code contribute to it and improve it, especially since I don't know so much about text rendering!
The code:
https://gist.github.com/1278483
You're right: this isn't a built-in part of UILabel.
If you really need this, subclass UILabel, use CoreText to implement -drawRect:, and you'll be able to compute the rect for a range via CTLineGetOffsetForStringIndex(), CTFrameGetLineOrigins(), and CTLineGetTypographicBounds(). If the text in the range gets laid out so as to cross a line break, this will get complicated; you'll likely want multiple rects.

Memory management on 500 labels

I have a view with several hundred individual UILabels on it. It works, but it is clearly a bog on the system. The app is sluggish until another method releases this view with all the labels.
However, I know that it is necessary often to put this amount of text on a screen, so I'm wondering how is a better way to do it. The labels are each placed in a specific location based on other parameters, so it cannot just be one big textview.
I am guessing the better way to do this is to "draw" the text on the screen using an Open GL Font or something like that. Any other suggestions or advice?
You can also check Additions to NSString: NSString UIKit Additions Sample usage:
- (void)drawRect:(CGRect)rect
{
NSString *temperature = [NSString stringWithFormat:#"%#\n%d / %d", weatherItem.place, high, low];
[[UIColor blackColor] set];
[temperature drawInRect:CGRectMake(15.0, 5.0, 50.0, 40.0) withFont:[UIFont systemFontOfSize:11.0]];
}
Use Core Graphics to draw the text onto the CGContext in your drawRect method. See the Drawing Text section of the CGContext docs

Using Interface Builder's System Media files in code

I have a question regarding the several images and sound files in the Media tab of IB's library: how can they be used in code?
I want change the image of a NSImageVew to NSRevealFreestandingTemplate, but how can I access it? I'm pretty sure there must be a better way than creating a hidden image view and pulling it from there. Any ideas?
Thank you!
Use NSImage's imageNamed: method to get system images. You can use any of the values found in NSImage's Constants section as the name. To get the image you specified, you would use:
[NSImage imageNamed: NSImageNameRevealFreestandingTemplate];
[imageView setImage:[NSImage imageNamed:#"NSRevealFreestandingTemplate"]];