• iOS常用技术-微博字符串的截取


     1 //
     2 //  NSString+Range.h
     3 //  字符串作业
     4 //
     5 //  Created by andezhou on 15/12/17.
     6 //  Copyright © 2015年 周安德. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 @interface NSString (Range)
    12 
    13 // 截取话题##
    14 - (NSArray *)queryTopicFromString:(NSString *)topic;
    15 
    16 // 截取表情符号[]
    17 - (NSArray *)querySmiliesFromString:(NSString *)fromStr
    18                          toStr:(NSString *)toStr;
    19 
    20 // 截取@name
    21 - (NSArray *)queryName;
    22 
    23 @end

    /*************************************************/

     1 //
     2 //  NSString+Range.m
     3 //  字符串作业
     4 //
     5 //  Created by andezhou on 15/12/17.
     6 //  Copyright © 2015年 周安德. All rights reserved.
     7 //
     8 
     9 #import "NSString+Range.h"
    10 
    11 @implementation NSString (Range)
    12 
    13 // 截取话题##
    14 - (NSArray *)queryTopicFromString:(NSString *)topic
    15 {
    16     // 初始化一个存放range的数组
    17     NSMutableArray *array = [NSMutableArray array];
    18     return [self queryStringFrom:topic to:topic array:array];
    19 }
    20 
    21 // 截取表情符号[]
    22 - (NSArray *)querySmiliesFromString:(NSString *)fromStr
    23                          toStr:(NSString *)toStr
    24 {
    25     NSMutableArray *array = [NSMutableArray array];
    26     return [self queryStringFrom:fromStr to:toStr array:array];
    27 }
    28 
    29 // 截取@name
    30 - (NSArray *)queryName
    31 {
    32     NSMutableArray *array = [NSMutableArray array];
    33     return [self queryStringFrom:@"@" to:@" " array:array];
    34 }
    35 
    36 - (NSArray *)queryStringFrom:(NSString *)from to:(NSString *)to array:(NSMutableArray *)array
    37 {
    38     // 获取开始#所在的位置
    39     NSRange fromRange = [self rangeOfString:from];
    40     
    41     if (fromRange.location != NSNotFound) {
    42         // 获取开始索引
    43         NSUInteger fromIndex = fromRange.location + fromRange.length;
    44         // 从索引开始截取字符串
    45         NSString *fromStr = [self substringFromIndex:fromIndex];
    46         
    47     
    48         // 获取与#所对应的#的位置
    49         NSRange toRange = [fromStr rangeOfString:to];
    50         
    51         // 判断所对应的#是否存在
    52         if (toRange.location != NSNotFound) {
    53             // 得到##之间的标题
    54             NSString *str = [fromStr substringToIndex:toRange.location];
    55             // 得到完整的标题字符串,如#大学是所整容院#
    56             NSString *string = [NSString stringWithFormat:@"%@%@%@", from, str, to];
    57             NSLog(@"string:%@", string);
    58 
    59             // 得到完成的标题字符串在字符串中的位置
    60             NSRange range = [self rangeOfString:string];
    61             
    62             // 先判断上一个是否存在
    63             NSValue *value = [array lastObject];
    64             if (value.rangeValue.length) {
    65                 // 当前话题的range中的位置添加上上一个话题的range的位置和长度
    66                 NSUInteger location = value.rangeValue.length + value.rangeValue.location;
    67                 range = NSMakeRange(range.location + location, range.length);
    68             }
    69             
    70             // 把range转化为NSValue存放在数组中
    71             [array addObject:[NSValue valueWithRange:range]];
    72 
    73             // 获取标题##后面的内容
    74             NSString *nextStr = [fromStr substringFromIndex:toRange.location + toRange.length];
    75             
    76             // 递归继续查询
    77             [nextStr queryStringFrom:from to:to array:array];
    78         }
    79     }
    80     
    81     return array;
    82 }
    83 
    84 @end

    /*******************************************************************/

     1 //
     2 //  ViewController.m
     3 //  作业
     4 //
     5 //  Created by andezhou on 16/1/4.
     6 //  Copyright (c) 2016年 周安德. All rights reserved.
     7 //
     8 
     9 #import "ViewController.h"
    10 #import "NSString+Range.h"
    11 
    12 @interface ViewController ()
    13 
    14 @end
    15 
    16 @implementation ViewController
    17 
    18 - (void)viewDidLoad {
    19     [super viewDidLoad];
    20     NSString *str = @"哈哈#大学是所整容院# 岁月是把杀猪刀还是整容刀[/尴尬],最近网友纷纷/晒出自己大学前后的对比照,简直集体整容的节奏[/开心]。@球球 你说对不对呢?[/阴险] #开心一刻# @小明 #你会不会# 啊 我不会啊。";
    21     
    22     
    23     NSArray *topicArray = [str queryTopicFromString:@"#"];
    24     NSArray *nameArray = [topicArray arrayByAddingObjectsFromArray:[str queryName]];
    25     NSArray *smilieArray = [nameArray arrayByAddingObjectsFromArray:[str querySmiliesFromString:@"[/" toStr:@"]"]];
    26     
    27     NSMutableAttributedString *mutString = [[NSMutableAttributedString alloc] initWithString:str];
    28     // 给添加颜色
    29     for (NSValue *value in smilieArray) {
    30         NSRange range = value.rangeValue;
    31         [mutString addAttributes:@{
    32                                    NSForegroundColorAttributeName : [UIColor blueColor]
    33                                    }
    34                            range:range];
    35     }
    36     
    37     UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 0, 0)];
    38     lab.numberOfLines = 0;
    39     lab.attributedText = mutString;
    40     CGSize size = [lab sizeThatFits:CGSizeMake(self.view.frame.size.width - 20, MAXFLOAT)];
    41     lab.frame = CGRectMake(10, 100, size.width, size.height);
    42     [self.view addSubview:lab];
    43     
    44     // Do any additional setup after loading the view, typically from a nib.
    45 }
    46 
    47 - (void)didReceiveMemoryWarning {
    48     [super didReceiveMemoryWarning];
    49     // Dispose of any resources that can be recreated.
    50 }
    51 
    52 @end

    /**********************************************************************/

  • 相关阅读:
    Inno Setup执行SQL脚本的方法
    批处理命令篇--配置免安装mysql
    nsis安装包_示例脚本语法解析
    全方位掌握nsis脚本
    dos批处理知识
    mysql alter 语句用法,添加、修改、删除字段等
    .Net WebAPI 增加Swagger
    CentOS 7 Docker
    四:Ionic Framework不支持Android4.2.2的解决方法
    二:Ionic Framework支持Android开发
  • 原文地址:https://www.cnblogs.com/MrWuYindi/p/5146604.html
Copyright © 2020-2023  润新知