NSMutableAttributedString-富文本的使用

富文本的使用步骤如下:

1. 创建一个 NSMutableAttributedString 的对象

2.设置 属性:

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;  --设置单个属性

- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range; --设置多个属性

其中,属性的设置有如下可以选择:

NSFontAttributeName	字号	UIFont 默认12
NSParagraphStyleAttributeName	段落样式	NSParagraphStyle
NSForegroundColorAttributeName	前景色	UIColor
NSBackgroundColorAttributeName	背景色	UIColor
NSObliquenessAttributeName	字体倾斜	NSNumber
NSExpansionAttributeName	字体加粗	NSNumber 比例 0就是不变 1增加一倍
NSKernAttributeName	字间距	CGFloat
NSUnderlineStyleAttributeName	下划线	1或0
NSUnderlineColorAttributeName	下划线颜色	UIColor
NSStrikethroughStyleAttributeName	删除线	1或0
NSStrikethroughColorAttributeName	删除线颜色	UIColor
NSStrokeColorAttributeName	same as ForegroundColor	UIColor
NSStrokeWidthAttributeName	字体描边	CGFloat
NSLigatureAttributeName	连笔字 没看出效果	1或0
NSShadowAttributeName	阴影	NSShawdow
NSTextEffectAttributeName	设置文本特殊效果,目前只有图版印刷效果可用	NSString
NSAttachmentAttributeName	设置文本附件,常用插入图片	NSTextAttachment
NSLinkAttributeName	链接	NSURL (preferred) or NSString
NSBaselineOffsetAttributeName	基准线偏移	NSNumber
NSWritingDirectionAttributeName	文字方向 分别代表不同的文字出现方向等等,我想你一定用不到它 - -	@[@(1),@(2)]
NSVerticalGlyphFormAttributeName	水平或者竖直文本 在iOS没卵用,不支持竖版	1竖直 0水平

3.给文本添加赋值 用  attributedText  这个字段

4.实例:

UILabel *attLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 300, 50)];
    attLabel.font = [UIFont systemFontOfSize:15];
    attLabel.textColor = [UIColor blueColor];
    [self.view addSubview:attLabel];

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"夕阳无限好,只是近黄昏"];
    attLabel.attributedText = str;

    NSDictionary *attDic = @{NSFontAttributeName:[UIFont systemFontOfSize:20],
                             NSForegroundColorAttributeName:[UIColor redColor],
                             NSBaselineOffsetAttributeName: @(-1.5),
                             NSStrikethroughStyleAttributeName:@(1),
                             NSStrikethroughColorAttributeName:[UIColor yellowColor],
                             NSKernAttributeName:@(4)};
    [str addAttributes:attDic range:NSMakeRange(6, 3)];

    attLabel.attributedText = str;

5.效果图:

时间: 2024-07-31 06:49:27

NSMutableAttributedString-富文本的使用的相关文章

NSMutableAttributedString(富文本)的简单使用

#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 84, self.view.frame.size.width-40, self.view.fr

NSMutableAttributedString富文本

一.ios5.0以前 1.首先导入CoreText.framework,并在需要使用的文件中导入: #import<CoreText/CoreText.h> 2.创建一个NSMutableAttributedString: NSMutableAttributedString *attriString = [[[NSMutableAttributedString alloc] initWithString:@"this is test!"] autorelease]; 非常常

NSMutableAttributedString 富文本删除线的用法

#import <UIKit/UIKit.h> //价格 NSString *priceStr = @"99元 剁手价66元"; NSMutableAttributedString *priceString = [[NSMutableAttributedString alloc] initWithString:priceStr]; [priceString addAttribute:NSFontAttributeName value:[UIFont systemFontOf

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

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

iOS之富文本总结

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

富文本NSAttributedString与NSMutableAttributedString

NSAttributedString NSAttributedString用来处理字符串,使在同一字符串内显示出不同属性的字符.(例如:用来处理字符串)总之就是可以设置字符串中指定位置或指定范围内字符的属性. 创建一个NSAttributedString对象 - (instancetype)initWithString:(NSString *)aString   使用字符串初始化对象 - (instancetype)initWithAttributedString:(NSAttributedSt

富文本(NSMutableAttributedString)

干货: - (void)viewDidLoad { [super viewDidLoad]; NSString * tempStr = @"貌似这个叫富文本"; NSMutableAttributedString * attStr = [self stringTurnToAttributeStringWithString:tempStr Font:25 TextColor:[UIColor redColor] Range:NSMakeRange(5, 3)]; UILabel * la

ios中label富文本的设置

1.修改不同文字和颜色 // 创建一个富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@"哈哈哈哈哈123456789"]; // 修改富文本中的不同文字的样式 [attri addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5

iOS常用技术-Label富文本

////  ViewController.m//  Label富文本////  Created by 大欢 on 16/1/19.//  Copyright © 2016年 bjsxt. All rights reserved.// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {    [super view

iOS富文本组件的实现—DTCoreText源码解析 数据篇

本文转载 http://blog.cnbang.net/tech/2630/ DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,通常用于在一些需要显示富文本的场景下代替低性能的UIWebView,来看看它是怎样解析和渲染HTML+CSS的,总体上分成两步: 数据解析—把HTML+CSS转换成NSAttributeString 渲染—用CoreText把NSAttributeString内容渲染出来,再加上图片等元素 本篇先介绍第一步,数据解