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

系统 1955 0

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

 

 

 

 

 

 

 

 

 

 

 

 

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


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论