给UIlabel的内容中的特定字符添加颜色,可以调整大小

摘要:实现对NSAttributedString的一个扩展,用法见.m的注释内容

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@interface NSAttributedString (color)

+ (instancetype)getAtriWith:(NSString*)markup withDestStr:(NSArray*)deStrs
andColor:(NSArray*)colors andFont:(NSArray*)fonts;

@end

#import "NSAttributedString+color.h"

@implementation NSAttributedString (color)

+ (instancetype)getAtriWith:(NSString*)markup withDestStr:(NSArray*)deStrs
andColor:(NSArray*)colors andFont:(NSArray*)fonts{

NSMutableAttributedString *aString = [[NSMutableAttributedString
alloc]
initWithString:markup];

int count = 0;

for (NSString* str
in deStrs){

NSRegularExpression *regex = [[NSRegularExpression
alloc]initWithPattern:str
options:NSRegularExpressionCaseInsensitive|NSRegularExpressionDotMatchesLineSeparators
error:nil];//2

NSArray *chunks = [regex
matchesInString:markup options:0
range:NSMakeRange(0, [markup
length])];

int subCount = 0;

if (chunks.count >
1 && count > 0){

subCount = count;

}

int tag = 0;

for (NSTextCheckingResult *b
in chunks) {

[aString
addAttribute:NSForegroundColorAttributeName
value:colors[count]
range:b.range];

[aString
addAttribute:NSFontAttributeName
value:fonts[count]
range:b.range];

if (tag == subCount){

break;

}

tag++;

}

count++;

}

return aString;

}

/**

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 300, 0)];

label.numberOfLines = 0;

NSString *mark = @"我
的天 小桥
流水 am id text core last 即使对方空间哈上课大家互粉阿萨德发阿萨德空间凤凰ask
的";

NSArray *destrs = @[@"天",@"流水"];

NSArray *fonts = @[[UIFont systemFontOfSize:18],[UIFont systemFontOfSize:21]];

NSArray *colors = @[[UIColor redColor],[UIColor greenColor]];

NSAttributedString *str = [NSAttributedString getAtriWith:mark withDestStr:destrs andColor:colors andFont:fonts];

label.attributedText = str;

[label sizeToFit];

[self.view addSubview:label];

*/

@end

时间: 2024-07-31 08:03:22

给UIlabel的内容中的特定字符添加颜色,可以调整大小的相关文章

mfc中 控件 对话框 添加颜色 背景图片

1 设置对话框透明 在设置控件颜色中要使用 nCtlColor Contains one of the following values, specifying the type of control: CTLCOLOR_BTN   Button control CTLCOLOR_DLG   Dialog box CTLCOLOR_EDIT   Edit control CTLCOLOR_LISTBOX   List-box control CTLCOLOR_MSGBOX   Message b

在字符串中删除特定字符

63.在字符串中删除特定的字符.题目:输入两个字符串,从第一字符串中删除第二个字符串中所有的字符.例如,输入”They are students.”和”aeiou”, 则删除之后的第一个字符串变成”Thy r stdnts.”. 思路: 1. 位图法 将两个字符串分别转换成bitmap 然后对他们做异或xor运算,得到的结果即为排除了第二个字符串的所有字符, 然后对该结果依次与原字符串的所有字符进行与运算,结果不为零的即为所得    恩 位图真是个好东西啊...时间复杂度o(n+m) 花在了遍历

使用Ews API去删除邮件内容中包含特定关键字的邮件

之前分享过通过Ews去删除特定邮件主题的邮件方法,日常工作中会遇到只知道邮件内容中的部分关键字,需要去删除邮件.具体过程如下:1 说明通过EWS API去删除邮件中包含特定关键字方法,比传统的Search-Mailbox去删除特定邮件的方法更有效.EWS去查找邮箱项目一次可以返回1000个对象(可以通过策略解除限制),而Search-mailbox一次检索只能返回250个对象.并且Search-mailbox查询无法精确匹配,有时候会将筛选条件无关的内容查询出来.EWS API可以在非Excha

c之PAT刷体--字符串-01--从字符串中找到特定字符

字符串-01. 在字符串中查找指定字符(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 白洪欢(浙江大学) 输入一个字符串S,再输入一个字符c,要求在字符串S中查找字符c.如果找不到则输出"Not found":若找到则输出字符串S中从c开始的所有字符. 输入格式: 输入在第1行中给出一个不超过80个字符长度的.以回车结束的非空字符串:在第2行中给出一个字符. 输出格式: 在一行中按照题目要求输出结果. 输入样例

给输出的字符添加颜色

给输出的字符串加颜色 在Linux脚本中,可以通过echo -e ,结合特殊的数字给不同的字符加上颜色并显示. echo -e是为了对反斜线进行转义 内容的颜色可用数字表示,范围为30~37,每个数字代表一种颜色. 代码如下: echo -e "\033[30m黑色字study \033[0m" #<==30m表示黑色字 echo -e "\033[31m红色字study \033[0m" #<==31m表示红色字 echo -e "\033[

Python基础练习-004-提取字符串中的特定字符

已知一个字符串"axbyczdq", 如何得到"abcd" 分析过程:取出奇数位的字符再拼接,但是在python中,index从0开始,所以取偶数位.(学习重点:字符串与列表之间的相互转换) 原文地址:https://www.cnblogs.com/piaopiao-emmm/p/10064606.html

IOS 改变字符串中特定字符的颜色和大小。

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 300)]; lable.text = @"其中俩字是红色,大小为17"; NSRange rangeRmb=[lable.text rangeOfString:[NSString stringWithFormat:@"红色"]]; NSMutableAttributedString *rmbStr=[[NSMutableAtt

使用POI替换word中的特定字符/文字改进版

package com.xfzx.test.POI.main; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import jav

java 删除字符串中的特定字符

/** * Delete any character in a given String. * @param inString the original String * @param charsToDelete a set of characters to delete. * E.g. "az\n" will delete 'a's, 'z's and new lines. * @return the resulting String */ public static String