Swift与OC代码转换实例
1.
Objectice-C code:
NSShadow *shadow = [NSShadow new];
[shadow setShadowColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
[shadow setShadowOffset:CGSizeMake(, )];
NSDictionary *attributes = @{
NSForegroundColorAttributeName: [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0],
NSShadowAttributeName: shadow,
NSFontAttributeName: [UIFont fontWithName:@"AmericanTypewriter" size:16.0]
};
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:attributes forState: UIControlStateNormal];
// Or you can use.
[[UIBarItem appearance] setTitleTextAttributes:attributes forState: UIControlStateNormal];
Swift Code:
// Bar title text color
let shadow = NSShadow()
shadow.shadowColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
shadow.shadowOffset = CGSizeMake(, )
let color : UIColor = UIColor(red: 220.0/255.0, green: 104.0/255.0, blue: 1.0/255.0, alpha: 1.0)
let titleFont : UIFont = UIFont(name: "AmericanTypewriter", size: 16.0)!
let attributes = [
NSForegroundColorAttributeName : color,
NSShadowAttributeName : shadow,
NSFontAttributeName : titleFont
]
self.navigationItem.rightBarButtonItem?.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
// Or you can use
UIBarItem.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)
2.
OC:
//设置导航栏字体颜色
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
//[UIFont fontWithName:@"Arial-Bold" size:0.0], UITextAttributeFont
Swift:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor(),
NSFontAttributeName: UIFont(name: "Heiti SC", size: 24.0)!]
3.
OC:
//改变UITabBarItem字体颜色
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0 green:0.72 blue:0.69 alpha:1],UITextAttributeTextColor, nil] forState:UIControlStateSelected];
Swift:
let attributes = [NSForegroundColorAttributeName: UIColor(red: 0, green: 0.72, blue: 0.69, alpha: 1)]
UITabBarItem.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Selected)
4.
OC:
//警示样式
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是个UIAlertController的默认样式" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
Swift:
//警示样式
let alertController = UIAlertController(title: "标题", message:"这个是UIAlertController的默认样式", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "取消", style:UIAlertActionStyle.Destructive, handler: nil)
let okAction = UIAlertAction(title: "确定", style:UIAlertActionStyle.Default, handler:nil)
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.presentViewController(alertController,animated:true,completion:nil)
Swift与OC代码转换实例的更多相关文章
- Swift和Objective-C混合编程——Swift调用OC
在iOS应用的开发中.Swift必将代替OC,两者的趋势是"短期共存,长期代替".但曾经有太多的代码是用OC语言完毕的,而Swift的开发也从 OC中继承了非常多的特性.两者也是有 ...
- Swift与OC区别
一.Swift与OC区别: 1.swift程序的入口是UIApplicationMain; 2.OC的类是以.h和.m组成的;swift是一.swift结尾的; 3.OC的类是以@interface和 ...
- swift与OC之间不得不知道的21点
swift与OC之间不得不知道的21点 自6月的WWDC大会上由苹果的大神Chris Lattner向我们首次展示swift至今已经大半年时间了,虽然绝大部分软件公司代码里还都见不到一丁点swif ...
- Swift与OC混编
OC调用Swift的方法:添加 import "xxxx-Swift.h" 头文件即可 Swift调用OC的方法:需要建立桥接: xxxx-Bridging-Header.h 头文 ...
- iOS开发--Swift 如何完成工程中Swift和OC的混编桥接(Cocoapods同样适用)
由于SDK现在大部分都是OC版本, 所以假如你是一名主要以Swift语言进行开发的开发者, 就要面临如何让OC和Swift兼容在一个工程中, 如果你没有进行过这样的操作, 会感觉异常的茫然, 不用担心 ...
- iOS获取手机型号,类似iphone 7这种 含swift和OC
获取手机设备信息,如name.model.version等,但如果想获取具体的手机型号,如iphone5.5s这种,就需要如下这种 swift: func phonetype () -> Str ...
- swift调用oc语言文件,第三方库文件或者自己创建的oc文件——简书作者
Swift是怎样调用OC的第三方库的呢?请看下面详情: 情况一: 1.首先打开Xcode,iOS->Application->Single View Application, 选Next. ...
- 在项目里交叉使用Swift和OC【转】
Swift and Objective-C in the Same Project在项目里交叉使用Swift和OC Swift与OC的兼容性使得你可以在项目里使用Swift+OC的方式编写应用程序,称 ...
- iOS开发--混编篇&swift与OC混合使用
Swift与OC混合使用 swift 语言出来后,可能新的项目直接使用swift来开发,但可能在过程中会遇到一些情况,某些已用OC写好的类或封装好的模块,不想再在swift 中再写一次,哪就使用混编. ...
随机推荐
- requireJS基本概念及使用流程(2)
上一篇我们一起研究了研究requireJS,这一篇我们来说一说requireJS具体的使用过程 其实很简单的,我总结了总结就是分为四步走 第一步:在页面中引入requireJS并且引入入口文件 第二步 ...
- WebSerivce与WebAPI的区别
一.什么是Web Service Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应 ...
- web界面 之 登录 (初稿)
web登录一般注意以下几点: 1)拥有元素:用户名,密码(密码字符显示与隐藏功能),登录按钮,忘记密码链接(忘记用户名链接),注册链接 2)用户体验: •页面显示,光标默认停在第一个输入框 利用htm ...
- 各类无次数限制的免费API接口整理
各类无次数限制的免费API接口整理,主要是聚合数据上和API Store上的一些,还有一些其他的. 聚合数据提供30大类,160种以上基础数据API服务,国内最大的基础数据API服务,下面就罗列一些免 ...
- ubuntu16.04安装matlab2016b
一.matlab2016b版本下载 在ubuntu下安装matlab2016b,需要三个文件,分别是:Matlab+2016b+Linux64+Crack.rar .R2016b_glnxa64_dv ...
- 用ContentProvider获取通讯录联系人
手机中的联系人数据实际是保存在数据库中的,但Android并没有让我们通过操作数据库去读取数据,而是通过一个ContractProvider这个应用提供了一个ContentProvider访问接口. ...
- JS截取字符串方法实例
// JS截取字符串可使用 substring()或者slice() // // 函数:substring() // 定义:substring(start,end)表示从start到end之间的字符串 ...
- 检测到在集成的托管管道模式下不适用的ASP.NET设置
解决方法是修改web.config如下: <system.webServer> <validation validateIntegratedModeConfiguration=&qu ...
- [iOS] UICollectionView实现图片水平滚动
最新更新: 简单封装了一下代码,参考新文章:UICollectionView实现图片水平滚动 先简单看一下效果: 新博客:http://wossoneri.github.io 准备数据 首先先加入一些 ...
- 大话存储 1 - 走进计算机IO世界
组成计算机的三大件:CPU,内存和IO. 1 总线 总线就是一条或者多条物理上的导线,每个部件都接到这些导线上,同一时刻只能有一个部件在接收或者发送. 仲裁总线:所有部件按照另一条总线,也就是仲裁总线 ...