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

这篇文章其实达到了标题所描述的效果,只是当时复制代码过来时,某些符号没粘贴起,大家自己补上,是能正常使用的!!!

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

// 中间的蓝色固定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)];//添加下划线

   // 设置阴影
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowBlurRadius = 3;
    shadow.shadowColor = [UIColor colorWithRed:7/255.0 green:20/255.0 blue:80/255.0 alpha:0.3];
    shadow.shadowOffset = CGSizeMake(0,1);
    [string addAttribute:NSShadowAttributeName
                          value:shadow
                         range:NSMakeRange(0, [string length])];
    // 添加删除线
     [mutableString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, MarketPrice.length)];
    // 设置删除线颜色
     [mutableString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, MarketPrice.length)];

    //旋转字体、斜体
   CGAffineTransform matrix = CGAffineTransformMake(1, 0, tanf(-15 * (CGFloat)M_PI/ 180), 1, 0, 0);
    discoverlabel.transform = matrix;

//检查字符串是否以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 - 固定文字间加图片或文字或改变某字符串属性)