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