您的位置:首页 > 移动开发 > IOS开发

ios中label富文本的设置

2016-01-21 20:07 337 查看
1.修改不同文字和颜色
//创建一个富文本
NSMutableAttributedString*attri=[[NSMutableAttributedStringalloc]initWithString:@"哈哈哈哈哈123456789"];

//修改富文本中的不同文字的样式
[attriaddAttribute:NSForegroundColorAttributeNamevalue:[UIColorblueColor]range:NSMakeRange(0,5)];
[attriaddAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:20]range:NSMakeRange(0,5)];

//设置数字为红色
[attriaddAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(5,9)];
[attriaddAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:30]range:NSMakeRange(5,9)];

2,添加图片

//添加表情
NSTextAttachment*attch=[[NSTextAttachmentalloc]init];
//表情图片
attch.image=[UIImageimageNamed:@"d_aini"];
//设置图片大小
attch.bounds=CGRectMake(0,0,32,32);

//创建带有图片的富文本
NSAttributedString*string=[NSAttributedStringattributedStringWithAttachment:attch];
[attriappendAttributedString:string];
//

attri是上面定义的对象

//用label的attributedText属性来使用富文本self.textLabel.attributedText=attri;


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: