NSMutableAttributedString简单使用

1、

//运行效果

self.attributeStringLabel.attributedText = [self getAttributedStringWithString:@"富为本属性字体" aString:@"颜色" aString:@"下划线"];

/**

*  NSMutableAttributedString 简单使用demo方法

*

*  @param str1 第一个属性字符串

*  @param str2 第一个属性字符串

*  @param str3 第一个属性字符串

*

*  @return NSMutableAttributedString

*/

- (NSMutableAttributedString *)getAttributedStringWithString:(NSString *)str1 aString:(NSString *)str2 aString:(NSString *)str3{

NSString *normalString = [NSString stringWithFormat:@"%@--%@--%@", str1, str2, str3];

NSMutableAttributedString *attritedString = [[[NSMutableAttributedString alloc] initWithString:normalString] autorelease];

//每个属性的NSRange

NSRange range1 = [normalString rangeOfString:str1];

NSRange range2 = [normalString rangeOfString:str2];

NSRange range3 = [normalString rangeOfString:str3];

//每个NSRange里的具体属性(所有属性可以到UIKit框架里面的NSAttributedString查询)

NSDictionary *attrites1 = [NSDictionary dictionaryWithObject:[UIFont fontWithName:@"Palatino-Roman" size:20.0f] forKey:NSFontAttributeName];

NSDictionary *attrites2 = [NSDictionary dictionaryWithObject:[UIColor purpleColor] forKey:NSForegroundColorAttributeName];

NSDictionary *attrites3 = [NSDictionary dictionaryWithObject:[UIColor redColor] forKey:NSUnderlineColorAttributeName];

NSDictionary *attrites4 = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"1"] forKey:NSUnderlineStyleAttributeName];

//设置属性

[attritedString addAttributes:attrites1 range:range1];

[attritedString addAttributes:attrites2 range:range2];

[attritedString addAttributes:attrites3 range:range3];

[attritedString addAttributes:attrites4 range:range3];

return attritedString;

}

2、iOS7以后新添加了API可以通过URL和流来过的富文本

NSMutableAttributedString *attritedString1 = [[NSMutableAttributedString alloc] initWithFileURL:<#(NSURL *)#> options:<#(NSDictionary *)#> documentAttributes:<#(NSDictionary **)#> error:<#(NSError **)#>]

NSMutableAttributedString *attritedString2 = [NSMutableAttributedString alloc] initWithData:<#(NSData *)#> options:<#(NSDictionary *)#> documentAttributes:<#(NSDictionary **)#> error:<#(NSError **)#>;

NSMutableAttributedString简单使用

时间: 2025-01-11 02:43:58

NSMutableAttributedString简单使用的相关文章

iOS NSMutableAttributedString 简单使用

NSMutableAttributedString 部分属性介绍 /** NSFontAttributeName --- 设置字体大小 *//** NSForegroundColorAttributeName --- 设置字体颜色 *//** NSParagraphStyleAttributeName --- 设置段落格式 (暂无明显变化) *//** NSBackgroundColorAttributeName --- 设置字体的背景颜色 *//** NSLigatureAttributeNa

NSMutableAttributedString的简单用法

一直对这个类的用法有点糊涂,今天抽了一个时间研究了一下 1.首先来看系统的api(方法) 1 @interface NSMutableAttributedString : NSAttributedString 2 3 - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str; 4 - (void)setAttributes:(nullable NSDictionary<NSString *, id> *

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

iOS - NSMutableAttributedString的简单使用

1. 初始化 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"YOUR_STRING"]; 还有一种初始化方法,直接在初始化时给String赋属性值: - (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary<NSString *,

iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)

在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦,而且很多UILabel的属性也不起作用了,效果都不理想.后来了解到NSMuttableAttstring(带属性的字符串),上面的一些需求都可以很简便的实现. 实例化方法和使用方法 实例化方法: 使用字符串初始化 - (id)initWithString:(NSString *)str; 例: N

iOS富文本-NSAttributedString简单封装

直接调用系统的写起来比较麻烦,封装一下 因为要简单所以就写类方法 WJAttributeStyle 基类 #import <Foundation/Foundation.h>#import <UIKit/UIKit.h>/** *  基类富文本 */@interface WJAttributeStyle : NSObject @property (nonatomic,strong)NSString *attributeName;@property (nonatomic,strong)

iOS实现简单图文混排效果

在很多新闻类或有文字展示的应用中现在都会出现图文混排的界面例如网易新闻等,乍一看去相似一个网页,其实这样效果并非由UIWebView 加载网页实现.现在分享一种比较简单的实现方式 iOS sdk中为我们提供了一套完善的文字排版开发组件:CoreText.CoreText库中提供了很多的工具来对文本进行操作,例如CTFont.CTLine.CTFrame等.利用这些工具可以对文字字体每一行每一段落进行操作. 此例中默认图片都在右上方,且为了美观和开发简便设定所占宽度都相同. 首先,需要引入Core

TYAttributedLabel——简单,强大的iOS属性文本控件

本文转载至 http://www.mobile-open.com/2015/86578.html TYAttributedLabel 简单,强大的属性文本的控件(无需了解CoreText),支持图文混排显示,支持添加链接,image和UIView控件,支持自定义排版显示 更新: v2.4 修复imge放大bug,新增imageAlignment 和 autolayout支持,以及相应的demo,感谢xinzhengzhang,nonstriater v2.3 新增 做题demo,代码优化(4s真

简单的Coretext 图文混排

在很多新闻类或有文字展示的应用中现在都会出现图文混排的界面例如网易新闻等,乍一看去相似一个网页,其实这样效果并非由UIWebView 加载网页实现.现在分享一种比较简单的实现方式 iOS sdk中为我们提供了一套完善的文字排版开发组件:CoreText.CoreText库中提供了很多的工具来对文本进行操作,例如CTFont.CTLine.CTFrame等.利用这些工具可以对文字字体每一行每一段落进行操作. 此例中默认图片都在右上方,且为了美观和开发简便设定所占宽度都相同. 1.