富文本的实现(NSMuttableAttstring/NSMutableAttributedString)
设置段落
1.NSMutableParagraphStyle
行间距
@property(NS_NONATOMIC_IOSONLY) CGFloat lineSpacing;
段落与段落之间的间距
@property(NS_NONATOMIC_IOSONLY) CGFloat paragraphSpacing;
文本对齐
@property(NS_NONATOMIC_IOSONLY) NSTextAlignment alignment;
首行缩进
@property(NS_NONATOMIC_IOSONLY) CGFloat firstLineHeadIndent;
段落左边缩进
@property(NS_NONATOMIC_IOSONLY) CGFloat headIndent;
段落右边缩进
@property(NS_NONATOMIC_IOSONLY) CGFloat tailIndent;
断行方式
@property(NS_NONATOMIC_IOSONLY) NSLineBreakMode lineBreakMode;
最小行高
@property(NS_NONATOMIC_IOSONLY) CGFloat minimumLineHeight;
最大行高
@property(NS_NONATOMIC_IOSONLY) CGFloat maximumLineHeight;
句子方向
@property(NS_NONATOMIC_IOSONLY) NSWritingDirection baseWritingDirection;
段首空间
@property(NS_NONATOMIC_IOSONLY) CGFloat paragraphSpacingBefore;
使用方法:
NSMutableAttributedString 设置段落的属性
NSFontAttributeName 字体大小
NSParagraphStyleAttributeName 段落样式
NSDictionary *dict = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:pStyle};
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string attributes:dict];
2.设置指定文字的各种属性
[string addAttribute:对应的属性
value:[UIColor redColor]
range:NSMakeRange(2, 2)];
addAttribute属性值:
NSFontAttributeName 文字大小
NSForegroundColorAttributeName 文字颜色
NSBackgroundColorAttributeName 文字所在区域的背景颜色
NSLigatureAttributeName 0为不连笔;1为默认连笔,也是默认值;
NSKernAttributeName 字距属性
NSStrikethroughStyleAttributeName 删除线 默认没有删除线
NSUnderlineStyle枚举值
NSLinkAttributeName 增加链接文字 要使用textView label没有效果
[string addAttribute:NSLinkAttributeName
value:[NSURL URLWithString:@”http://www.baidu.com“]
range:NSMakeRange(2, 20)];
textView.editable = NO; 编辑框不能处于编辑状态
返回值表示是否使用系统浏览器打开链接
- (BOOL)textView:(UITextView )textView shouldInteractWithURL:(NSURL )URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0)
{
NSLog(@”%@”,URL);
return NO;
}
NSShadowAttributeName 设置文字阴影
NSObliquenessAttributeName 设置倾斜度
插入图片
NSString *imageName = [NSString stringWithFormat:@”%d”, arc4random()%4];
//创建附件
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
textAttachment.image = [UIImage imageNamed:imageName];; //设置图片
textAttachment.bounds = CGRectMake(0, -5, 25, 25); //图片范围
//创建不可变富文本(包含图片附件)
NSAttributedString *textString = [NSAttributedString attributedStringWithAttachment:textAttachment];
[string insertAttributedString:textString atIndex:40];
Demo
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.//NSAttributedString
//NSMutableAttributedString
//NSFontAttributeName 文字的大小NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@”哈斯卡减肥哈客户” attributes:nil];
[attrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24.0f] range:NSMakeRange(0, 4)];
//[attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4, 4)];
[attrString addAttribute:NSKernAttributeName value:@1 range:NSMakeRange(0, attrString.length)];
[attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, attrString.length)];
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, attrString.length)];
//[attrString addAttribute:NSObliquenessAttributeName value:@1 range:NSMakeRange(0, attrString.length)];
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor orangeColor];
shadow.shadowOffset = CGSizeMake(-5, 5);//shadow.shadowBlurRadius = 10;
[attrString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, attrString.length)];
//插入图片
NSTextAttachment *textImage = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
textImage.image = [UIImage imageNamed:@”1”];
textImage.bounds = CGRectMake(0, 0, 20, 20);NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:textImage];
[attrString insertAttributedString:string atIndex:4];UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 375, 40)];
label.attributedText = attrString;[self.view addSubview:label];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 100, 375, 100)];
NSMutableAttributedString *linkString = [[NSMutableAttributedString alloc] initWithString:@”www.baidu.com”];
[linkString addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@”http://www.baidu.com“] range:NSMakeRange(0, linkString.length)];
textView.attributedText = linkString;
textView.editable = NO;
textView.delegate = self;
[self.view addSubview:textView];
}
//是否用浏览器打开该链接 YES
- (BOOL)textView:(UITextView )textView shouldInteractWithURL:(NSURL )URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0)
{
NSLog(@”%@”,URL);
return YES;
}