字符串的用法
/* 1. 刪除字符串里面的空格,包括首、尾空格*/
? ?NSString *urlString = @"? 4245 ? 25424 ? 24545242? ? 324234234? ? 524234 ? ";
? ? ? ? NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""];
// ? ?刪除空格, 也就意味著,把字符串里面的空格,用給替換掉。
// ?替換字符串中的某一段字符串使用?stringByReplacingOccurrencesOfString 方法
? ? ? ? NSString *replace = [urlString stringByReplacingOccurrencesOfString:@"24545242" withString:@"我的家在中國"];
? ? ? ? NSLog(@"----replace--%@",replace);
? ? ? ? NSLog(@"---首尾----%@",content);
?? ? ? ?
/* ? ? ? ?去除首尾空格和換行:把字符串放到集合中, 由系統(tǒng)方法,去掉首尾空格*/
? ? ? ? NSString *content = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
? ? ? ? NSLog(@"---換行----%@",contents);
/* ?改變字符串中某些字符的顏色,大小 */
? ??NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Change NSAttributed String color and font"];
? ? [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
? ? [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
? ? [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
? ? [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
? ? [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
? ? [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
?UILabel *labe = [[UILabel alloc] init];
? ? labe.titleLabel.attributedText = str;
?
轉(zhuǎn)載于:https://www.cnblogs.com/balopy/p/5210411.html
總結(jié)
- 上一篇: retain和copy还有assign的
- 下一篇: UITextField的属性与程序启动后