1.属性字符串的使用
可为一段文本的不同区间设置不同显示格式
int remainCount = 999;
NSMutableAttributedString *attributes = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d+新品",remainCount]];
//添加前景色属性(此处将0-3范围的字符串的颜色设置为紫色)类似还可添加不同的属性
[attributes addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(0, 3)];
//设置属性文字
_remianCountLabel.attributedText = attributes;
2.iOS富文本组件的实现-DTCoreText
DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,主要用来替代高内存消耗的低性能的UIWebView
3.TextKit
4.代码快速排版快捷键
全选ctrl+A , 快速排版ctrl+I
5.自定义按钮的图片和文字的显示位置
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"我在这里" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"pp"]; forState:UIControlStateNormal];
button.titleEdgeInsets = UIEdgeInsetsMake(0, -40, 0, 0);
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 10, 0);
UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);
四个参数分别表述上、左、底、右距离默认位置的距离
6.UIImage图片旋转
通过UIImage的类方法实现
//下面的类方法可对图片进行旋转的同时进行缩放
+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation ;
示例:
UIImage *image = [UIImage imageWithCGImage:[UIImage imageNamed:@"pp"].CGImage scale:1 orientation:UIImageOrientationLeft];
7.UITextView设置占位文本
在控制器的view上添加UITextView和UILabel,这里将UILabel设置为我们的占位文本,并设置UITextView的代理之后编写代理方法
UITextView *_secretContent;
UILabel *_placeHolderLabel;
#pragma mark 带有placeholder的textview的实现
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]) {//检测到“完成”
[textView resignFirstResponder];//释放键盘
return NO;
}
if (_secretContent.text.length==0){//textview长度为0
if ([text isEqualToString:@""]) {//判断是否为删除键
_placeHolderLabel.hidden=NO;//隐藏文字
}else{
_placeHolderLabel.hidden=YES;
}
}else{//textview长度不为0
if (_secretContent.text.length==1){//textview长度为1时候
if ([text isEqualToString:@""]) {//判断是否为删除键
_placeHolderLabel.hidden=NO;
}else{//不是删除
_placeHolderLabel.hidden=YES;
}
}else{//长度不为1时候
_placeHolderLabel.hidden=YES;
}
}
return YES;
}
8.dispatch_once实现单例
void dispatch_once( dispatch_once_t *predicate, dispatch_block_t block);
predicate:用于检查该代码块是否已经被调度的谓词
示例代码如下:
+ (id)sharedManager {
static dispatch_once_t once;
static id instance;
dispatch_once(&once, ^{
instance = [self new];
});
return instance;
}
9.iOS正则匹配(当前时间的正则匹配)
正则匹配表达式基础入门:http://blog.csdn.net/lxcnn/article/details/4268033
//当前时间的正则匹配
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSString *urlString = [formatter stringFromDate:date];
NSLog(@"urlString=%@",urlString);
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^(0[1-9]|[1|2][0-9]|3[0|1])[-|/|.](0[1-9]|1[0|1|2])[-|/|.](19|20)\\d\\d\\s(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$" options:0 error:&error];
if (regex != nil) {
NSTextCheckingResult *firstMatch = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];
if (firstMatch) {
NSRange resultRange = [firstMatch rangeAtIndex:0];
//从urlString中截取数据
NSString *result = [urlString substringWithRange:resultRange];
NSLog(@"正则匹配结果=%@",result);
}
}
输出结果:
2015-04-17 14:19:14.713 PredicateTest[2023:97164] urlString=17-04-2015 14:19:14
2015-04-17 14:19:14.719 PredicateTest[2023:97164] 正则匹配结果=17-04-2015 14:19:14
10.使用Object-C字面量
//字典的存取
NSDictionary *dict = @{@"s1":@"v1",@"s2":@"v2"};
NSString *string = dict[@"s1"];
//相当于[NSNumber numberWithInt:10]
NSNumber *number = @10;
//数组的存取
NSArray *array = @[@1,@2,@"s"];
NSString *s = array[1];
11.iOS8.x模拟器输入中文
模拟器设置->通用->键盘->键盘->添加新键盘->简体中文(英文环境下:Settings->General->Keyboard->Keyboards->Add New Keyboard->Chinese(Simplified)->Pinyin-QWERTY->Done
然后在Xcode菜单Product->Scheme->EditScheme->Options->Application Region->中国
12.将汉字转成拼音
NSMutableString *pinyin = [@"吧啦吧啦小魔仙变身!!!" mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
NSLog(@"%@", pinyin);
13.将OC代码转成C++底层代码
终端输入:clang -rewrite-objc main.m