本文转自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 与内阴影的更多相关文章

  1. 【Android】 图片编辑:创建圆角图片

    创建圆角图片的方式大同小异,最简单的就是 9.png 美工做出来的就是.这种最省事直接设置就可以. 另外一种就是通过裁剪 这里的剪裁指的是依据原图我们自己生成一张新的bitmap,这个时候指定图片的目 ...

  2. WPF 采用Border创建圆角

    通过设置可以创建圆角border的CornerRadius属性其边框呈现圆角样式 代码: <Border Height="50" Background="Red&q ...

  3. WPF换肤之一:创建圆角窗体

    原文:WPF换肤之一:创建圆角窗体 我们都期望自己的软件能够有一套看上去很吸引人眼球的外衣,使得别人看上去既专业又有美感.这个系列就带领着大家一步一步的讲解如何设计出一套自己的WPF的窗体皮肤,如果文 ...

  4. Qt 创建圆角、无边框、有阴影、可拖动的窗口 good

    程序窗口的边框,标题栏等是系统管理的,Qt 不能对其进行定制,为了实现定制的边框.标题栏.关闭按钮等,需要把系统默认的边框.标题栏去掉,然后使用 Widget 来模拟它们.这里介绍使用 QSS + Q ...

  5. 一个简单的创建圆角图像的UIImage扩展实现

    - (UIImage *)roundedCornerImageWithCornerRadius:(CGFloat)cornerRadius { CGFloat w = self.size.width; ...

  6. 创建圆角 抛出一个错误:二元运算符“|”不能用于两个UIRectCorner操作数

    //        let beizer:UIBezierPath = UIBezierPath(roundedRect: btn5.bounds, byRoundingCorners: UIRect ...

  7. iOS中创建自定义的圆角按钮

    iOS中很多时候都需要用到指定风格的圆角按钮,尽管UIButton提供了一个方式创建圆角按钮: + (id)buttonWithType:(UIButtonType)buttonType;//指定bu ...

  8. iOS阶段学习第29天笔记(UITextField的介绍)

    iOS学习(UI)知识点整理 一.关于UITextField的介绍 1)概念: UITextField 是用于接收用户输入的一个控件 2)UITextField  初始化实例代码: //创建一个UIt ...

  9. Swift基础之UITextField

    //设置全局变量,将下面的替换即可     //var myTextField = UITextField();     //系统生成的viewDidLoad()方法     override fun ...

随机推荐

  1. android 8 wifi wifi 扫描过程

    查看一下android wifi扫描的过程. packages\apps\Settings\src\com\android\settings\wifi\WifiSettings.java public ...

  2. e741. 将标签的焦点置于关联的文本框上面

    This example associates a label with a text field using setLabelFor(). A mnemonic is set on the labe ...

  3. C#绘制数字图像灰度直方图

    灰度直方图是灰度的函数,描述的是图像中具有该灰度级的像素的个数.如果用直角坐标系来表示,则它的横坐标是灰度级,纵坐标是该灰度出现的概率(像素的个数). 灰度直方图的分布函数: 其中,K是指第k个灰度级 ...

  4. ubuntu 14.04 忘记密码怎么办?

    ref: https://jingyan.baidu.com/article/8065f87fe20832233024985a.html

  5. Linux+Apache+PHP+MySQL服务器环境(CentOS篇)

    1.前言 CentOS(Community ENTerprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定 ...

  6. asp.net 局域网中获取 client的机器名

    //获取客户端计算机名称 System.Net.IPAddress clientIP = System.Net.IPAddress.Parse(Request.UserHostAddress);//根 ...

  7. 无向带权图的最小生成树算法——Prim及Kruskal算法思路

    边赋以权值的图称为网或带权图,带权图的生成树也是带权的,生成树T各边的权值总和称为该树的权. 最小生成树(MST):权值最小的生成树. 生成树和最小生成树的应用:要连通n个城市需要n-1条边线路.可以 ...

  8. thinkphp模板中使用方法

    1.php中的方法使用 <?php $var_num = "13966778888"; $str = substr_replace($var_num,'*****',3,5) ...

  9. Memcached系列之一

    安装.运行 memcached -h 启动选项: -d 作为后台程序 -m -u -l -p -c -P (1)作为前台程序运行 memcached -vv // 显示调试信息 official do ...

  10. js中的坑

    for in vs hasOwnProperty == === 对象比较用===,值比较用==. 严格运算用=== http://www.zhihu.com/question/31442029 著作权 ...