Archive for December, 2012
Adding Custom Fonts in ios
1) Add the .TTF or .OTF font file/s you wish to use as a resource
2) Open your info.plist and create a new key called UIAppFonts(Fonts provided by application), set the key to be an array type.
3) For each font you want to use in your app, add it as a new object in the array with its full name and extension and save the info.plist when finished
4) Now in your code you can simply call
[UIFont fontWithName:@”Insert font name here” size:18]
Scrolling Images in Image Gallery iphone
.h file
#import <UIKit/UIKit.h>
@interface ScrollingImageViewController : UIViewController {
IBOutlet UIScrollView *FirstScrollView;
}
@property (nonatomic, retain) UIView *FirstScrollView;
@end
.m file
#import “ScrollingImageViewController.h”
@implementation ScrollingImageViewController
@synthesize FirstScrollView;
const CGFloat kScrollObjHeight = 353.0;
const CGFloat kScrollObjWidth = 258.0;
const NSUInteger kNumImages = 3;
– (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [FirstScrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[FirstScrollView setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [FirstScrollView bounds].size.height)];
}
– (void)viewDidLoad
{
// 1. setup the scrollview for multiple images and add it to the view controller
//
// note: the following can be done in Interface Builder, but we show this in code for clarity
[FirstScrollView setBackgroundColor:[UIColor blackColor]];
[FirstScrollView setCanCancelContentTouches:NO];
FirstScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
FirstScrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
FirstScrollView.scrollEnabled = YES;
FirstScrollView.pagingEnabled = YES;
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:@”Snap%d.jpg”, i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
[FirstScrollView addSubview:imageView];
[imageView release];
}
[self layoutScrollImages];
}
– (void)dealloc
{
[FirstScrollView release];
[super dealloc];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
in .xib file add a scroll view with (353.0,258.0) frame
Playing video from url in iPhone
.h file
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController : UIViewController
{
MPMoviePlayerController *moviePlayerController;
}
@end
.m file
– (void)viewDidLoad
{
[super viewDidLoad];
NSURL *fileURL = [NSURL URLWithString:@”http://www.w3schools.com/html/movie.mp4″%5D;
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
//for rotation
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
} else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[moviePlayerController.view setFrame:CGRectMake(0, 0, 480, 320)];
}
return YES;
}