博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS之TextView属性设置
阅读量:5275 次
发布时间:2019-06-14

本文共 2333 字,大约阅读时间需要 7 分钟。

UIFontDescriptor *bodyFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
self.textView.font = [UIFont fontWithDescriptor:bodyFontDescriptor size:0];
 
self.textView.textColor = [UIColor blackColor];
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.scrollEnabled = YES;
 
// Let's modify some of the attributes of the attributed string.
// You can modify these attributes yourself to get a better feel for what they do.
// Note that the initial text is visible in the storyboard.
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.textView.attributedText];
 
NSString *text = self.textView.text;
 
// Find the range of each element to modify.
NSRange boldRange = [text rangeOfString:NSLocalizedString(@"bold", nil)];
NSRange highlightedRange = [text rangeOfString:NSLocalizedString(@"highlighted", nil)];
NSRange underlinedRange = [text rangeOfString:NSLocalizedString(@"underlined", nil)];
NSRange tintedRange = [text rangeOfString:NSLocalizedString(@"tinted", nil)];
 
// Add bold.
UIFontDescriptor *boldFontDescriptor = [self.textView.font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
UIFont *boldFont = [UIFont fontWithDescriptor:boldFontDescriptor size:0];
[attributedText addAttribute:NSFontAttributeName value:boldFont range:boldRange];
 
// Add highlight.
[attributedText addAttribute:NSBackgroundColorAttributeName value:[UIColor aapl_applicationGreenColor] range:highlightedRange];
 
// Add underline.
[attributedText addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:underlinedRange];
 
// Add tint.
[attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor aapl_applicationBlueColor] range:tintedRange];
 
// Add an image attachment.
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
UIImage *image = [UIImage imageNamed:@"text_view_attachment"];
textAttachment.image = image;
textAttachment.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
 
NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedText appendAttributedString:textAttachmentString];
 
self.textView.attributedText = attributedText;

转载于:https://www.cnblogs.com/wcLT/p/4745857.html

你可能感兴趣的文章
Java 时间处理实例
查看>>
Java 多线程编程
查看>>
Java 数组实例
查看>>
mysql启动过程
查看>>
2017前端面试题总结
查看>>
Http GetPost网络请求
查看>>
SWIFT国际资金清算系统
查看>>
Sping注解:注解和含义
查看>>
站立会议第四天
查看>>
如何快速掌握一门技术
查看>>
利用AMPScript获取Uber用户数据的访问权限
查看>>
vagrant 同时设置多个同步目录
查看>>
python接口自动化28-requests-html爬虫框架
查看>>
生成随机数的模板
查看>>
Mysql 数据库操作
查看>>
转:linux终端常用快捷键
查看>>
UVa 11059 最大乘积
查看>>
数组分割问题求两个子数组的和差值的小
查看>>
composer 报 zlib_decode(): data error
查看>>
hdu 3938 并查集
查看>>