UITextView设置文字垂直居中和LinkAttribute
想要几行文字居中对齐的实现方式有多种,如使用:UILabel,UITextField,UITextView。以下是UITextView的简单使用。
NSMutableDictionary *ornamentAttributes = [NSMutableDictionary dictionary];
NSMutableParagraphStyle *ornamentParagraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
//设置text文字垂直居中
ornamentParagraph.alignment = NSTextAlignmentCenter;
//设置行间距
ornamentParagraph.lineSpacing = 5;
ornamentAttributes[NSParagraphStyleAttributeName] = ornamentParagraph;
//整个字符串的样式
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],NSParagraphStyleAttributeName: ornamentParagraph, NSForegroundColorAttributeName: UIColorFromRGB(0x7dd4f2)};
//设置可点击字符串的Attributes
NSDictionary *linkAttributes = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSLinkAttributeName: @"1", NSForegroundColorAttributeName: [UIColor whiteColor]};
//初始化字符串
NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"注册使用XXXXX,就表示您同意\nXXXXX的用户协议"] attributes: attributes];
[content addAttributes:linkAttributes range: NSMakeRange(content.length - 4, 4)];
self.textView.editable = NO;
self.textView.linkTextAttributes = linkAttributes;
self.textView.delegate = self;//设置UITextView的代理为self
self.textView.attributedText = content;
self.textView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_textView];
实现UITextViewDelegate的代理方法:
- (BOOL) textView:(UITextView )textView shouldInteractWithURL:(NSURL )URL inRange:(NSRange)characterRange
- (BOOL) textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
...
...
do some thing;
...
...
return YES;
}