Objective-C属性字符串NSAttributedString
来源:互联网 发布:数据产品运营专员 编辑:程序博客网 时间:2024/05/30 12:30
NSAttributedString是Objective-C中的属性字符串类,GitHub上也有很多第三方,用得较多的是TTTAttributedLabel,这里给大家介绍一下系统NSAttributedString类来实现富文本,并可实现点击事件,同时点击事件可携带参数。
因为要做点击事件,所以我们用UITextView,首先声明一个UITextView属性:
@property (nonatomic, strong) UITextView *textView;
NSAttributedString中NSFontAttributeName是用来设置文字字体的,有很多可以设置的属性,这里介绍一些常用的,其他的用到的话修改一下key-value就可以了:
//NSFontAttributeName:文字字体- (void)setAttributeCorlorAndSize{ _textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 100)]; _textView.editable = NO; _textView.selectable = NO; _textView.scrollEnabled = NO; _textView.autoresizingMask = UIViewAutoresizingFlexibleHeight; [self.view addSubview:_textView]; //初始化属性字符串 NSMutableAttributedString * aAttributedString = [[NSMutableAttributedString alloc] initWithString:@"富文本:文字颜色 字体大小 背景色 下划线 空心 点击事件"]; //文字颜色 [aAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4, 4)]; //文字大小 [aAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(9, 4)]; //文字背景色 [aAttributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(14, 3)]; //下划线 NSUnderlineColorAttributeName设置下划线颜色 [aAttributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:NSMakeRange(18, 3)]; //空心 [aAttributedString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInt:3] range:NSMakeRange(22, 2)]; //点击事件携带一个NSInteger类型,值为110的参数 [aAttributedString addAttribute:@"tapID" value:[NSNumber numberWithInteger:110] range:NSMakeRange(25, 4)]; [_textView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(textTapped:)]]; NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; //行间距 paragraphStyle.lineSpacing = 15.0; [aAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, aAttributedString.length)]; _textView.attributedText = aAttributedString;}
点击事件实现:
-(void)textTapped:(UITapGestureRecognizer*)recognizer{ NSLayoutManager *layoutManager = _textView.layoutManager; CGPoint location = [recognizer locationInView:_textView]; location.x -= _textView.textContainerInset.left; location.y -= _textView.textContainerInset.top; NSUInteger characterIndex = [layoutManager characterIndexForPoint:location inTextContainer:_textView.textContainer fractionOfDistanceBetweenInsertionPoints:NULL]; if(characterIndex < _textView.textStorage.length){ NSRange range; id number = [_textView.attributedText attribute:@"tapID" atIndex:characterIndex effectiveRange:&range]; NSNumber *value_id = number; NSLog(@"%@", value_id); }}
NSParagraphStyleAttributeName用来设置段落样式(字符串通过“\n”进行分段,此设置必须在lable.numberOfLines = 0时有效,value通过NSMutableParagraphStyle设置,它有以下属性)
- (void)setAttributeParagraph{ UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 130, SCREEN_WIDTH, 200)]; label2.numberOfLines = 0; [self.view addSubview:label2]; NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:@"NSParagraphStyleAttributeName 段落样式(字符串通过“\n”进行分段,此设置必须在lable.numberOfLines = 0时有效,value通过NSMutableParagraphStyle设置,它有以下属性)"]; //段落样式 NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; //段落间距 paragraphStyle.paragraphSpacing = 20.0; [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)]; label2.attributedText = attributedString;}
效果图,点击事件就不演示了,但点击“点击事件”四个字的时候,控制台打印所携带的参数110。
0 0
- Objective-C属性字符串NSAttributedString
- NSAttributedString 详解(属性字符串)
- NSAttributedString字符串属性
- iOS-属性字符串NSAttributedString
- iOS NSAttributedString(属性字符串)
- 属性字符串NSAttributedString简易教程
- 带属性的字符串 NSMutableAttributedString/NSAttributedString
- 带属性的字符串 NSMutableAttributedString/NSAttributedString
- iOS - NSAttributedString字符串属性详解
- NSAttributedString 属性
- NSAttributedString属性
- UILabel的一些常用属性、NSAttributedString属性字符串
- UIKit 带属性的字符串 NSAttributedString、NSMutableAttributedString、NSTextStorge(未完成)
- Objective-C属性介绍
- Objective-C属性介绍
- Objective-C属性介绍
- Objective-C 方法、属性
- Objective-C 方法、属性
- leetcode题集——permutations
- c++——数组指针和指针数组
- websocket学习笔记
- UVA 340-Master-Mind Hints
- Beauty of Programming 1
- Objective-C属性字符串NSAttributedString
- c#——Winform DatagridView上显示下拉树(二)
- java中double类型的数保留两位小数的输出方法并且小数点前面是0的时候如何处理
- 20160714 数组
- 图解tomcat
- Node.js模块导出exports 和 module.exports 的区别
- 2016华为暑期实习生编程题之和尚挑水
- html中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
- hdu 5464(简单dp)