Categories
Recent Posts

Archive for February, 2009

iPhone SDK开发:如何显示WiFi提示

如果你的程序中用到了WiFi,想在没有有效WiFi的时候出现如图所示的提示该怎么做?

这个问题被很多人问到过,其实很简单, 只需要在Info.plist中添加如下Key/Value

1
2
3
key name                        value type    value
 
UIRequiresPersistentWiFi     Boolean        true

一定要是 Boolean类型才可以, iPhone OS 2.1 以后会忽略String类型的YES值

编译运行你的程序, 如果当前WiFi网络没有任何有效设置的话, 就会弹出提示并列出所有可达的WiFi接入点

iPhone SDK开发:改变UISWitch文本和颜色

iPhone SDK中的Switch控件默认的文本为 ON OFF两种,不同的语言显示不同, 颜色均为蓝色和亮灰色,如图

如果想改变ON,OFF文本,我们必须重从UISwitch继承一个新类, 然后在新的Switch类中修改替换原有的Views, 申明代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
//  UICustomSwitch.h
//  UICatalog
//
//  Created by aish on 09-2-25.
//  Copyright 2009  .. All rights reserved.
//
 
#import <UIKit/UIKit.h>
 
// 该方法时SDK文档中没有的, 添加一个category
@interface UISwitch (extended)
- (void) setAlternateColors:(BOOL) boolean;
@end
// 自定义Slider 类
@interface _UISwitchSlider : UIView
@end
 
 
@interface UICustomSwitch : UISwitch {
 
}
- (void) setLeftLabelText:(NSString *)labelText
                     font:(UIFont*)labelFont
                    color: (UIColor *)labelColor;
- (void) setRightLabelText:(NSString *)labelText
                      font:(UIFont*)labelFont
                     color:(UIColor *)labelColor;
- (UILabel*) createLabelWithText:(NSString*)labelText
                            font:(UIFont*)labelFont
                           color:(UIColor*)labelColor;
 
@end

代码中添加了一个名为extended的category,主要作用是申明一下UISwitch的 setAlternateColors消息,否则在使用的时候会出现找不到该消息的警告。其实setAlternateColors已经在UISwitch中实现,只是没有在头文件中公开而已, 所以在此只是做一个申明。当调用setAlternateColors:YES 时, UISwitch的状态为“on”时会显示为橙色,否则为亮蓝色

下面是实现部分的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
//  UICustomSwitch.m
//  UICatalog
//
//  Created by aish on 09-2-25.
//  Copyright 2009.. All rights reserved.
//
 
#import "UICustomSwitch.h"
 
 
 
@implementation UICustomSwitch
 
 
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}
 
 
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
 
 
- (void)dealloc {
    [super dealloc];
}
 
- (_UISwitchSlider *) slider {
    return [[self subviews] lastObject];
}
- (UIView *) textHolder {
    return [[[self slider] subviews] objectAtIndex:2];
}
- (UILabel *) leftLabel {
    return [[[self textHolder] subviews] objectAtIndex:0];
}
- (UILabel *) rightLabel {
    return [[[self textHolder] subviews] objectAtIndex:1];
}
 
// 创建文本标签
- (UILabel*) createLabelWithText:(NSString*)labelText
                            font:(UIFont*)labelFont
                           color:(UIColor*)labelColor
{
    CGRect rect = CGRectMake(-25.0f, -10.0f, 50.0f, 20.0f);
    UILabel *label = [[UILabel alloc] initWithFrame: rect];
    label.text = labelText;
    label.font = labelFont;
    label.textColor = labelColor;
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
 
    return label;
}
 
// 重新设定左边的文本标签
- (void) setLeftLabelText:(NSString *)labelText
                     font:(UIFont*)labelFont
                    color:(UIColor *)labelColor
{
    @try {
        //
        [[self leftLabel] setText:labelText];
        [[self leftLabel] setFont:labelFont];
        [[self leftLabel] setTextColor:labelColor];
    } @catch (NSException *ex) {
        // 
        UIImageView* leftImage = (UIImageView*)[self leftLabel];
        leftImage.image = nil;
        leftImage.frame = CGRectMake(0.0f, 0.0f, 0.0f, 0.0f);
 
        //
        [leftImage addSubview: [[self createLabelWithText:labelText
                                                     font:labelFont
                                                    color:labelColor] autorelease]];
    }
}
 
// 重新设定右边的文本
- (void) setRightLabelText:(NSString *)labelText font:(UIFont*)labelFont color:(UIColor *)labelColor {
    @try {
        // 
        [[self rightLabel] setText:labelText];
        [[self rightLabel] setFont:labelFont];
        [[self rightLabel] setTextColor:labelColor];
    } @catch (NSException *ex) {
        // 
        UIImageView* rightImage = (UIImageView*)[self rightLabel];
        rightImage.image = nil;
        rightImage.frame = CGRectMake(0.0f, 0.0f, 0.0f, 0.0f);
 
        //
        [rightImage addSubview: [[self createLabelWithText:labelText
                                                      font:labelFont
                                                     color:labelColor] autorelease]];   
    }
}
 
@end

实现的过程就是替换原有的标签view 以及slider。

使用也很简单,设置一下左右文本以及颜色即可,比如

1
2
3
4
5
6
7
8
9
	switchCtl = [[UICustomSwitch alloc] initWithFrame:frame];
 
//	[switchCtl setAlternateColors:YES];
	[switchCtl setLeftLabelText:@"Yes"
                            font:[UIFont boldSystemFontOfSize: 17.0f]
                           color:[UIColor whiteColor]];
    [switchCtl setRightLabelText:@"No"
                             font:[UIFont boldSystemFontOfSize: 17.0f]
                            color:[UIColor grayColor]];

上面的代码将显示Yes,No两个选项,如图

iPhone SDK开发:自定义UIAlertView

iPhone SDK提供 UIAlertView用以显示消息框, 默认的消息框很简单,只需要提供title和message 以及button按钮即可, 而且默认情况下素有的text是居中对齐的。 那如果需要将文本向左对齐或者添加其他控件比如输入框时该怎么办呢? 不用担心, iPhone SDK还是很灵活的, 有很多delegate消息供调用程序使用。 所要做的就是在
- (void)willPresentAlertView:(UIAlertView *)alertView
中按照自己的需要修改或添加即可, 比如需要将消息文本左对齐,下面的代码即可实现

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)willPresentAlertView:(UIAlertView *)alertView
{
 
		for( UIView * view in alertView.subviews )
		{
			if( [view isKindOfClass:[UILabel class]] )
			{
				UILabel* label = (UILabel*) view;
				label.textAlignment = UITextAlignmentLeft;
 
			}
		}
}

这段代码很简单, 就是在消息框即将弹出时,遍历所有消息框对象,将其文本对齐属性修改为 UITextAlignmentLeft即可。

添加其他部件也如出一辙, 如下代码添加两个UITextField

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- (void)willPresentAlertView:(UIAlertView *)alertView
{
	CGRect frame = alertView.frame;
	if( alertView==twitterAlertView )
	{
		frame.origin.y -= 120;
		frame.size.height += 80;
		alertView.frame = frame;
		for( UIView * view in alertView.subviews )
		{
			if( ![view isKindOfClass:[UILabel class]] )
			{
				CGRect btnFrame = view.frame;
				btnFrame.origin.y += 70;
 
				view.frame = btnFrame;
 
			}
		}
		UITextField* accoutName = [[HelperClass createTextField] autorelease];//这里创建一个UITextField对象
		UITextField* accoutPassword = [[HelperClass createTextField] autorelease];//这里创建一个UITextField对象
		accoutName.frame = CGRectMake( 10, 40,frame.size.width - 20, 30 );
		accoutPassword.frame = CGRectMake( 10, 80,frame.size.width -20, 30 );
		accoutName.placeholder = @"Account Name";
		accoutPassword.placeholder = @"Password";
		accoutPassword.secureTextEntry = YES;
		[alertView addSubview:accoutPassword];
		[alertView addSubview:accoutName];
	}
}

显示将消息框固有的button和label移位, 不然添加的text field会将其遮盖住。 然后添加需要的部件到相应的位置即可。

对于UIActionSheet其实也是一样的, 在
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
中做同样的处理一样可以得到自己想要的界面。

软件草稿设计软件Balsamiq Mockups

意大利软件开发作者开发的软件Mockups,用于软件设计初级阶段绘制草稿。 使用方便,功能强大,支持绝大多数软件所需部件。 直接拖放操作,非常简单易用。

该软件是用Adobe Air 平台开发, 可以作为web应用和桌面应用, 下面是一些软件案例

更多示例请看这里

该软件支持windows,linux以及mac OSX, 安装使用之前确认安装了Adobe Air, 然后下载MockupsForDesktop.air安装即可。

详情请看官方网站http://www.balsamiq.com/products/mockups

UIWebView 显示本地html及引用资源

有些软件中可能会用到html页面,比如用在做帮助页面或者其它相关信息时。 这种情况下只需要将html页面文件以及索引用到的资源(图片、声音等) 都加入到bundle中,或者单独创建一个bundle, 在需要的时候调用UIWebView的相关方法即可

比如

1
2
3
4
5
NSBundle* bundle = [NSBundle mainBundle];
NSString*  resPath = [bunder resourcePath];
NSString* filePath = [resPath stringByAppendPathComponent:@"test.html"];
 
[WebView loadHTMLString:[NSString stringWithContentsOfFile:filePath] baseURL:[NSURL fileURLWithPath:[bundle bundlePath]]];

其中bundle可以不是mainbundle,test.html就是想要显示的页面

Alexa的排名很奇怪

今天突发奇想,想看看自己网站的排名, 这个博客的排名是 970,669, 奇怪的是我的另外一个小站的访问量和流量都远远大于这个博客站点, 但是排名却在 7 位数以后, 实在让我感到莫名。

一般来讲, 影响网站排名有以下几个因素:

  1. 访问量; 这一条是硬指标, 访问量的的网站说明很popular,才能取得好的排名
  2. 外部链接, 也就是从别的网站到该网站的链接;
  3. 流量; 这个一般和访问量成正比

我想这个博客的排名比那个软件的小站排名靠前可能因为第二条, 外部链接稍微多一些的缘故吧:)

最近在研究有关SEO方面的技术, 不看不知道, 那些SEO的大牛确实不一般。 继续努力ing。。。