Facebook 的登入界面很好看,email 框與 password 框合為一體。這種效果怎麼做出來?我們都知道 layer 的 cornerRadius 可以做出圓角,但要如何只讓上方有圓角、中間還帶一條分隔線呢?直接動手做一遍最清楚。
現在 Xcode 新建的專案預設帶 Storyboard,純代碼練習時反而不方便,先把它拿掉。打開 AppDelegate.m:
- (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;
}
#import "ViewController.h"
@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;
cornerRadius 填入想要的圓角半徑即可。效果稍微好一點,但兩個框各自圓角顯得零散,接下來把它們合成一體。
直接合并會在中間留下凹陷,所以有兩種解法:一是只替上方加圓角,二是整體墊一張背景。這裡用第二種。
先新增一個繼承 UIView 的類別 textFieldBackground,因為待會要用到它的繪圖方法:
- (void)drawRect:(CGRect)rect {
// Drawing code
}
然後在 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];
關鍵是兩個 textField 的背景都改成 clearColor,讓-white 的底層背景透出來,視覺上就成為一個整體的白色卡片,圓角只由背景視圖負責。
這就是前面特意建立自訂 UIView 的原因。改寫 drawRect:
- (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);
}
// 1. 獲取繪圖上下文 CGContextRef context = UIGraphicsGetCurrentContext(); // 2. 設置線寬 CGContextSetLineWidth(context, 0.2); // 3. 開始繪圖 CGContextBeginPath(context); // 4. 移動到起點 CGContextMoveToPoint(context, 5, 50); // 5. 畫線到終點 CGContextAddLineToPoint(context, self.frame.size.width - 5, 50); // 6. 關閉路徑 CGContextClosePath(context); // 7. 設置顏色 [[UIColor grayColor] setStroke]; // 8. 描邊 CGContextStrokePath(context);
第一,drawRect 的座標系是「以自身為原點」,因此上面那條線的 y = 50 正好落在 100 高的背景正中央。若要改分隔位置,直接調這個數字即可。
第二,drawRect 只有在呼叫 setNeedsDisplay 之後才會重繪,且每次重繪都會建立新的 backing store,效能成本不低。純靜態的線條也可以改用一個 1pt 高的 UIView 直接擺上去,更省事。
第三,若只想讓上方兩個角圓、下方直角,可以改用 CAShapeLayer 搭配 UIBezierPath 設定遮罩:
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_background.bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(5, 5)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = _background.bounds;
maskLayer.path = maskPath.CGPath;
_background.layer.mask = maskLayer;
這樣就不必額外墊一層背景視圖,兩個輸入框各自設圓角也能得到漂亮的接合效果。