Showing posts with label Interface builder. Show all posts
Showing posts with label Interface builder. Show all posts

Monday, 6 October 2014

How to change UISegmentedControl text font and color

UISegmentedControl is mainly used to toggle between two screen or functionality from a common screen. but some times it requires to change the font size of the UISegmentedControl text. Interface builder is not providing the option to change the text font size.

Let me share the way how to change UISegmentedControl text font size and text color.

Create a new viewcontroller in your project or may create a new project if you dont want to modify your existing project for safety point of view.

I am creating UIsegmented control using code but if you using IBOutlet you can go with that also.

in .h file

@interface ViewController : UIViewController

@property (strong, nonatomic)  UISegmentedControl *segment1;

@property (strong, nonatomic)  UISegmentedControl *segment2;
@end

in .m file

 - (void)viewDidLoad
{
    [super viewDidLoad];
   
     NSMutableArray *segtitleArr=[[NSMutableArray alloc]initWithObjects:@"First1",@"Second2", nil];
    _segment1 = [[UISegmentedControl alloc]initWithItems:segtitleArr];
    _segment1.frame = CGRectMake(30, 50, 252, 29);
   
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIFont fontWithName:@"Helvetica" size:17], NSFontAttributeName,
                                [UIColor redColor], NSForegroundColorAttributeName, nil];

  
    [_segment setTitleTextAttributes:attributes forState:UIControlStateNormal];
  
    [self.view addSubview:_segment];



//creating second segment.
  
    NSMutableArray *segtitleArr2=[[NSMutableArray alloc]initWithObjects:@"Third3",@"Fourth"4, nil];
    _segment2 = [[UISegmentedControl alloc]initWithItems:segtitleArr2];
    _segment2.frame = CGRectMake(30, 152, 252, 29);
    _segment2.tintColor = [UIColor colorWithRed: 8/255.0 green:83/255.0 blue:131/255.0 alpha:1.0];
  
    NSDictionary *attributes2 = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIFont fontWithName:@"Helvetica" size:10], NSFontAttributeName,
                                [UIColor orangeColor], NSForegroundColorAttributeName, nil];

  
    [_segment2 setTitleTextAttributes:attributes2 forState:UIControlStateNormal];
  
    [self.view addSubview:_segment2];




Best way to change UISegmentedControl text colr and  size



Above code will show you two segment control in different text size and font color and selected segmented color change using tint color property.
In both UISegmented control one thing is common that is a NSDictionary having objects of Font/Text and color these you can change as per your requirement.

If you find another best way to change UISegmentedControl  text  size and color, Please share using your comments below.

 

  
}

Friday, 3 October 2014

How to use User Defined Runtime Attributes in Xcode Interface Builder

What is the mean by “User Defined Runtime Attributes” ?

“User Defined Runtime Attributes” are featured from Xcode 4 and iOS 5,  great feature supported in interface builder. But bad thing is that most of the intermediate developers are not aware about it.

Where can I found in Interface builder ?
take a look under Identity Inspector tab.


User Defined Runtime Atrributes screen in interface builder




Key Path- Its like properties of controls and or setter methods.
Value- Assigned values to those properties and or setter methods.
What about the 'Type' ?

Below are the supported Attributes types 


User Defined Runtime Atrributes Supported types

    

Lets come to the point.

How can we  use these in our application.
It is not possible to set a View's CornerRadius or borderWidth using interface builder's Attributes Inspector. To achieve this we unnecessarily creates a IBOutlet and use CALayer for this.

like

    _myBGview.layer.borderWidth = 3;
    _myBGviewlayer.cornerRadius = 15;


Lets do the same using Interface builder

Configure User Defined Runtime attributes in interface builder


Wow its so simple Just fill out the objects property and select type and set the value

One problem is here that I required to set the color of the border but CALayer's color is CGColorRef type not UIColor type

So we can use a trick of Objective-C's Category. Just implement a setter method.

@implementation CALayer (Enhancement)

-(void)setMycolorFromcolor:(UIColor*)mycolor
{
     self.borderColor = mycolor.CGColor;
}




Configure User Defined Runtime attributes of unsupported types in interface builder





 Its all about Using User Defined Runtime Attributes from my side. If  you have any other best way to configure User Defined Runtime Attributes in Interface builder
 Please share using comments section.
It would be much appreciated.