UITextView使用
- (UITextView*)contentTextView {
if (!_contentTextView) {
_contentTextView = [[UITextView alloc] init];
_contentTextView.textAlignment = NSTextAlignmentLeft;
_contentTextView.delegate = self;
// 必须禁止输入,否则点击将弹出输入键盘
_contentTextView.editable = NO;
_contentTextView.scrollEnabled = NO;
_contentTextView.backgroundColor = [UIColor clearColor];
_contentTextView.showsVerticalScrollIndicator = NO;
_contentTextView.showsHorizontalScrollIndicator = NO;
_contentTextView.contentInset = UIEdgeInsetsZero;
_contentTextView.scrollIndicatorInsets = UIEdgeInsetsZero;
_contentTextView.contentOffset = CGPointZero;
_contentTextView.textContainerInset = UIEdgeInsetsZero;
_contentTextView.textContainer.lineFragmentPadding = 0;
}
return _contentTextView;
}
textView赋值:
当文本内容里有链接需要点击跳转时:
NSString*string =@"自定义"];
NSMutableAttributedString* mutAttributedString = [
[NSMutableAttributedString alloc]
initWithString:string
attributes:@{
NSFontAttributeName : IS_FONT(15),
NSForegroundColorAttributeName : IS_COLOR_HEX(@"#202020")
}];
NSString* channelString = @"去频道";
[mutAttributedString
appendAttributedString:[[NSAttributedString alloc]
initWithString:channelString
attributes:@{
NSFontAttributeName : IS_FONT(15)
}]];
NSRange channelRange =
[mutAttributedString.string rangeOfString:channelString];
NSString* channelUrlString = [NSString
stringWithFormat:@"channel://%@", self.experienceModel.idString];
[mutAttributedString addAttribute:NSLinkAttributeName
value:channelUrlString
range:channelRange];
// 解析文本内链接
[self analyseUrlWithContentString:mutAttributedString];
contentTextView.attributedText =mutAttributedString;
textView代理方法
#pragma mark--- protocolTextView-UITextViewdelegate
- (BOOL)textView:(UITextView*)textView
shouldInteractWithURL