开发过程中小的知识点梳理
UILabel显示不同字体和颜色
NSString *myTotal = [NSString stringWithFormat:@"积分 %@",18];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:myTotal];
//颜色
[attributeString addAttribute:NSForegroundColorAttributeName value:Color_FC02 range:NSMakeRange(0, 2)];
[attributeString addAttribute:NSForegroundColorAttributeName value:Color_FC09 range:NSMakeRange(2, attributeString.length - 2)];
//字体
//[attributeString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 2)];
//[attributeString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(2, attributeString.length - 2)];
如果是UIButton上的文本,就用UIButton的setAttributedTitle属性:
[_myIntegralBtn setAttributedTitle:attributeString forState:UIControlStateNormal];
如果是UILabel上的文本,就用attributedText属性:
_myIntegralLabel.attributedText = attributeString;
UILabel正常显示省略号的做法
1.设置好UILabel上下左右四个位置的布局,且numberOfLines属性为1行,若文本过长则会自动省略。
2.self.importantLabel.lineBreakMode = NSLineBreakByTruncatingTail;
判断 UITextField 输入的全是空格
NSString *temp = [tf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([temp length]== 0) {} else {};
文本去空格和过滤特殊符号
//过滤特殊符号
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_//|~<>$€^•'@#$%^&*()_+'/"""];
由于NSString中有全角符号和半角符号, 因此有些符号要包括全角和半角的
然后调用stringByTrimmingCharactersInSet
NSString *trimmedString = [string stringByTrimmingCharactersInSet:set];
trimmedString就是过滤后的字符串
//去空格:
1.去掉两端的空格
[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
2.去掉多余的空格
NSString *str = @" this is a test . ";
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
str = [filteredArray componentsJoinedByString:@" "];
3.去掉所有空格
[str stringByReplacingOccurrencesOfString:@" " withString:@""];
视图层次管理 sendSubviewToBack、bringSubviewToFront
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 100, 50)];
view1.backgroundColor = [UIColor blueColor];
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(15, 55, 100, 50)];
view2.backgroundColor = [UIColor grayColor];
[self.view addSubview:view2];
//正常情况是 view1 会在下面 view2会在上面
//将view2调整到父视图的最下面
[self.view sendSubviewToBack:view2];
//将view1调整到父视图的最上面
[self.view bringSubviewToFront:view1];
UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
orangeView.backgroundColor = [UIColor orangeColor];
//给self.view插入一个orangeView,并且这个orangeView是在view2的上面
[self.view insertSubview:orangeView aboveSubview:view2];
//给self.view插入一个orangeView,并且这个orangeView是在view2的下面
[self.view insertSubview:orangeView belowSubview:view2];
iOS获取当前app的名称和版本号
https://www.jianshu.com/p/91f16fe354ff