您的位置:首页 > 移动开发 > IOS开发

IOS7-TextKit

2014-09-07 22:56 260 查看
-------------------------------------------TextKitDemo---------------------------------------

- (void)viewDidLoad
{
[super viewDidLoad];

_textView = [[UITextView alloc]initWithFrame:CGRectMake(0,20,320, 200)];
_textView.text = @"sldjdsflsdflsflskfls水电费了解多少分 双方就发生了地方我 都是浪费了多少积分离开多少份简历上发动机我收到了房间里的减肥了手机费我lfsdjfdfks发生的楼房的i地上了飞机了时间了i是的房间里的手机发送了地方历史的房价都是浪费";
_textView.font = [UIFont systemFontOfSize:14];
[self.view addSubview:_textView];

NSTextStorage *textStorage = [[NSTextStorage alloc]initWithString:_textView.text];//用于文本存储字符和相关属性

NSLayoutManager *layoutManager = [[NSLayoutManager alloc]init];//用于管理文本存储
[textStorage addLayoutManager:layoutManager];

CGRect textViewRect = CGRectInset(self.view.bounds, 10.0, 20.0);//(10, 20,300, 440),view的宽减20,高减40。
NSLog(@"rect = %@",NSStringFromCGRect(textViewRect));
_textContainer = [[NSTextContainer alloc]initWithSize:textViewRect.size];//排版区域
[layoutManager addTextContainer:_textContainer];

[_textView removeFromSuperview];
_textView = [[UITextView alloc]initWithFrame:textViewRect textContainer:_textContainer];/*根据排版区域初始化*/
[self.view addSubview:_textView];

//设置印刷效果
[textStorage beginEditing];

NSDictionary *attrsDic = @{NSTextEffectAttributeName: NSTextEffectLetterpressStyle};
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:_textView.text  attributes:attrsDic];
[textStorage setAttributedString:attrStr];

[self markWord:@"我" inTextStorage:textStorage];
[self markWord:@"i" inTextStorage:textStorage];

[textStorage endEditing];

//2.嵌入图片
_tImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"combg_btn@2x"]];
_tImageView.frame = CGRectMake(80,46,150, 70);
[self.view addSubview:_tImageView];
//
[self.view insertSubview:_textView belowSubview:_tImageView];
_textView.textContainer.exclusionPaths = @[[self translateBezierPath]];/*设置环绕路径*/

//3监听用户设置字体大小
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(preferredContentSizeChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
}

-(void)preferredContentSizeChanged:(NSNotification *)notification
{
self.textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
}

-(void)markWord:(NSString *)word inTextStorage:(NSTextStorage *)textStorage
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:word options:0 error:nil];
NSArray *matches = [regex matchesInString:_textView.text options:0 range:NSMakeRange(0, [_textView.text length])];

for (NSTextCheckingResult *match in matches)
{
NSRange matchRange = [match range];
//改变属性匹配出来的i和我字符
[textStorage addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:matchRange];
}

}

-(UIBezierPath *)translateBezierPath
{
CGRect imageRect = [self.textView convertRect:_tImageView.frame fromView:self.view];
UIBezierPath *newPath = [UIBezierPath bezierPathWithRect:imageRect];
return newPath;
}




2.label设置内容样式

  NSString *content=[NSString stringWithFormat:@"订货卡片条款号 : %@",text];
//设置段落样式
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[paragraphStyle setParagraphSpacing:34];
NSDictionary *para = @{NSParagraphStyleAttributeName:paragraphStyle};
//设置文字样式
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:content
attributes:para];
NSRange range = [content rangeOfString:text];
[attrString addAttribute:NSForegroundColorAttributeName
value:[UIColor cyanColor]
range:range];
UIFont *font = [UIFont systemFontOfSize:23];
[attrString addAttribute:NSFontAttributeName
value:font
range:range];
self.tipLabel.attributedText = attrString;
weakSelf.tipLabel.text =content;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: