iphone简单实例 (字体,弹出窗口) (实例)

1. UIFont 可以支持的字体预览

 

为iPhone上到底支持哪些字体而发愁吗?  为光看字体名称而不知道其长得 是啥样子而发愁吗?以下demo可以帮到你.

 

帖子地址 http://www.cocoachina.com/bbs/read.php?tid-19894.html

 

下载见附件:FontTest.zip

 

 

IPhone 所支持的字体:
  "Courier"
  "AppleGothic"
  "Arial"
  "STHeiti TC"
  "Hiragino Kaku Gothic ProN"
  "Courier New"
  "Zapfino"
  "Arial Unicode MS"
  "STHeiti SC"
  "American Typewriter"
  "Helvetica"
  "Marker Felt"
  "Helvetica Neue"
  "DB LCD Temp"
  "Verdana"
  "Times New Roman"
  "Georgia"
  "STHeiti J"
  "Arial Rounded MT Bold"
  "Trebuchet MS"
  "STHeiti K"

或者 对于ios中都包含那些可用字体,可以通过调用[UIFont familyNames]方法,它返回的是一个NSArray,

    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];

  每一种FamilyName都是个Array类型的变量,N一般取0基本没有错误,如果N取其他数,且Array里没有那么多,则报错可自己尝试,IPhone字体对国字支持的不够好,基本没有太大变化,一般对英文是嘎嘎好使。

 

 

 

 

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];
 

样子:

 


因为按下 "OK" 按钮后不需要任何动作,所以也不用设置代理 (delegate)。
cancelButtonTitle 是 UIAlertView 预设的按钮,是必须设备的,但按钮显示的文字则可以任意更改。
而 otherButtonTitles 则可以用来增加按钮,每加入一个 NSString 就会多一个按钮。好像以下这样:

 

 

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;
    }
     
}
 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值