iOS常用組件之高效切圓角的方法匯總
前言
圓角(RounderCorner)是一種很常見的視圖效果,相比于直角,它更加柔和優(yōu)美,易于接受。但很多人并不清楚如何設置圓角的正確方式和原理。
iOS 客戶端開發(fā)中,經常碰到圓角視圖的需求,本文簡單總結一下 UIView 及其子類的一些切圓角方法,并且保證避免出現離屏渲染。下面話不多說了,來一起看看詳細的介紹吧。
UIView(不包括其子類)
UIView *view = [[UIView alloc] init]; view.backgroundColor = [UIColor blackColor]; view.layer.cornerRadius = 3.f; // 以下兩行,任寫一行 view.layer.masksToBounds = NO; view.clipToBounds = NO; // 以下兩行,千萬不要加! view.layer.masksToBounds = YES; view.clipToBounds = YES;
注意點:UIView 只要設置圖層的 cornerRadius 屬性即可(不明白的話,可以看看官方文檔里對 cornerRadius 的描述),如果設置 layer.masksToBounds = YES
,會造成不必要的離屏渲染。
文本類視圖
UITextField
UITextField有兩種實現方法
// 天然支持設置圓角邊框 UITextField *textField = [[UITextField alloc] init]; textField.borderStyle = UITextBorderStyleRoundedRect;
// 與 UIView 類似 UITextField *textField = [[UITextField alloc] init]; textField.layer.cornerRadius = cornerRadius;
UITextView
// 與 UIView 類似 UITextView *textView = [[UITextView alloc] init]; textView.layer.cornerRadius = cornerRadius;
UILabel
UILabel *label = [[UILabel alloc] init]; // 重點在此?。≡O置視圖的圖層背景色,千萬不要直接設置 label.backgroundColor label.layer.backgroundColor = [UIColor grayColor].CGColor; label.layer.cornerRadius = cornerRadius;
其它
UIButton
說明:UIButton 的背景圖片,如果是復雜的圖片,可以依靠 UI 切圖來實現。如果是簡單的純色背景圖片,可以利用代碼繪制帶圓角的圖片。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; // 設置 UIButton 的背景圖片。 [button setBackgroundImage:image forState:UIControlStateNormal];
背景圖片繪制方法
+ (UIImage *)pureColorImageWithSize:(CGSize)size color:(UIColor *)color cornRadius:(CGFloat)cornRadius { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, size.width, size.height)]; view.backgroundColor = color; view.layer.cornerRadius = cornerRadius; // 下面方法,第一個參數表示區(qū)域大小。第二個參數表示是否是非透明的。如果需要顯示半透明效果,需要傳NO,否則傳YES。第三個參數是屏幕密度 UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
UIImageView
UIImageView 有四種方式實現圓角:
截取圖片方式(性能較好,基本不掉幀)
@implementation UIImage (extend) - (UIImage *)drawRectWithRoundedCorner { CGRect rect = CGRectMake(0.f, 0.f, 150.f, 150.f); UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:rect.size.width * 0.5]; UIGraphicsBeginImageContextWithOptions(rect.size, false, [UIScreen mainScreen].scale); CGContextAddPath(UIGraphicsGetCurrentContext(), bezierPath.CGPath); CGContextClip(UIGraphicsGetCurrentContext()); [self drawInRect:rect]; CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke); UIImage *output = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return output; } @end
貝塞爾曲線切割圓角(不推薦,掉幀嚴重)
- (UIImageView *)roundedRectImageViewWithCornerRadius:(CGFloat)cornerRadius { UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius]; CAShapeLayer *layer = [CAShapeLayer layer]; layer.path = bezierPath.CGPath; self.layer.mask = layer; return self; }
繪制四個角的遮罩(使用場景受限)
在 UIImageView 上添加一個四個角有內容,其它部分是透明的視圖,只對 UIImageView 圓角部分進行遮擋。但要保證被遮擋的部分背景色要與周圍背景相同,避免穿幫。所以當 UIImageView 處于一個復雜的背景時,是不適合使用這個方法的。
最不推薦做法(當一個頁面只有少量圓角圖片時才推薦使用)
UIImageView *imageView = [[UIImageView alloc] init]; imageView.layer.cornerRadius = 5.f; imageView.layer.masksToBounds = YES;
參考資料:
總結
以上是開發(fā)過程常用組件的切圓角方法總結,如果有更好的方法或者文中有不對的地方,還請指正提出,謝謝。
好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
iOS中的導航欄UINavigationBar與工具欄UIToolBar要點解析
UINavigation可以附著于導航控制器之中使用,也可以在controller中單獨使用,這里我們將來看iOS中的導航欄UINavigationBar與工具欄UIToolBar要點解析.2016-06-06iOS簡單登錄LoginViewController、注冊RegisterViewController等功能實現方法
這篇文章主要為大家詳細介紹了iOS簡單登錄LoginViewController、注冊RegisterViewController、UcenterViewController功能實現方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09iOS和JS交互教程之WKWebView-協(xié)議攔截詳解
這篇文章主要給大家介紹了關于iOS和JS交互教程之WKWebView-協(xié)議攔截的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-09-09為textView添加語音輸入功能的實例代碼(集成訊飛語音識別)
下面小編就為大家分享一篇為textView添加語音輸入功能的實例代碼(集成訊飛語音識別),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01ios開發(fā)UITableViewCell圖片加載優(yōu)化詳解
這篇文章主要為大家介紹了ios開發(fā)UITableViewCell圖片加載優(yōu)化的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07