iphone简单实例 (字体,弹出窗口) (实例)
1. UIFont 可以支持的字体预览
?
为iPhone上到底支持哪些字体而发愁吗???为光看字体名称而不知道其长得 是啥样子而发愁吗?以下demo可以帮到你.
?
帖子地址?http://www.cocoachina.com/bbs/read.php?tid-19894.html
?
下载见附件:FontTest.zip
?
?
IPhone 所支持的字体:NSArray *familyNames = [UIFont familyNames]; for( NSString *familyName in familyNames ){ printf( "Family: %s \n", [familyName UTF8String] ); NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName]; for( NSString *fontName in fontNames ){ printf( "\tFont: %s \n", [fontName UTF8String] ); } }?正式用法:
UIFont *tFont = [UIFont fontWithName:[[UIFont fontNamesForFamilyName:@"Helvetica"] objectAtIndex:N] size:17]; [textLabel setFont:tFont];
?
?
?
?
2. iPhone 弹出框代码例子
?
这个 iPhone 弹出框代码例子由 CocoaChina 会员 “sunmingze198” 分享,效果类似 iOS 系统自带的 WiFi 选择弹出框。
?
下载见附件:popUpDemo.zip
?
?
UIAlertView 这个元件并不常用,如果将UIAlertView 用作显示普通讯息,这不是一个好的介面设计,因为弹出来的讯息是非常引人注意的,就好像 Javascript 的 alert 一样,弹出来后整个视窗也不能操作,一定要用户按下 "OK" 才能继续操作,我相信各位也不喜欢到经常弹出 alert box 的网站吧,在 iPhone也是同样道理。
那何时才使用 UIAlertView? 应该是有某些讯息无论如何也要用户去知道,不是那些无关紧要的事,有可能是你的应用程式发生一些问题,令操作不能继续的讯息。例如你的应用程式必须依赖网路来拿取资料,但用户的装置根本没有连接网路,这时候你便需要使用UIAlertView 去提示用户去连接网路,不然应用程式不能运作。
首先是最简单,只显示讯息并只有一个 "OK" 按钮的 Message Box:
?
?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message 1......\nMessage 2......" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show];[alert release];?
样子:
?
?
?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message 1......\nMessage 2......" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil];?
这样便会增加多三个按钮,加上 Cancel Button 一共有 4 个按钮。
样子:?
?
?
如果想按下按钮后有其他动作,你需要在相对应的 Class 加上 UIAlertViewDelegate 的 protocol。
?
例如我想 UIViewController 当 UIAlertView 的代理:
?
ViewController.h
?
?
#import <UIKit/UIKit.h> @interface ViewController : UIViewController <UIAlertViewDelegate> { } @end?
在?ViewController.m?加上以下方法:
?
?
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ //Code.....}?
而 UIAlertView 的 CancelButton 的 buttonIndex 是 0,其他按钮的 buttonIndex 则顺序增加。
可以这样判断用户究竟按下了那一个按钮:
?
?
- (void)loadView { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message 1......\nMessage 2......" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil]; [alert show]; [alert release]; } - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ switch (buttonIndex) { case 0: NSLog(@"Cancel Button Pressed"); break; case 1: NSLog(@"Button 1 Pressed"); break; case 2: NSLog(@"Button 2 Pressed"); break; case 3: NSLog(@"Button 3 Pressed"); break; default: break; } }?
?
?
?
?
?
?
?
?
?
?
?
?