Button increase font size / button decrease font size in Xcode - uilabel

How can i add to my application two buttons to increase / decrease font size of a text loaded from a database into a Label?

The solution is to use a UIStepper with the following code :
- (IBAction)ChangeFontSize:(id)sender {
[labelText setFont:[UIFont systemFontOfSize:[fontStep value]]];
}

Related

Dynamically changing font size of text without changing UILabel size

I am developing an application in which I am having fixed size UILabel which may contain text of any size.
I need to resize font size without changing UILabel size.
Following is how I have tried to achieve this:
UILabel *errorMessageLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,50,300,30)];
errorMessageLabel.lineBreakMode =NSLineBreakByTruncatingTail;
errorMessageLabel.numberOfLines = 0;
errorMessageLabel.adjustsFontSizeToFitWidth=YES;
errorMessageLabel.adjustsLetterSpacingToFitWidth=YES;
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0) {
[errorMessageLabel setTextAlignment:NSTextAlignmentCenter];
}
else{
[errorMessageLabel setTextAlignment:UITextAlignmentCenter];
errorMessageLabel.minimumFontSize=5.0;
}
[errorMessageLabel setTag:405];
[errorMessageLabel setFont:[UIFont systemFontOfSize:13]];
[backgroundView addSubview:errorMessageLabel];
Above solution works for iOS 7 but when I try to run this on iOS 6 it changes font size properly but sets text alignment to left ignoring its centre alignment.
Can any one please let me know how I can achieve this? I tried to search form other solution but nothing is working. I cannot change UILabel's size
errorMessageLabel.numberOfLines = 1;
errorMessageLabel.adjustsFontSizeToFitWidth = YES;
errorMessageLabel.minimumFontSize = MIN_FONT_SIZE;

Custom font misplaced in Cocoa

When using non-standard fonts in Cocoa, they are sometimes misplaced compared to standard system fonts. I've created a sample OS X Cocoa Application which illustrates the issue: http://www.fileconvoy.com/dfl.php?id=g2bff79a0dcf64cc4999493513da42fd8dbb9294e1.
It looks like this:
The label and button on left is the default system font, whereas on the right, the font is changed to Al Bayan (shipped with OS X).
I'm developing an application, where the font has to be changed dynamically. If the user changes the font to one with similar behavior like this, I need some technique to change the margin/padding inside the controls, in this case like:
Button: +5px top margin/padding
Label: -5px top margin/padding
How can this be done?
This is especially worse with "Helvetica Neue" which is kinda standard font (in iOS) and likely to play a bigger role in future OS X versions. I'm not aware of a way to adjust individual controls other than changing the font, but I found a way to render text correctly with own computations. Key is here not to use the string height (e.g. for centering or placing the string), but to take the ascender and xHeight into account.
The following code draws the title of an NSTextFieldCell vertically centered, regardless of the font used:
- (NSRect)titleRectForBounds: (NSRect)theRect
{
NSRect titleFrame = [super titleRectForBounds: theRect];
CGRect rect = [self.attributedStringValue boundingRectWithSize: NSMakeSize(FLT_MAX, NSHeight(titleFrame))
options: 0];
titleFrame.origin.y -= NSHeight(rect) - floor(self.font.ascender);
titleFrame.origin.y += floor((titleFrame.size.height - self.font.xHeight) / 2);
return titleFrame;
}
- (void)drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView *)controlView
{
NSRect titleRect = [self titleRectForBounds: cellFrame];
[self.attributedStringValue drawInRect: titleRect];
}
The cell is part of an NSOutlineView and hence flipped. For non-flipped content the solution is probably a bit simpler (haven't tested that).
Use setAttributedTitle: of the NSButtonCell class.
Supply it an NSAttributedString
See the Attributed String Programming Guide for guidance. The font will be part of an NSMutableParagraphStyle defining the text styling.
The standard buttons are doing all of this with predefined styling.

How can I change the font size of a uilabel without changing the font itself?

I'm having an issue where I allow the user to select a font for a label in my project, but then when the user sets the size of that label (using a different button), the label resets to a default font. I'd like to be able to retain the font that the user had applied while still allowing the user to change the font size. Any help is appreciated, thanks!
Here's my code..
-(IBAction)setFont{
[userText setFont:[UIFont fontWithName:#"Arial-BoldMT" size:50.0]];
//I had to add a size when setting the font or else I got an error
}
-(IBAction)setFontSize{
[userText setFont:[UIFont systemFontOfSize:24]];
}
Just use the fontWithSize: method on the label's current font:
- (IBAction)setFontSize {
// Keep the same font but change its size to 24 points.
UIFont *font = userText.font;
userText.font = [font fontWithSize:24];
}
The function [UIFont systemFontOfSize:] will always return default system font. You can just make use of the same function that you call in setFont which is [UIFont fontWithName:size:].

Interface Builder Autolayout and Resizing

So I have two questions about Interface Builder for Xcode:
I'm trying to build an interface with a label on the left and a text field on the right, which have constant spacing between them, but I want the text field to expand horizontally when I resize the window horizontally to keep the spacing. I've added a restraint that keeps the spacing between them equal, but when I move the window, it resizes the label box rather than the text field. I tried pinning the width of the label, but then it stops me from resizing the window.
Is there any way to resize multiple items at the same time? Like if I have 8 labels vertically and I want to size them all to each be an 8th of the window space, how can I do that without just eyeballing it? It would be easy if you could highlight all of them and drag one corner to resize them all, but it wont let me do that.
For your first problem you should uncheck AutoLayout from Utilities Panel -> File Inspector and see what happens. Concerning the second you can create how many labels you want with your desired size directly from code as follows :
Example for 10 labels:
for (int i=0; i<30; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:anyFrame];
[yourView addSubView:label];
[label release]; //if not using ARC
}

How to make height of OHAttributedLabel scale with content height?

I use an OHAttributedLabel called demoLbl for displaying text with formatted areas. This label is laid out with Interface Builder and is connected to a property in my ViewController. After setting the attributedText to the label I want all the text to be displayed in the label.
If I don't resize the label then the text is cropped at the end of the label so the rest of the text is missing.
If I use [demoLbl sizeToFit]; then the height of the label is larger or smaller in height than the text (about 10 point, varying with the text's length) thus giving me blank areas at the bottom of my view (after scrolling) plus the width of the label is increased by about 2 points.
If I calculate the height of the original text (NSString) before putting it in a NSAttributedString and adding it to the label's attributedText property then the calculated height is way too small for setting it as the label's height.
Is there a hack or trick I can apply so that the label's height is adjusted according to the NSAttributedString's height?
PS: To be more specific I wanted to add OHAttributedLabel as a tag but it's not allowed to me yet.
I'm the author of OHattributedLabel.
I made some fixes recently about my computation of the size. Please check it out it will probably solve your issue.
I also added a method named sizeConstrainedToSize:fitRange: in NSAttributedString+Attributes.h that returns the CGSize of a given NSAttributedString (quite the same way UIKit's sizeWithFont:constrainedToSize: works, but for Attributed strings and CoreText and not plain stings an UIKit)
Actually OHAttributedLabel's sizeThatFits: calls this method itself now.
You can see if this category gives you a more reliable height.
https://gist.github.com/1071565
Usage
attrLabel.frame.size.height = [attrLabel.attributedString boundingHeightForWidth:attrLabel.frame.size.width];
I added this code to the implementation of the OHAttributedLabel class:
// Toni Soler - 02/09/2011
// Overridden of the UILabel::sizeToFit method
- (void)sizeToFit
{
// Do not call the standard method of the UILabel class, this resizes the frame incorrectly
//[super sizeToFit];
CGSize constraint = CGSizeMake(self.frame.size.width, 20000.0f);
CGRect frame = self.frame;
frame.size = [self sizeThatFits:constraint];
[self setFrame:frame];
}
// End Toni Soler - 02/09/2011
Thank you Olivier for sharing your code!