美文网首页
iOS - 固定文字间加图片或文字或改变某字符串属性

iOS - 固定文字间加图片或文字或改变某字符串属性

作者: HanZhiZzzzz | 来源:发表于2017-05-19 11:42 被阅读0次
    这篇文章其实达到了标题所描述的效果,只是当时复制代码过来时,某些符号没粘贴起,大家自己补上,是能正常使用的!!!

    /** 固定文字中间夹图片*/

    // 中间的蓝色固定label
    
    UILabel *blueLabel = MakeLabelWith(Main_BlueC,six_P,14);// 初始化label 此为我自己自定义的  大家可以自己写
    
    blueLabel.textAlignment = NSTextAlignmentLeft;
    
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString"无法重新连接设备,请确认设备处于开机状态,且 "];
    
    // 创建图片图片附件
    
    NSTextAttachment *attach = [[NSTextAttachment alloc] init];
    
    attach.image = [UIImage imageNamed"wifi_logo"];
    
    CGFloat imageW = 10;
    
    attach.bounds = CGRectMake(0, - imageW / 3,imageW ,imageW);
    
    NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
    
    [string appendAttributedString:attachString];
    
    [string appendAttributedString:[[NSAttributedString alloc] initWithString" 指示灯处于绿色闪烁状态。并点击“重新连接设备”。"]];
    
    // 添加图片另一种方式 (YYLabel  可显示)
     UIImageView *imageView1 = [[UIImageView alloc] initWithImage:imageWithName(@"icon_baoyou")];
     imageView1.frame = CGRectMake(0, 0, 30, 15);
    imageView1.contentMode = UIViewContentModeScaleAspectFit;
    NSMutableAttributedString *attachText0 = [NSMutableAttributedString yy_attachmentStringWithContent:imageView1
    contentMode:UIViewContentModeCenter                                                                                            attachmentSize:imageView1.size          alignToFont:[UIFont systemFontOfSize:midFont]                                alignment:YYTextVerticalAlignmentCenter];
     [string insertAttributedString:attachText0 atIndex:wstr.length];
    
    
    
    
    // 设置行距
    
    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    
    [paragraphStyle1 setLineSpacing:10];
    // 居中对齐
     paragraphStyle1.alignment = NSTextAlignmentCenter;
    
    [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [string length])];
    
    blueLabel.attributedText = string;
    
    blueLabel.numberOfLines = 0;
    
    [whiteBgView addSubview:blueLabel];
    
    self.blueLabel = blueLabel;
    

    /** 固定文字中间夹 不定文字*/

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString"设备已被 "];
    
    // 电话号码
    
    NSString *phoneNumStr = securityPhonNum(managerPhoneNum);
    
    NSMutableAttributedString *phoneNum = [[NSMutableAttributedString alloc] initWithString:phoneNumStr];
    
    [phoneNum addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, managerPhoneNum.length)];
    
    [string appendAttributedString:phoneNum];
    
    [string appendAttributedString:[[NSAttributedString alloc] initWithString" 绑定,\n请联系管理员获取控制权限。"]];
    
    // 设置行距
    
    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    
    [paragraphStyle1 setLineSpacing:15];
    
    [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [string length])];
    
    self.blueLabel.attributedText = string;
    

    /** 改变字符串中间 某字段属性*/

    if([cellTilte rangeOfString:self.searchStr].location !=NSNotFound) {
    
    NSRange range = [cellTilte rangeOfString:self.searchStr];
    
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:cellTilte];
    
    
    [string addAttribute:NSForegroundColorAttributeName value:MainLiBlueC range:range];
    
    self.label.attributedText = string;
    }
    
       [mutableString addAttribute:NSFontAttributeName
                             value:[UIFont systemFontOfSize:21.0]
                             range:NSMakeRange(3, totalFee.length - 3)];//设置字体
         [mutableString addAttribute:NSForegroundColorAttributeName
                               value:[UIColor redColor]
                              range:NSMakeRange(3, totalFee.length - 3)];//设置颜色
         [mutableString addAttribute:NSUnderlineStyleAttributeName
                             value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]
                               range:NSMakeRange(0, totalFee.length)];//添加下划线
    
         [mutableString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, MarketPrice.length)];// 添加删除线
         [mutableString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, MarketPrice.length)];// 设置删除线颜色
    

    //检查字符串是否以astring开头;

    -(BOOL) hasPrefix:(NSString *) astring;
    

    //检查字符串是否以astring结尾;

    -(BOOL) hasSuffix:(NSString *) astring;
    

    //如果想知道字符串内的某处是否包含其他的字符串,使用rangeOfString:

    -(NSRange) rangeOfString:(NSString *) astring;
    

    //将rangeOfString:发送给一个NSString对象时,传递的参数时要查找的字符串。它会返回一个NSRange struct来告诉你一个与这个字符串相匹配的部分从哪里开始以及匹配上的字符个数。如下:

    NSRange range=[filename rangeOfString:@"www"];
    
    
    
    if(range.location!=NSNotFound) {
    
        return true;
    
    }
    
    else { 
    
        return false;
    
    }
    

    相关文章

      网友评论

          本文标题:iOS - 固定文字间加图片或文字或改变某字符串属性

          本文链接:https://www.haomeiwen.com/subject/kahyxxtx.html