Facebook 的登入頁很好看,其中一個關鍵細節是:Email 框和 Password 框是合在一起的,全體共用同一個圓角外框,中間只用一條細線分隔。
我們都知道,輸入框靠 layer.cornerRadius 就能做出圓角。但問題來了:如果上下兩個框各自加圓角,中間交界處會出現凹陷的缺口,看起來很糟。要怎麼只讓最上面有圓角?
廢話不多說,直接實戰。
Xcode 新建的專案預設自帶故事板,操作起來不夠直觀。先把它拿掉,在 AppDelegate 裡手動建立視窗:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[ViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
記得同時把 Storyboard 的 Main Interface 設定清空,否則系統仍會去載入故事板。
在 ViewController 的類別延伸中宣告屬性,並在 viewDidLoad 中建立:
@interface ViewController ()
@property (nonatomic, strong) UITextField *account;
@property (nonatomic, strong) UITextField *password;
@property (nonatomic, strong) UIButton *loginButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:
[UIColor colorWithRed:51/255.0 green:204/255.0 blue:255/255.0 alpha:1]];
_account = [[UITextField alloc]
initWithFrame:CGRectMake(20, 200, self.view.frame.size.width - 40, 50)];
_account.backgroundColor = [UIColor whiteColor];
_account.placeholder = @"Email";
[self.view addSubview:_account];
_password = [[UITextField alloc]
initWithFrame:CGRectMake(20, 260, self.view.frame.size.width - 40, 50)];
_password.backgroundColor = [UIColor whiteColor];
_password.placeholder = @"Password";
[self.view addSubview:_password];
_loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_loginButton setFrame:CGRectMake(20, 320, self.view.frame.size.width - 40, 50)];
[_loginButton setTitle:@"Login" forState:UIControlStateNormal];
[_loginButton setBackgroundColor:
[UIColor colorWithRed:51/255.0 green:102/255.0 blue:255/255.0 alpha:1]];
[_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.view addSubview:_loginButton];
}
@end
跑一下看結果——喔天哪,醜到哭。完全不能看。來美化一下。
Apple 早就替我們想好了,只要加一行:
_account.layer.cornerRadius = 5.0;
執行起來確實好一點點,但還是很挫。因為接下來我們要把兩個輸入框合併,各自帶圓角的做法本來就走不通。
合併之後中間會有凹進去的部分,所以有兩種解法:
兩種都能達成,我們先用第二種,順便示範自繪。
先新增一個類別繼承 UIView,作為背景。之所以要自訂 UIView,是因為我們待會要用到它的繪圖方法 - (void)drawRect:(CGRect)rect。
在 ViewController 中修改:
_background = [[textFieldBackground alloc]
initWithFrame:CGRectMake(20, 200, self.view.frame.size.width - 40, 100)];
[_background setBackgroundColor:[UIColor whiteColor]];
[[_background layer] setCornerRadius:5];
[[_background layer] setMasksToBounds:YES];
[self.view addSubview:_background];
_account = [[UITextField alloc]
initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 40, 50)];
[_account setBackgroundColor:[UIColor clearColor]];
_account.placeholder = @"Email";
[_background addSubview:_account];
_password = [[UITextField alloc]
initWithFrame:CGRectMake(10, 50, self.view.frame.size.width - 40, 50)];
[_password setBackgroundColor:[UIColor clearColor]];
_password.placeholder = @"Password";
[_background addSubview:_password];
關鍵是兩個輸入框的背景改成 clearColor,讓下面的白色圓角背景透出來。這樣圓角就只有外框那一層在處理,不必去湊四個角的交集。
又好看了一點,不過還是少了什麼——對了,中間少一條分隔線。這就是為什麼要自訂一個 UIView:現在終於可以用到它的繪圖方法了。
- (void)drawRect:(CGRect)rect {
// 取得繪圖上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 設定線寬
CGContextSetLineWidth(context, 0.2);
// 開始繪製路徑
CGContextBeginPath(context);
// 移到起點
CGContextMoveToPoint(context, 5, 50);
// 畫到終點
CGContextAddLineToPoint(context, self.frame.size.width - 5, 50);
// 關閉路徑
CGContextClosePath(context);
// 設定顏色
[[UIColor grayColor] setStroke];
// 實際描邊
CGContextStrokePath(context);
}
看效果——就是這樣,一個簡潔的登入頁完成了。
這個登入頁用到的技術其實不多,重點主要在視覺設計這塊,加上一點點繪圖。主要步驟就是上面那七個 API 呼叫:
// 取得繪圖上下文 CGContextRef context = UIGraphicsGetCurrentContext(); // 設定粗細 CGContextSetLineWidth(context, 0.2); // 開始繪圖 CGContextBeginPath(context); // 移動到起點 CGContextMoveToPoint(context, 5, 50); // 畫到第二個點 CGContextAddLineToPoint(context, self.frame.size.width - 5, 50); // 關閉路徑 CGContextClosePath(context); // 設定顏色 [[UIColor grayColor] setStroke]; // 描邊 CGContextStrokePath(context);
UIBezierPath bezierPathWithRoundedRect:byRoundingCorners:cornerRadii: 搭配 CAShapeLayer 當 mask,比硬湊 cornerRadius 乾淨得多。UIView 的 layer border 或一個 1pt 高的 UIView 子視圖會更省。drawRect: 留給真正需要客製路徑的场合。returnKeyType、在 password 框設 secureTextEntry = YES、實作 keyboardType 與鍵盤收合,這些細節才是使用者實際感受到「好不好用」的地方。