如何创建圆角 UITextField 与内阴影
生活随笔
收集整理的這篇文章主要介紹了
如何创建圆角 UITextField 与内阴影
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文轉自http://www.itstrike.cn/Question/9309fbd6-ef5d-4392-b361-a60fd0a3b18e.html
主要學習如何創建內陰影
我自定義?UITextfield?,看起來像?UISearchbar?。
像做
self.back_textfield = [[UITextField alloc]initWithFrame:CGRectMake(5, 7, 310, 30)]; [self.back_textfield setBorderStyle:UITextBorderStyleRoundedRect]; self.back_textfield.layer.cornerRadius = 15.0;但我看到這個:
正如您可以看到內陰影不會按照邊界。
解決方法 1:
我猜背景上?UITextField?是一個圖像,所以它沒有按照你的圓角半徑。
創建內部陰影是 iOS 中棘手的。你有 2 個選項。
1) 使用該圖像作為背景UITextField
2) 以編程方式設置陰影 (但它看起來的吸引力,比 1 選項)。
這里是從 @Matt Wilding 設置為文本字段與解決方案的圓內陰影的代碼
_textField.layer.cornerRadius = 10.0f;CAShapeLayer* shadowLayer = [CAShapeLayer layer]; [shadowLayer setFrame:_textField.bounds];// Standard shadow stuff [shadowLayer setShadowColor:[[UIColor colorWithWhite:0 alpha:1] CGColor]]; [shadowLayer setShadowOffset:CGSizeMake(0.0f, 0.0f)]; [shadowLayer setShadowOpacity:1.0f]; [shadowLayer setShadowRadius:4];// Causes the inner region in this example to NOT be filled. [shadowLayer setFillRule:kCAFillRuleEvenOdd];// Create the larger rectangle path. CGMutablePathRef path = CGPathCreateMutable(); CGPathAddRect(path, NULL, CGRectInset(_textField.bounds, -42, -42));// Add the inner path so it's subtracted from the outer path. // someInnerPath could be a simple bounds rect, or maybe // a rounded one for some extra fanciness. CGPathRef someInnerPath = [UIBezierPath bezierPathWithRoundedRect:_textField.bounds cornerRadius:10.0f].CGPath; CGPathAddPath(path, NULL, someInnerPath); CGPathCloseSubpath(path);[shadowLayer setPath:path]; CGPathRelease(path);[[_textField layer] addSublayer:shadowLayer];CAShapeLayer* maskLayer = [CAShapeLayer layer]; [maskLayer setPath:someInnerPath]; [shadowLayer setMask:maskLayer];別忘了要導入
#import <QuartzCore/QuartzCore.h>總結
以上是生活随笔為你收集整理的如何创建圆角 UITextField 与内阴影的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js 解析JSON
- 下一篇: 左右TextView旋转门问题