I thought I'd try and reuse the font settings and size class variations in my own view. I have my drawing code within drawRect within a UILabel class. However the size of the font isn't that I've set with my sizing classes.
OK, I know I'm not using the label as intended, but shouldn't this work ?
H
=
IB_DESIGNABLE
#interface TitleBannerView : UILabel
M
=
#implementation TitleBannerView
- (void)drawRect:(CGRect)rect {
NSLog(#"self.font.pointSize=%f", self.font.pointSize);
UIFont* textFont = [UIFont fontWithName:APP_FONT size:self.font.pointSize];
//reuse the font in my drawing code here
//Don't add the label!
//[super drawRect: rect];
Maybe you should try preferredFontDescriptorWithTextStyle which respect the user's selected content size category as described here.
UIFontDescriptor *userFont = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
float userFontSize = [userFont pointSize];
UIFont *font = [UIFont fontWithName:APP_FONT size:userFontSize];
You need to use the system font in IB with all your sizing classed and use this method.
#import <UIKit/UIKit.h>
#interface UILabelEx : UILabel
#end
#import "UILabelEx.h"
#import "Constants.h"
#implementation UILabelEx
- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {
[super traitCollectionDidChange: previousTraitCollection];
self.font = [UIFont fontWithName:APP_FONT size:self.font.pointSize];
}
#end
Related
I am using 5 to 7 UITextField on every UIViewController in my project. I want to set border width for all those. I know the code for changing UITextField border width:
textField.layer.borderWidth=2.0f;
But for each and every UITextField I need to do that. Is there any simple way to achieve that.
You can use the category for making global setting for UITextField like following:
FOR CREATE CATEGORY check this answer: How to give padding to UITextField in iOS?
UITextField+setBorder.h
#import <UIKit/UIKit.h>
#interface UITextField (setBorder)
-(CGRect)textRectForBounds:(CGRect)bounds;
-(CGRect)editingRectForBounds:(CGRect)bounds;
- (void)setBorderForColor:(UIColor *)color
width:(float)width
radius:(float)radius;
#end
UITextField+setBorder.m
#import "UITextField+setBorder.h"
#implementation UITextField (setBorder)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
-(CGRect)textRectForBounds:(CGRect)bounds {
[self setBorderForColor:[UIColor redColor] width:5 radius:0];
return CGRectMake(bounds.origin.x , bounds.origin.y ,
bounds.size.width , bounds.size.height ); // here you can make coercer position change
}
-(CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
- (void)setBorderForColor:(UIColor *)color
width:(float)width
radius:(float)radius
{
self.layer.cornerRadius = radius;
self.layer.masksToBounds = YES;
self.layer.borderColor = [color CGColor];
self.layer.borderWidth = width;
}
OUTPUT IS
U can Assign textField.layer.borderWidth=2.0f; in NSObject class then u can inherit the class whenever u need
A best way to create a class subclass of UITextField and write code for border width and colour like stuff in subclass.And give your textfield's super class to created class.You don't need to write code again and again.
I am trying to have dynamic font size for the textview.I have mad a custom class for the dynamic font size of text view.I have used following code for the custom class.
#import "CustomTextView.h"
#implementation CustomTextView
- (void)drawRect:(CGRect)rect
{
// Drawing code
int numLines = self.contentSize.height / self.font.lineHeight;
self.font = [UIFont fontWithName:self.font.fontName size:((self.frame.size.height / numLines) - 4)];
[super drawRect: rect];
}
#end
but this does not work.it shows a black color for the textview.please guide how to do it?
There must be something wrong while implementing "CustomTextView.h" in your ViewController.
Let us know how u have done the same.
Share your code in ViewController.
Have you added "CustomTextView" as subview in your view controller.
I'm working with and interface built using size classes and autolayout. One of the issues I'm encountering is that I can no longer use a fixed font size for the text fields - less text will be visible on smaller screens.
One of the solutions is to use "Adjust to Fit", however that option only works when there's enough text to overflow the text field horizontally.
What is the correct solution for the font size to use when the UITextField size at runtime is unknown?
Am using a custom class for all labels and am assigning two properties at the time of designing. One is autoFont and another is the fontSize which is currently the font size in iPhone 4 inch xib at design time. Here is the class.
the .h file
#import <UIKit/UIKit.h>
#interface CustomLabel : UILabel
#property (nonatomic) IBInspectable BOOL autoFont;
#property (nonatomic) IBInspectable CGFloat fontSize;
#end
and here is the .m file
#import "CustomLabel.h"
#implementation CustomLabel
#synthesize autoFont;
#synthesize fontSize;
- (void)layoutSubviews
{
[super layoutSubviews];
if (autoFont) {
float newFontSize = [UIScreen mainScreen].bounds.size.height * (fontSize / 568.0);
if ([UIScreen mainScreen].bounds.size.height < 500) {
newFontSize = [UIScreen mainScreen].bounds.size.height * (fontSize / 480.0);
}
self.font = [UIFont fontWithName:self.font.fontName size:newFontSize];
}
}
#end
i think this is the easiest solution for this kind of problem and also i have did this same thing with buttons and text fields by using custom class.
I'm aware of non interface builder soultion.
You get height of your screen and set label's font accordingly.
CGFloat windowHeight = [UIApplication sharedApplication].delegate.window.frame.size.height;
label.font = [UIFont fontWithName:#"Font-Name" size:windowHeight / 10];
Normally, you use:
label.adjustsFontSizeToFitWidth = YES;
to make a label reduce the size of its font whenever it is no longer possible to increase the size of its frame.
Is there any way to make it try an alternate font, like a condensed version of the same font family before it reduces font size?
There are a few good ways to handle this. In IB select your label:
Method 1:
If you click the plus button, you can add specific fonts for a given size class.
In code:
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
if(self.view.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular && self.view.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular)
{
//iPad here
myLbl.Font = [UIFont fontWithName:#"Heyo" size:10];
}
}
The better way, in my opinion, is Method 2:
With this approach, you can either tell the font to shrink up to a certain size, or instead identify a scale factor. Toy with these values until you get the desired result. I also talk about this topic in this tutorial.
In code:
//Minimum font size
[myLbl setMinimumScaleFactor:MIN_FONT_SIZE/[UIFont labelFontSize]];
//Scale factor
[myLbl setMinimumScaleFactor:0.4];
This didn't work for me, but this did.
You also need to use the system font in IB
#import <UIKit/UIKit.h>
#interface UILabelEx : UILabel
#end
#import "UILabelEx.h"
#import "Constants.h"
#implementation UILabelEx
- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {
[super traitCollectionDidChange: previousTraitCollection];
self.font = [UIFont fontWithName:APP_FONT size:self.font.pointSize];
}
#end
I use Storyboards heavily for my iPhone development. To keep the Labels look the same I create custom UILabels and set the font and size in the custom classes. Then in the Storyboard I assign those classes to the labels displayed in the view.
This works fine but I have 4-5 different kind of labels which only differ by size or weight. How do I deal with this situation? Currently I have the following:
PrimaryLabel
PrimaryLabelBold
DescriptionLabel
DescriptionLabelSmall
DescriptionLabelBold
I think this is too much work and these have to be a better way!!
No need to subclass UILabel so many times. Just create one subclass, like so:
MyLabel.h
typedef NS_ENUM(NSUInteger, MyLabelStyle) {
MyLabelStyleSmall,
MyLabelStyleMedium,
MyLabelStyleBig,
};
#interface MyLabel : UILabel
#property (nonatomic) MyLabelStyle style;
#end
MyLabel.m
#import "UILabel+Styles.h"
#implementation UILabel (Styles)
- (void)setStyle:(MyLabelStyle)style
{
switch (style) {
case MyLabelStyleSmall:
self.font = [UIFont systemFontOfSize:12.0];
break;
case MyLabelStyleMedium:
self.font = [UIFont systemFontOfSize:17.0];
break;
case MyLabelStyleBig:
self.font = [UIFont systemFontOfSize:22.0];
break;
default:
self.font = [UIFont systemFontOfSize:17.0];
break;
}
}
#end
In your storyboard, set the style of a particular label using User Defined Runtime Attributes:
2 corresponds to MyLabelStyleBig. Use strings instead of an enum if you want.
You can subclass the UILabel class and than create the constructor method in which you can define the type of label you want ie
PrimaryLabel
PrimaryLabelBold
DescriptionLabel
DescriptionLabelSmall
DescriptionLabelBold
For this you can create an enumeration depecting the types of Label's you want. Within the constructor method you can set this enum value.
Happy coding :)