-(NSString*)deleteSpecificSymbolForString:(NSString*)text{
//先建立NSCharacterSet,把須要去除的字符串放在裏面
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/::;;¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()()_+'\""];
//該方法返回的就是去除特殊符號後的字符串
NSString *trimmedString = [text stringByTrimmingCharactersInSet:set];
return trimmedString;
}
複製代碼
-(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;
}
複製代碼
-(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;
}
複製代碼
每個應用程序,應該都會有統一的導航欄和狀態欄風格。可是有時候,也有須要在指定控制器裏面修改
狀態欄的風格,如:播放視頻。
-(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];
}
複製代碼
當點擊搜索時,進入搜索控制器,使用系統自帶的搜索框,searchController.searchBar,進入時,自動實現以下圖的效果 git
找了不少方法,下面這個是有效方法:- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self performSelector:@selector(showKeyboard) withObject:nil afterDelay:0];
}
- (void)showKeyboard {
[self.searchController.searchBar becomeFirstResponder];
}
複製代碼
系統自帶的搜索框,使用方便快捷,可是有時候它的外觀與要求不符,這時候就須要咱們來對系統搜索框
進行一些修改
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,搜索框右側的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];
}
}
複製代碼
有時候,須要在指定控制器中隱藏導航欄,試過好幾個方法,或多或少有些坑,下面這個是我認爲比較好的:
首先讓該控制器成爲導航欄的代理:
self.navigationController.delegate = self;
而後,實現指定的代理方法:
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
// 判斷要顯示的控制器是不是本身
BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
[self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
可是,若是須要在連續的兩個控制器裏面隱藏導航欄,上述方法就行不通了,建議寫一個導航欄的基類,在基類中設置代理:
self.delegate = self;
而後,實現代理方法:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 判斷若是是須要隱藏導航控制器的類,則隱藏
BOOL isHideNav = ([viewController isKindOfClass:NSClassFromString(@"EXBusinessCardVC")] ||
[viewController isKindOfClass:NSClassFromString(@"EXSearchCardVC")] ||
[viewController isKindOfClass:NSClassFromString(@"EXExhibitDataVC")] ||
[viewController isKindOfClass:NSClassFromString(@"EXNoticeVC")] ||
[viewController isKindOfClass:NSClassFromString(@"EXBusinessCenterVC")] ||
[viewController isKindOfClass:NSClassFromString(@"EXMeVC")]);
[self setNavigationBarHidden:isHideNav animated:YES];
}
複製代碼
// 根據指定文本,字體和最大高度計算尺寸
- (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;
}
// 根據指定文本,字體和最大寬度計算尺寸
- (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;
}
複製代碼
//給UILabel設置行間距和字間距
-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping; //結尾部分的文字省略方式
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;
}
複製代碼