1.去除NSString字符串里面的特殊符號
-(NSString*)deleteSpecificSymbolForString:(NSString*)text{
//先創(chuàng)建NSCharacterSet休建,把需要去除的字符串放在里面
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/::;;¥「」"、[]{}#%-*+=_\\|~<>$€^?'@#$%^&*()()_+'\""];
//該方法返回的就是去除特殊符號后的字符串
NSString *trimmedString = [text stringByTrimmingCharactersInSet:set];
return trimmedString;
}
2.判斷NSString字符串里面有沒有漢字
-(BOOL)estimateStringContainChinese:(NSString*)text{
if(text){
for (int i=0; i<text.length; i++) {
NSRange range = NSMakeRange(i,1);
NSString *subString = [text substringWithRange:range];
const char *cString = [subString UTF8String];
if (strlen(cString) == 3){ //代表有漢字
return YES;
}
}
}
return NO;
}
3.判斷NSString字符串里面有沒有英文字母
-(BOOL)estimateStringContainEnglish:(NSString*)text{
if(text){
for (int i=0; i<text.length; i++) {
NSRange range = NSMakeRange(i,1);
NSString *subString = [text substringWithRange:range];
const char *cString = [subString UTF8String];
if (strlen(cString) == 1){ //代表有英文
return YES;
}
}
}
return NO;
}
4.在指定控制器里面更改狀態(tài)欄文字顏色
每一個應用程序渴频,應該都會有統(tǒng)一的導航欄和狀態(tài)欄風格驱证。但是有時候冷离,也有需要在指定控制器里面修改
狀態(tài)欄的風格愿棋,如:播放視頻招盲。
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
}
5.點擊進入搜索控制器時扭屁,自動彈出鍵盤算谈,讓搜索框進入編輯狀態(tài)
當點擊搜索時,進入搜索控制器料滥,使用系統(tǒng)自帶的搜索框然眼,searchController.searchBar,進入時葵腹,自動實現(xiàn)如下圖的效果
效果圖.png
找了很多方法高每,下面這個是有效方法:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self performSelector:@selector(showKeyboard) withObject:nil afterDelay:0];
}
- (void)showKeyboard {
[self.searchController.searchBar becomeFirstResponder];
}
6.修改系統(tǒng)自帶搜索框的UI
系統(tǒng)自帶的搜索框,使用方便快捷践宴,但是有時候它的外觀與要求不符鲸匿,這時候就需要我們來對系統(tǒng)搜索框
進行一些修改
for (UIView* subview in [[self.searchController.searchBar.subviews lastObject] subviews]) {
//相當于UITextField,輸入框阻肩,可以修改背景顏色等
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
UITextField *textField = (UITextField*)subview;
[textField setBackgroundColor:COLOR_RGB(238, 238, 238)];
}
//相當于UIButton带欢,搜索框右側(cè)的Cancle按鈕运授,可以修改默認文字等
if ([subview isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
UIButton* cancle = (UIButton*)subview;
[cancle setTitle:@"取消" forState:UIControlStateNormal];
[cancle setTitleColor:COLOR_RGB(82, 184, 255) forState:UIControlStateNormal];
}
//相當于UIView,搜索框的背景view乔煞,一般將它去掉吁朦,然后自己添加一層背景view
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
UIView* back = [[UIView alloc]initWithFrame:self.searchController.searchBar.bounds];
back .backgroundColor = [UIColor whiteColor];
[self.searchController.searchBar insertSubview:back atIndex:0];
}
}
7.在指定控制器中隱藏導航欄
有時候,需要在指定控制器中隱藏導航欄渡贾,試過好幾個方法逗宜,或多或少有些坑,下面這個是我認為比較好的:
首先讓該控制器成為導航欄的代理:
self.navigationController.delegate = self;
然后空骚,實現(xiàn)指定的代理方法:
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
if ([viewController isKindOfClass:[self class]]) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
//返回時纺讲,在指定控制器重新讓導航欄顯示
if ([viewController isKindOfClass:[ContactVC class]]) {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
8.根據(jù)指定文本,字體府怯,固定寬度或高度刻诊,求尺寸
// 根據(jù)指定文本,字體和最大高度計算尺寸
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxHeight:(CGFloat)height
{
NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
attrDict[NSFontAttributeName] = font;
CGSize size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil].size;
return size;
}
// 根據(jù)指定文本,字體和最大寬度計算尺寸
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxWidth:(CGFloat)width
{
NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
attrDict[NSFontAttributeName] = font;
CGSize size = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil].size;
return size;
}
9.給UILabel設置行間距,字間距等富文本屬性牺丙,并計算高度
//給UILabel設置行間距和字間距
-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping; //結(jié)尾部分的文字省略方式
paraStyle.alignment = NSTextAlignmentLeft; //文本對齊方式
paraStyle.lineSpacing = 7; //設置行間距
paraStyle.hyphenationFactor = 1.0; //連字屬性 在iOS则涯,唯一支持的值分別為0和1
paraStyle.firstLineHeadIndent = 0.0; //首行縮進
paraStyle.paragraphSpacingBefore = 0.0; //段首行空白空間
paraStyle.headIndent = 0; //整體縮進(首行除外)
paraStyle.paragraphSpacing = 15; //段與段之間的間距
//設置字間距 NSKernAttributeName:@1.5f
NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f};
NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
label.attributedText = attributeStr;
}
//計算UILabel的高度(帶有行間距的情況)
-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.lineSpacing = 7;
paraStyle.hyphenationFactor = 1.0;
paraStyle.firstLineHeadIndent = 0.0;
paraStyle.paragraphSpacingBefore = 0.0;
paraStyle.headIndent = 0;
paraStyle.paragraphSpacing = 15;
NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f};
CGSize size = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
return size.height;
}