NSAttributedString

一、字符属性

字符属性:字符属性可以应用于NSAttributedString中

  • (1)、NSString *const NSFontAttributeName;(字体)
  • (2)、NSString *const NSParagraphStyleAttributeName;(段落)
  • (3)、NSString *const NSForegroundColorAttributeName;(字体颜色)
  • (4)、NSString *const NSBackgroundColorAttributeName;(字体背景色)
  • (5)、NSString *const NSLigatureAttributeName;(连字符)
  • (6)、NSString *const NSKernAttributeName;(字间距)
  • (7)、NSString *const NSStrikethroughStyleAttributeName;(删除线)
  • (8)、NSString *const NSUnderlineStyleAttributeName;(下划线)
  • (9)、NSString *const NSStrokeColorAttributeName;(边线颜色)
  • (10)、NSString *const NSStrokeWidthAttributeName;(边线宽度)
  • (11)、NSString *const NSShadowAttributeName;(阴影)
  • (12)、NSString *const NSVerticalGlyphFormAttributeName;(横竖排版)

常量

  • (1)、NSFontAttributeName(字体)
    该属性所对应的值是一个UIFont对象。该属性用于改变一段文本的字体。如故宫不指定该属性,则默认为12-point Helvetica(Neue).
  • (2)、NSParagraphStyleAttributeName(段落)
    该属性做对应的值是一个NSParagraphStyle对象。该属性在一段文本上应用多个属性。如果不指定该属性,则默认为NSParagraphStyle的defaultParagraphStyle方法返回的默认段落属性。
  • (3)、NSForegroundColorAttributeName(字体颜色)
    该属性对应的值是一个UIColor对象。该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色。
  • (4)、NSBackgroundColorAttributeName(字体背影色)
    该属性所对应的值是一个UIColor对象。该属性用于指定一段文本的背影颜色。如果不指定该属性,则默认无背景色。
  • (5)、NSLigatureAttributeName(连字符)
    该属性所对应的值是一个NSNumber对象(整数)。连体字符是指某些连在一起的字符,他们采用单个的图元符号。0表示没有连体字符。1表示使用默认的连体字符。2表示使用所有连体符号。默认值为1。(注意,iOS不支持值为2)。
  • (6)、NSKernAttributeName(字间距)
    该属性对应的值是一个NSNumber对象(整数)。字母紧排指定了用于调整字距的像素点数。字母紧排的效果依赖于字体。值为0表示不适用字母紧排。默认值为0;
  • (7)、NSStrikethroughStyleAttributeName(删除线)
    该属性对应的值是一个NSNumber对象(整数)。该值指定是否在文字上加上删除线,默认值是NSUnderlineStyleNone。改值参考“Underline Style Attributes”
  • (8)、NSUnderlineStyleAttributeName(下划线)
    该属性所对应的值是一个NSNumber对象(整数)。该值指定是否在文字上加上下划线。,默认值是NSUnderlineStyleNone;
  • (9)、NSStrokeColorAttributeName(边线颜色)
    该属性所对应的值是一个UIColor对象。该属性不指定(默认)则等同于NSForegroundColorAttributeName。否则,是定为删除线或下划线颜色。
  • (10)、NSStrokeWidthAttributeName边线宽度
    该属性所对应的值是一个NSNumber对象(小数)。该值改变描边宽度(相当于字体size的百分比)。默认为0,既不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为3.0。
  • (11)、NSShadowAttributeName(阴影)
    该属性所对应的值是一个NSShadow对象。默认为nil。
  • (12)、NSVerticalGlyphFormAttributeName(横竖排版)
    该属性所对应的值是一个NSNumber对象(整数)。0表示横排文本。1表示竖排文本。在iOS中,总是使用横排文本,0以外的值都未定义。

二、NSAttributeString管理字符串

NSAttributeString管理一个字符串,以及与该字符串中的单个字符或某些范围的字符串的相关的属性。

NSAttributedString就是用来存储这些信息的。具体实现时,NSAttributedString维护了一个NSString,用来保存最原始的字符串,另有一个NSDictionary用于保存个个子串/字符的属性。

有两种方法创建NSAttributedString

  • (1)、

    • (instancetype)initWithString:(NSString *)str;
    • (instancetype)initWithString:(NSString )str attributes:(NSDictionary )attrs;
    • (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;

例如:创建字符串关联Font属性

UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@“strigil" attributes:attrsDictionary];

可以看到上面创建的整个字符串关联了Font属性。如果希望只是对某一范围的字符串施加某个属性应该使用NSMutableAttributedString的setAttribute:range:方法。即: - (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;

例如:创建不同颜色的label

NSString*string = @"如何使得Label里的字符有不同的颜色?";
NSRange range = [string rangeOfString: @"Label”];
NSMutableAttributedString*attribute = [[NSMutableAttributedString alloc] initWithString: string];
[attribute beginEditing];
[attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor redColor]}range: range];
[attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor greenColor]}range: NSMakeRange(0, range.location)];
[attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor cyanColor]}range: NSMakeRange(range.location+ range.length, 5)];
[attribute endEditing];
  • (2)、

    • (instancetype)initWithFileURL:(NSURL )url options:(NSDictionary )options documentAttributes:(NSDictionary )dict error:(NSError )error NS_AVAILABLE_IOS(7_0);
    • (instancetype)initWithData:(NSData )data options:(NSDictionary )options documentAttributes:(NSDictionary )dict error:(NSError )error NS_AVAILABLE_IOS(7_0);

从HTML数据中创建 。有线程安全问题,使用时需注意。

例如:使用URL创建NSAttributedString

NSURL *html = [[NSBundle mainBundle] URLForResource: @"test" withExtension:@"html"];
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithFileURL:html options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
// 创建TextView,添加attributed str
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 320, 50)];
[textView setAttributedText:attrStr];

例如:使用Data创建NSAttributeString

NSString* strUrl = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"html"];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[strUrl dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
// 创建TextView,添加attributed str
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 320, 50)];
[textView setAttributedText:attributedString];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
NSAttributedString 转换成 PDF 格式可以通过以下步骤实现: 1. 创建一个 NSMutableData 对象来存储 PDF 数据。 2. 创建一个 UIGraphicsPDFRenderer 对象,指定页面大小和页面边距。 3. 在 renderer 上调用 drawPDFPage 方法,将 NSAttributedString 绘制到 PDF 页面上。 4. 关闭 renderer,将 PDF 数据写入 NSMutableData 对象中。 以下是示例代码: ```swift let pdfData = NSMutableData() let pageSize = CGSize(width: 8.5 * 72, height: 11 * 72) let pageRect = CGRect(origin: .zero, size: pageSize) let renderer = UIGraphicsPDFRenderer(bounds: pageRect) let attributes = [ NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12), NSAttributedString.Key.foregroundColor: UIColor.black ] let attributedString = NSAttributedString(string: "Hello, World!", attributes: attributes) let pdfMetaData = [ kCGPDFContextCreator: "My App", kCGPDFContextAuthor: "Me" ] let pdfFormat = UIGraphicsPDFRendererFormat() pdfFormat.documentInfo = pdfMetaData as [String: Any] renderer.writePDF(to: pdfData) { context in context.beginPage() attributedString.draw(in: pageRect) } // Save PDF data to file or display in a UIWebView ``` 在上面的示例中,我们创建了一个 NSMutableData 对象来存储 PDF 数据。然后,我们创建了一个 UIGraphicsPDFRenderer 对象,并指定了页面大小和边距。接下来,我们定义了一个 NSAttributedString,并使用指定的字体和颜色。 在 renderer 上调用 beginPage 方法,开始一个新的 PDF 页面。然后,我们调用 draw 方法,并将 NSAttributedString 绘制到页面上。最后,我们关闭 renderer 并将 PDF 数据写入 NSMutableData 对象中。你可以将 PDF 数据保存到文件或在 UIWebView 中显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoxiaobukuang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值