由于最近遇到的項目需要用到富文本開發(fā)卡辰,主要的也就是這些胞皱,有些屬性可以按著command鍵指著對應屬性一槍進去看看其他的屬性邪意,在此部多贅述
//
// ViewController.m
// NSMutableAttributedString
//
// Created by WangLe on 16/5/23.
// Copyright ? 2016年 WangLe. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UITextView * textView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 50, 200, 200)];
UILabel * myLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 300, 200, 200)];
myLabel.backgroundColor = [UIColor yellowColor];
_textView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:myLabel];
[self.view addSubview:_textView];
// 定義一個可變屬性字符串對象
NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:@"緩緩飄落的楓葉像思念我點燃燭火溫暖歲末的秋天激光掠過天邊被風掠過想你的思念"];
// 設置字體大小 range是設置范圍,下同
[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 5)];
// 設置字體顏色
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(2, 5)];
// 設置下劃線
[str addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(3, 7)];
// 設置字體樣式
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Geeza Pro" size:25] range:NSMakeRange(5, 5)];
//NSLog(@"字體集合%@",[UIFont familyNames]);
// 刪除線 常用于劃掉原價
[str addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(8, 5)];
// 刪除線的顏色(先設置刪除線再設置顏色)
[str addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(8, 5)];
// 設置空心字
[str addAttribute:NSStrokeWidthAttributeName value:@1 range:NSMakeRange(18, 5)];
// 插入圖片
NSTextAttachment * att = [[NSTextAttachment alloc]init];
att.image = [UIImage imageNamed:@"2"];
NSAttributedString * attStr = [NSAttributedString attributedStringWithAttachment:att];
[str insertAttributedString:attStr atIndex:25];
// 添加鏈接
[str addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://www.baidu.com"] range:NSMakeRange(30, 6)];
// 創(chuàng)建字體段落 行間距 格式
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 50;
paragraphStyle.firstLineHeadIndent = 30;// 設置為字體大小大兩倍
// _textView.attributedText = [[NSAttributedString alloc] initWithString:str.string attributes:@{
// NSFontAttributeName:[UIFont systemFontOfSize:15],
// NSParagraphStyleAttributeName:paragraphStyle
// }];
[str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, str.string.length)];
// 這句不能寫前面反砌,不然沒效果
_textView.attributedText = [str copy];
_textView.editable = NO;
_textView.delegate = self;
myLabel.attributedText = str;
myLabel.numberOfLines = 0;
}
/**
* 點擊圖片觸發(fā)代理事件
*/
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
{
NSLog(@"圖片%@", textAttachment);
return NO;
}
/**
* 點擊鏈接雾鬼,觸發(fā)代理事件
*/
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
[[UIApplication sharedApplication] openURL:URL];
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果圖如下