UISegmentedControl Font change IOS7 - xcode

I stumbled on a UISegmentedControl and i want to adjust the font size, went over several tutorials but still not able to fix it since all the examples look different of what i have and no font option in the InterfaceBuilder, so how and actually where do i add the font criteria? Thanks for your help. This is what i have:
- (void)valueChanged:(UISegmentedControl *)segment
{
if(segment.selectedSegmentIndex == 0) {
[self getlistInformationbyIdPerson:self.person.id_person withInformationType:1 withCompletion:^{
[self.personConnectedInformationTableView reloadData];
}];
}
else if(segment.selectedSegmentIndex == 1){
[self getlistInformationbyIdPerson:self.person.id_person withInformationType:3 withCompletion:^{
[self.personConnectedInformationTableView reloadData];
}];
}
else if(segment.selectedSegmentIndex == 2){
[self getlistInformationyIdPerson:self.person.id_person withInformationType:2 withCompletion:^{
[self.personConnectedInformationTableView reloadData];
}];
}
}
and i have this one:
- (NSString *)noSegmentedControlDataLabelForSegment:(NSInteger)aSegment andCelebrity:(NSString *)aCelebrity
{
NSString *noDataMessage;
NSString *segmentText
switch (aSegment) {
case 0:
segmentText = #"list1";
break;
case 1:
segmentText = #"list2";
break;
case 2:
segmentText = #"list3";
break;
default:
break;
}
noDataMessage = [NSString stringWithFormat:#"We are sorry to inform you that our dataBase dosen't contain any information"];
return noDataMessage;
}
and this one in the .h
#property(nonatomic,retain) IBOutlet UISegmentedControl *segment;

Please use this code in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
if let font = UIFont(name: "Courier", size: 12.0) {
print("Font Available")
let attributes = [NSFontAttributeName : font]
self.segmentControl.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
}
else {
print("Font not available")
}
}
Where segmentControl is IBOutlet of UISegmentedControl
If you want to use SystemFont then you don't have to unwrap the font value. In that case use below code
let font = UIFont.systemFontOfSize(12.0)
let attributes = [NSFontAttributeName : font]
self.segmentControl.setTitleTextAttributes(attributes, forState: UIControlState.Normal)

UIFont *font = [UIFont boldSystemFontOfSize:8.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
[self.segment setTitleTextAttributes:attributes
forState:UIControlStateNormal];
Adding this to the ViewDidLoad changed the font size for me, thanks for getting me on the right track Inder Kumar :)

Related

NSPopUpButton White text

I have created a NSPopUpButton in my app in a custom view which has a black fill. I would now like to make the NSPopUpButton have a white text color but I do not seem to be able to do this. How should I go about doing this?
Thanks
That's easy. You just need to override the -[NSPopUpButtonCell drawTitle:withFrame:inView], and then can change everything about title, like color, font, frame , etc. Here is a example below. Hope that can help you.
Objective-C:
#interface MyPopUpButtonCell : NSPopUpButtonCell
#property (nonatomic, readwrite, copy) NSColor *textColor;
#end
#implementation MyPopUpButtonCell
- (NSRect)drawTitle:(NSAttributedString*)title withFrame:(NSRect)frame inView:(NSView*)controlView {
NSRect newFrame = NSMakeRect(NSMinX(frame), NSMinY(frame)+2, NSWidth(frame), NSHeight(frame));
if (self.textColor) {
NSMutableAttributedString *newTitle = [[NSMutableAttributedString alloc] initWithAttributedString:title];
NSRange range = NSMakeRange(0, newTitle.length);
[newTitle addAttribute:NSForegroundColorAttributeName value:self.textColor range:range];
return [super drawTitle:newTitle withFrame:newFrame inView:controlView];
}else {
return [super drawTitle:title withFrame:newFrame inView:controlView];
}
}
#end
Swift:
class MyPopUpButtonCell: NSPopUpButtonCell {
var textColor: NSColor? = nil
override
func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) -> NSRect {
if self.textColor != nil {
let attrTitle = NSMutableAttributedString.init(attributedString: title)
let range = NSMakeRange(0, attrTitle.length)
attrTitle.addAttributes([NSAttributedString.Key.foregroundColor : self.textColor!], range: range)
return super.drawTitle(attrTitle, withFrame: frame, in: controlView)
}else {
return super.drawTitle(title, withFrame: frame, in: controlView)
}
}
}
Use -[NSButton setAttributedTitle:], bearing in mind that NSPopUpButton is a subclass of NSButton. Make a mutable copy of the attributed title, set its foreground color to white, and then set that as the new attributed title.
NSMutableAttributedString* myTitle = [[button.attributedTitle mutableCopy] autorelease];
NSRange allRange = NSMakeRange( 0, [myTitle length] );
myTitle addAttribute: NSForegroundColorAttributeName
value: [NSColor whiteColor]
range: allRange];
button.attributedTitle = myTitle;

Cocoa osx NSTableview change row highlight color

In my application I have a view based NSTableView with one column. The highlight color of the rows is set to regular (blue). I need to change that color to my custom color. In the interface builder I tried changing it but the only options are "None, regular and source list".
I tried this post solution with no success:
https://stackoverflow.com/a/9594543/3065901
I read that I have to use this delegate method but I dont know how to use this.
- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
I tried drawing the row in that method but i get invalid context warnings and the row still keeps with the same higlight.
Please post a simple example of how to use this delegate method:
Need help please. Thanks in advance.
From this link.
MyNSTableRowView.h
#import <Cocoa/Cocoa.h>
#interface MyNSTableRowView : NSTableRowView
#end
MyNSTableRowView.m
#import "MyNSTableRowView.h"
#implementation MyNSTableRowView
- (id)init
{
if (!(self = [super init])) return nil;
return self;
}
- (void)drawSelectionInRect:(NSRect)dirtyRect {
if (self.selectionHighlightStyle != NSTableViewSelectionHighlightStyleNone) {
NSRect selectionRect = NSInsetRect(self.bounds, 2.5, 2.5);
[[NSColor colorWithCalibratedWhite:.65 alpha:1.0] setStroke];
[[NSColor colorWithCalibratedWhite:.82 alpha:1.0] setFill];
NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:selectionRect
xRadius:6 yRadius:6];
[selectionPath fill];
[selectionPath stroke];
}
}
#end
AppDelegate.m
- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
MyNSTableRowView *rowView = [[MyNSTableRowView alloc]init];
return rowView;
}
Here is user3065901's answer in Swift 3:
class MyNSTableRowView: NSTableRowView {
override func drawSelection(in dirtyRect: NSRect) {
if self.selectionHighlightStyle != .none {
let selectionRect = NSInsetRect(self.bounds, 2.5, 2.5)
NSColor(calibratedWhite: 0.65, alpha: 1).setStroke()
NSColor(calibratedWhite: 0.82, alpha: 1).setFill()
let selectionPath = NSBezierPath.init(roundedRect: selectionRect, xRadius: 6, yRadius: 6)
selectionPath.fill()
selectionPath.stroke()
}
}
}
NSTableViewDelegate:
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
return MyNSTableRowView()
}
If you are using cell based tableview, set the NSTableView selectionHighLightStyle to None
NSTableView, and override drawRow sample below
- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect {
NSColor* bgColor = Nil;
// Set the color only when its first responder and key window
if (self == [[self window] firstResponder] && [[self window] isMainWindow] && [[self window] isKeyWindow])
{
bgColor = [NSColor brownColor];
}
else
{
bgColor = [NSColor windowBackgroundColor];;
}
NSIndexSet* selectedRowIndexes = [self selectedRowIndexes];
if ([selectedRowIndexes containsIndex:row])
{
[bgColor setFill];
NSRectFill([self rectOfRow:row]);
}
[super drawRow:row clipRect:clipRect];
}
Add below line in your tableview method
ObjectiveC
[yourtableview setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];
Swift
yourtableview.selectionHighlightStyle = .none

UISearchBar background image working weirdly

I'm looking to do a simple one colour search bar like this:
I've succeeded in doing this in my view controller by setting this image as a background image:
and with this code:
[self.searchBar setTranslucent:NO];
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:#"searchbg.png"] forState:UIControlStateNormal];
However when I click on the search bar and it goes into the searchdisplaycontroller, it looks like this:
There's a white section there that I can't get rid off. I've tried doing this:
[self.searchDisplayController.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:#"searchbg.png"] forState:UIControlStateNormal];
But it doesn't seem to work. I'm a bit puzzled as to why they act differently since they're both uisearchbars?
Thanks.
Try setting borderColor and borderWidth for UISearchBar layer it will fix your color change
remember that you have to keep in mind that for ios7 and later view position is different so use as i mentioned below. also i have subclass for UISearchBar, if u want u too create one or replace self by your serachBar
- (void)setup
{
self.placeholder = #"Search Products";
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0")) {
self.backgroundColor = [UIColor lightGrayColor];
UIView *topView = self.subviews[0];
[self searchBarCustomization:topView.subviews];
} else {
self.backgroundImage = [UIImage imageWithColor:[UIColor lightGrayColor]];
self.layer.borderColor = [UIColor lightGrayColor].CGColor;
self.layer.borderWidth = 0.0;
[self searchBarCustomization:self.subviews];
}
}
- (void)searchBarCustomization:(NSArray *)subViews
{
for (UIView *subView in subViews) {
if ([subView isKindOfClass:[UITextField class]]) {
[self customSearchBarField:subView];
}
if ([subView isKindOfClass:[UIButton class]]) {
[self customSearchCancelButton:subView];
}
}
}
- (void)customSearchBarField:(id)searchBarField
{
UITextField *searchField = (UITextField *) searchBarField;
if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0")) {
searchField.background = [UIImage imageWithColor:[UIColor whiteColor]];
searchField.borderStyle = UITextBorderStyleNone;
searchField.layer.cornerRadius = 5.0;
}
}

EDITED: MapKit Annotation callouts. Adjust size of the UIPopoverController

Sorry, I have read a bunch of tutorials how to create a custom Callout for MapKit Annotation. It works with NSLog, but I cannot display the information in the Callouts.
I have two type of icons on the map. This is my viewForAnnotation method:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if (! [annotation isKindOfClass:[IGAMapAnnotation class]]) {
return nil;
}
IGAMapAnnotation *myLocation = (IGAMapAnnotation *) annotation;
self.typeIsFix = [myLocation.navaidType isEqualToString:#"FIX"];
self.typeIsPort = [myLocation.navaidType isEqualToString:#"PORT"];
int planeImageViewTag = 42;
NSString *reuseId;
if (self.typeIsPort)
reuseId = #"IGAMapAnnotationPort";
else if (self.typeIsFix)
reuseId = #"IGAMapAnnotationFix";
else
reuseId = #"IGAMapAnnotationOther";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
annotationView.enabled = YES;
UIButton *annotationInfo = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = annotationInfo;
annotationView.canShowCallout = YES;
if (self.typeIsPort)
{
annotationView.image = [UIImage imageNamed:#"mapPORT.png"];
annotationView.centerOffset = CGPointMake(0, 0);
}
else if (self.typeIsFix)
{
annotationView.image = [UIImage imageNamed:#"mapFIX.png"];
annotationView.centerOffset = CGPointMake(0, 0);
}
else
return nil;
}
else
{
annotationView.annotation = annotation;
}
return annotationView;
}
Then I have a calloutAccessoryControlTapped method
- (void)mapView:(MKMapView *)mapview annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
IGAAnnotationInfoViewController *popOverCallout = [[IGAAnnotationInfoViewController alloc]init];
UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:popOverCallout];
popOver.popoverContentSize = CGSizeMake(300, 200);
[popOver presentPopoverFromRect:view.bounds
inView:view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
I have also created a UIViewController which I assigned to UIPopoverController.
Now, when I tap the button on my annotation I see a white space for text. Great. If I assign text to a label in UIViewController, it also works great (the following is my UIViewController.m):
- (void)viewDidLoad {
[super viewDidLoad];
txtCallout = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 300, 200) ];
txtCallout.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(14.0)];
txtCallout.numberOfLines = 0;
txtCallout.clipsToBounds = YES;
txtCallout.backgroundColor = [UIColor clearColor];
txtCallout.textColor = [UIColor blackColor];
txtCallout.textAlignment = NSTextAlignmentLeft;
txtCallout.text = #"text\ntext\ntext";
[self.view addSubview:txtCallout];
}
But how do I insert the text from my annotation method? Also the text must be different depending on the icon type, say #"PORT, PORT" or #"FIX,FIX". How do I do it?
EDIT:
I have managed to display callouts with the necessary information passed. My last problem is that sometimes my callout is 3 lines, sometimes -- as many as 15. How is it possible to make the callout adjust automatically to the number of lines in my string? Should I modify my popoverContentSize or my label size in the UIViewController?
Thank you so much!
I have figured out how to adjust the MK Annotation callout to a UILabel.
Implement the calloutAccessoryControlTapped method
- (void)mapView:(MKMapView *)mapview annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
// OPTIONAL: Deselecting Annotation View when Callout is tapped
//[mapview deselectAnnotation:view.annotation animated:YES];
NSString *calloutDetails;
IGAMapAnnotation *annotationTapped = (IGAMapAnnotation *)view.annotation;
calloutDetails = [NSString stringWithFormat:#"YOUR TEXT:\nYOURTEXT\n"];
// Declare and initialize the UIViewController that has the label to contain the callout information
IGAAnnotationInfoViewController *detailViewController = [[IGAAnnotationInfoViewController alloc]initWithText:calloutDetails];
UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:detailViewController];
// Size of the UIPopoverController = size of the label + 40 pts
popOver.popoverContentSize = CGSizeMake(detailViewController.txtCallout.frame.size.width+40,detailViewController.txtCallout.frame.size.height+40);
// Show popover controller
[popOver presentPopoverFromRect:view.bounds
inView:view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
Now, IGAAnnotationInfoViewController.h
#interface IGAAnnotationInfoViewController : UIViewController {
CGRect calloutSize;
}
#property (strong,nonatomic) NSString *calloutInformation;
#property (strong,nonatomic) IGACalloutLabel *txtCallout;
-(IGAAnnotationInfoViewController*) initWithText : (NSString*) calloutText;
IGAAnnotationInfoViewController.m
#implementation IGAAnnotationInfoViewController
#synthesize calloutInformation,txtCallout;
-(IGAAnnotationInfoViewController*) initWithText : (NSString*) calloutText {
self = [super init];
if ( self ) {
calloutInformation = calloutText;
// Creating a label that will display the callout information (passed from IGAAcarasViewController - Map Annotation)
txtCallout = [[IGACalloutLabel alloc] initWithFrame:CGRectMake(20, 20, 0, 0)];
txtCallout.lineBreakMode = NSLineBreakByWordWrapping;
txtCallout.numberOfLines=0;
txtCallout.backgroundColor = [UIColor clearColor];
txtCallout.textColor=[UIColor blueColor];
txtCallout.text = calloutInformation;
[txtCallout drawTextInRect:CGRectMake(10,10,0,0)];
[txtCallout sizeToFit];
[self.view addSubview:txtCallout];
}
return self;
}
Finally, subclass the UILabel class:
implementation IGACalloutLabel
#synthesize topInset, leftInset, bottomInset, rightInset;
- (void)drawTextInRect:(CGRect)rect
{
UIEdgeInsets insets = {topInset,leftInset,bottomInset,rightInset};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
Regards,

How to dismiss UIPickerView in iOS7 when I click the screen

I have a question about UIPickerView.
I build the UIPickerView , the picker have popup normal,
but I need dismiss the pickerview when I click the screen.
But When I click the screen , the picker have dismiss, but the backgrouond gray color view(?) still remain.
Like this:
my part code is here
dataAry = [[NSArray alloc] initWithObjects:#"test1",#"test2",#"test3",#"test4",#"test5", nil] ;
pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = true;
_myTF.inputView = pickerView;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideThePicker)];
tapGesture.delegate = self;
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
}
-(IBAction)hideThePicker {
pickerView.frame = CGRectMake(0, 480, 320, 260);
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent:(NSInteger)component
{
return dataAry[row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
return [dataAry count];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
NSLog(#"dataAry selected:%ld", row);
}
Have any one teach me how can I totally dismiss pickerview when I touch the screen?
thank you very much.
my test pickerview totally code in here:
git: https://github.com/dickfala/pickerViewTest.git
Why don't you use -
-(IBAction)hideThePicker {
[_myTF resignFirstResponder];
pickerView.hidden = YES;
}
you can hide the UIPickerView just by using its 'hidden' property.
EDIT-
Check updated answer above, you have to resign the text field responder as well.

Resources