在一个字符串中搜索子字符串
- 最灵活的方法
1 | |
- 3个方法
1 2 3 | |
- 可以同时工作在32位和64位的
1 2 3 | |
Modifier | d, i | o, u, x, X |
hh | signed char | unsigned char |
h | short | unsigned short |
(none) | int | unsigned int |
l(ell) | long | unsigned long |
j | intmax_t | uintmax_t |
t | ptrdiff_t |
|
z |
| size_t |
- 转换规则
1 2 3 4 5 | |
- 设置最小字段宽度和最小数字位数
1 2 3 4 5 6 | |
- %p可打印指针,和%#x不同的是它可以同时在32位和64位执行
- 使用%f和%g
1 2 3 4 5 | |
- 使用 来
1 2 3 4 5 6 7 8 9 10 | |
- 等价写法
1 2 3 4 5 6 | |
更简洁的方法
1 | |
- NSMutableString的四个方法
1 2 3 4 | |
NSString的方法
1 2 3 | |
NSMutableString不会创建新字符串,性能会好点
1 2 3 4 5 6 7 | |
1 2 | |
正则表达式
1 2 3 4 5 6 7 8 9 10 | |
将字符串分解成数组,使用componentsSeparatedByString:这个方法,或者 enumerateSubstringsInRange:options:usingBlock:。如果是按照行来进行分解可以使用option这个参数 传NSStringEnumerationByLines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
扫描
- NSScanner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
解析器
- 设计一个能够用(100,0,255)或者#ff0000这样的字符来定义颜色的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
先进星扫描,使用NSScanner来解析这个表达式
1 2 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
进行语法解析,需要语法分析库描述我们的语言。下面代码就是为那个布局约束语言写的解析语法,用的扩展的巴科斯:
1 2 3 4 5 6 7 8 | |
还有很多Objective-C的语法解析,更多的可以在CocoaPods上找到:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
一个规则匹配后解析器就找到同样名称的类
1 2 3 4 5 6 7 | |
:
1 | |
可以得到如下结果,方便转换成NSLayoutConstraint对象
1 2 3 4 5 6 7 8 9 | |
UILabel
- label默认显示一行,如果设置numberOfLines为大于1的话可以显示指定行数,如果设置为0,则多少行都显示
- attributedText属性可以显示富文本
- label的font,textColor,textAlignment,shadowColor和shadowOffset属性可以改变外观。
- 改变程序内所有Label的风格,可以使用[UILabel appearance]方法
UITextField
- text field只限于单行
- UITextfield实现了UITextInputTraits协议,这个协议需要指定键盘外观和操作等细节。比如显示什么键盘和返回按键响应等
- 可以通过设置左右辅助视图,或者设置背景来自定义输入框风格了。
UITextView
TableView中显示动态文本
Table view的Delegate有个方法用来计算高度:tableView:heightForRowAtIndexPath:。自定义一个UITableViewCell的子类
1 2 3 4 5 6 7 | |
计算真实高度需要使用boundingRectWithSize:options:context: 这个方法
1 2 3 4 5 6 7 8 9 10 11 12 |
|
使用Text Kit和NSAttributedString进行布局
先设置attributes
1 2 3 4 5 6 7 8 9 10 11 12 |
|
这里字体为BodoniSvtyTwoITCTT,如果需要查看更多字体可以使用 +[UIFont familyNames]这个方法。为了得到字体的名字,可以使用 +[UIFont fontNamesForFamilyName:]。接下来创建段落的属性
1 2 3 4 5 | |
装饰段落风格,使用装饰字体将文本居中对齐,装饰字符的前后加上空白段落
1 2 3 4 5 6 7 8 | |
显示数字表格table,表格布局示例
1 2 3 4 5 6 7 8 9 10 11 12 | |
显示列表的属性设置如下
1 2 3 4 5 6 7 8 9 10 | |