UILabel设置富文本格式显示

  1. 实例化方法和使用方法

实例化方法:

使用字符串初始化

- (id)initWithString:(NSString *)str;

例:

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天气不错呀"];

- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;

字典中存放一些属性名和属性值,如:

NSDictionary *attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:

[UIFontsystemFontOfSize:15.0],NSFontAttributeName,

[UIColorredColor],NSForegroundColorAttributeName,

NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天气不错呀" attributes:attributeDict];

- (id)initWithAttributedString:(NSAttributedString *)attester;

使用NSAttributedString初始化,跟NSMutableString,NSString类似

使用方法:

为某一范围内文字设置多个属性

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;

为某一范围内文字添加某个属性

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

为某一范围内文字添加多个属性

- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;

移除某范围内的某个属性

- (void)removeAttribute:(NSString *)name range:(NSRange)range;

  1. 常见的属性及说明

NSFontAttributeName  字体

NSParagraphStyleAttributeName       段落格式

NSForegroundColorAttributeName     字体颜色

NSBackgroundColorAttributeName    背景颜色

NSStrikethroughStyleAttributeName  删除线格式

NSUnderlineStyleAttributeName       下划线格式

NSStrokeColorAttributeName            删除线颜色

NSStrokeWidthAttributeName           删除线宽度

NSShadowAttributeName                 阴影

2.实例设置文本的属性

//从网络获取的数组中拿到字典

NSDictionary *dataDic = @{@"Position":@"苏州",

@"TrueName":@"苏州***信息科技有限公司"};

//变换字体大小及颜色positionstr拼接设置一定的格式如(),{},[]……

NSString *positionStr = [NSString stringWithFormat:@"(%@)",dataDic[@"Position"]];

NSString *str = [NSString stringWithFormat:@"%@%@",dataDic[@"TrueName"],positionStr];

//初始化

NSMutableAttributedString *mutableStr = [[NSMutableAttributedString alloc]initWithString:str];

//限定范围

NSRange trueNameRange = [str rangeOfString:dataDic[@"TrueName"]];

NSRange positionRange = [str rangeOfString:positionStr];

//设置范围属性

[mutableStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:trueNameRange];

[mutableStr addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:positionRange];

[mutableStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16] range:trueNameRange];

[mutableStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13] range:positionRange];

//取出获得的数据,直接给label

nameLabel.attributedText = mutableStr;

具体的效果图,可以自己测试之后看到

时间: 2024-08-23 12:08:25

UILabel设置富文本格式显示的相关文章

iOS 富文本格式字符 以及处理iOS8下textview设置富文本后输入bug

iOS的富文本设置,在iOS8下在textView的代理方法中设置textview的attributedText时,会出现富文本格式未加到text上的情况,可以在初始化时将文本属性赋给textview的typingAttributes.代码如下: NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 7;// 字体的行间距 UIF

简易使用UILabel的富文本

使用效果: 源码: NSString+YX.h    NSString+YX.m // // NSString+YX.h // YXKit // // Copyright (c) 2014年 Y.X. All rights reserved. // #import <Foundation/Foundation.h> #import "ConfigAttributedString.h" @interface NSString (YX) // 创建富文本并配置富文本(NSArr

PHP UEditor富文本编辑器 显示 后端配置项没有正常加载,上传插件不能正常使用

UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码... 问题描述 我的编辑器在本地测试的时候没问题,但是上传到服务器上之后,上传图片.视频等文件的时候出错,显示后端配置项没有正常加载,上传插件不能正常使用!如图: 解决方法 可以测试一下 php 代码是否正确执行,在浏览器打开 ueditor/controller.php 对应的路径,看看是否有下面的返回值. { state: "请求地址出错&

UILabel Attributed 富文本

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Menlo; color: #703daa } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px "PingFang SC"; color: #008400 } p.p4 {

&lt;pre&gt; 保留文本格式显示在网页上

<code> 标签 解释:保留输入的格式空格等不变,原样显示在网页上 例如: <pre> 通知 即日起不再提供公共设施 个店铺需自行准备. 望周知~!! 2020/10/10 </pre> 原文地址:https://www.cnblogs.com/rygl/p/11829441.html

Excel 用于批量把单元格设置为&quot;文本格式保存的数字&quot;的宏

首先把所有的数字录入(或者导出为)井号+数字的格式,比如“#3333333323424234234234”,然后运行下面的宏: Sub Num2Text() If Not TypeOf Application.Selection Is Range Then MsgBox "You must select cells!" Return End If For Each cell In Application.Selection.Cells If cell.Text <> &qu

cxSpreadBook 或 cxSpreadSheet 设置成文本格式

uses cxSSStyles,cxSSDesigner; Type TStyleAccess = class(TcxSSCellStyle); TSheetAccess = class(TcxSSBookSheet); procedure TForm2.Button16Click(Sender: TObject); var Row,Col: Integer; AStyle: TcxSSCellStyle; CurrentStyle: integer; begin TSheetAccess(cx

QTableView使用HTML显示富文本

对于QTableView中的显示,我们前面介绍过很多种,其中包括:文本.进度条.复选框等,今天我们介绍一下关于富文本的显示. 可能绝大多数小伙伴会通过QAbstractTableModel中的data来实现,可是现实告诉我们,那是行不通的,那么我们如何去显示呢?请看正文. 简述 效果 源码 分析 效果 源码 下面我们使用QAbstractItemDelegate来进行富文本的显示,主要通过QTextDocument设置HTML代码来实现. void CheckBoxDelegate::paint

转载的一个富文本,挺实用的

文章内容大纲 1.NSMutableAttributedString的基本使用 2.NSMutableAttributedString的简易封装 3.使用开源代码GOBMarkupPaser处理富文本 4.UITextKit简介 5.编程思想的相关思考 前言 富文本使用案例: 这里我自己也用了富文本实现了简单的却也是常用的例子: 对于最后面的¥50中划线这种设置,估计只有富文本最好用了. 在IOS或者Mac OS X通过UIKit提供的用来显示字符串控件有三个: UILable,UITextFi