富文本NSMutableAttributedString的相关设置
2016-01-20 16:30
555 查看
1、NSMutableAttributedString总的来说一共有21个属性:
2、在代码中的相应实现
3、运行这段代码的具体效果如图:
4、NSLinkAttributeName
之所以把
需要实现UITextView的代理方法
要设置代理:<UITextViewDelegate>
同时实现代理方法:
如果返回NO则text中的链接没有点击效果 。
NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12 NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色 NSBackgroundColorAttributeName 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色 NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符 NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄 NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数) NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色 NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似 NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色 NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果 NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象 NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象 NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用: NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏 NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾 NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本 NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写 NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本 NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址 NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排 NSParagraphStyleAttributeName 设置文本段落排版格式,取值为 NSParagraphStyle 对象
2、在代码中的相应实现
NSString *str = @"Hello World!"; NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:str]; [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, 6)]; // [attributeString addAttribute:NSBackgroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(6, 6)]; [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(6, 6)]; [attributeString addAttribute:NSUnderlineStyleAttributeName value:@1 range:NSMakeRange(6, 6)]; [attributeString addAttribute:NSUnderlineColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(6, 6)]; [attributeString addAttribute:NSLigatureAttributeName value:@1 range:NSMakeRange(6, 6)]; [attributeString addAttribute:NSKernAttributeName value:@10 range:NSMakeRange(6, 6)]; [attributeString addAttribute:NSStrikethroughStyleAttributeName value:@10 range:NSMakeRange(6, 6)]; [attributeString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blackColor] range:NSMakeRange(6, 6)]; [attributeString addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, 3)]; [attributeString addAttribute:NSBaselineOffsetAttributeName value:@20 range:NSMakeRange(6, 1)]; [attributeString addAttribute:NSVerticalGlyphFormAttributeName value:@1 range:NSMakeRange(6, 6)]; [attributeString addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"https://www.baidu.com"] range:NSMakeRange(6, 6)]; _textview.editable = NO; _textview.attributedText = attributeString;
3、运行这段代码的具体效果如图:
4、NSLinkAttributeName
之所以把
NSLinkAttributeName属性单独列出来,是因为在 UILabel 和 UITextField 中是无法使用该属性的。更准确点说是在UILabel 和 UITextField 中无法实现点击链接启动浏览器打开一个URL地址,因为在此过程中用到了一个代理函数。只能用在 UITextView 中。
NSLinkAttributeName的对象是 NSURL 类型 或 NSString,但是优先使用 NSURL。
需要实现UITextView的代理方法
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange,在该方法中,返回 YES,则会打开URL地址,返回 NO则不会。
要设置代理:<UITextViewDelegate>
同时实现代理方法:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{ return YES; }
如果返回NO则text中的链接没有点击效果 。
相关文章推荐
- 模式识别之bpnn---神经网络训练
- 使用UIAlertController报错
- gcc 编译简单loop error:error: ‘for’ loop initial declarations are only allowed in C99 mode
- Linux C语言 内存越界问题总结
- Android多tab实现方式
- CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
- 转义字符
- 17.View the Exhibit and examine the data in the EMPLOYEES table.
- odps编写UDF的实现
- js 打印指定页面部分打印
- Jenkins+Maven+Git搭建持续集成和自动化部署的配置手记
- 按钮事件响应的注意点
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- 数据库事务的隔离级别
- 仿淘宝详情页上拉看详情
- easyui控件与Activex控件遮挡问题
- .NET的三种缓存(页面缓存,控件缓存,自定义缓存)
- C# 对象与JSON串互相转换
- css之clearfix清除浮动
- spark idea 源码阅读环境搭建