实现类似微博@..的点击文字跳转事件 iOS
来源:互联网 发布:广告投放算法 编辑:程序博客网 时间:2024/04/30 05:38
#import "firstVC.h"
#import "secondVC.h"
#import "userlist.h"
@interface firstVC ()<UITextViewDelegate>
@end
@implementation firstVC
{
NSMutableAttributedString *_attributedString;
NSArray * _dataArr;
}
- (void)viewDidLoad {
[superviewDidLoad];
// 给定一个用户列表
_dataArr =@[@"nonono",@"abc"];
}
-(void)createTextView{
UITextView * TView = [[UITextViewalloc]initWithFrame:CGRectMake(100,100,200,100)];
TView.backgroundColor = [UIColorlightGrayColor];
TView.editable =NO;
//创建富文本
_attributedString = [[NSMutableAttributedStringalloc]initWithString:self.textviewCS.text];
for (NSString * resstrin_dataArr) {
//遍历是否存在相同字符串
if ([self.textviewCS.textrangeOfString:resstr].location!=NSNotFound) {
// 发现相同的关键字
NSString * usernameCS = resstr;
NSString * totalname = [NSStringstringWithFormat:@"@%@",usernameCS];
//添加链接的方式
[_attributedStringaddAttribute:NSLinkAttributeName
value:[NSStringstringWithFormat:@"username://%@",usernameCS]
range:[[_attributedStringstring]rangeOfString:[NSStringstringWithFormat:@"%@",totalname]]];
// 设置点击时的样式
NSDictionary *linkAttributes =@{NSForegroundColorAttributeName: [UIColorgreenColor],NSUnderlineColorAttributeName: [UIColorlightGrayColor],NSUnderlineStyleAttributeName:@(NSUnderlinePatternSolid)};
// 添加链接文字
TView.linkTextAttributes = linkAttributes;
}
}
TView.attributedText =_attributedString;
TView.delegate =self;
[self.viewaddSubview:TView];
}
//点击的回调
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
if ([[URLscheme]isEqualToString:@"username"]) {
// 自定义的点击文字
NSString *username = [URLhost];
NSLog(@"%@",username);
secondVC * VC = [[secondVCalloc]init];
[self.navigationControllerpushViewController:VCanimated:YES];
VC.title= username;
returnNO;
}
returnYES;
}
//发布
- (IBAction)push:(UIButton *)sender {
[selfcreateTextView];
}
//跳转用户列表
- (IBAction)jumpUser:(UIButton *)sender {
userlist * userVC = [[userlistalloc]init];
[self.navigationControllerpushViewController:userVCanimated:YES];
userVC.dataArr=_dataArr;
}
@end
- 实现类似微博@..的点击文字跳转事件 iOS
- IOS点击文字展开滚轮实现,类似下拉列表
- iOS UITabelViewCell的点击跳转事件
- TextView实现点击部分文字跳转,实现微信朋友圈评论Item的显示效果
- RecyclerView Item点击事件实现 类似GridView
- Android富文本实现不同的文字添加点击事件
- Xamarin.Ios 实现imge 的点击事件
- TextView中的部分文字自定义颜色和点击事件(类似新浪微博带颜色的可点击字体)
- Button点击事件实现页面跳转的两种方法
- Android类似微博弹出窗口效果并且点击事件
- 使用UITextView实现文字点击事件
- Android TextView部分文字实现点击事件
- Kotlin 实现按钮点击跳转监听事件
- Android系统上实现类似按键精灵的效果(模拟触屏点击事件)
- ios实现点击复制文字到剪贴板
- 微博点击不同文字,触发不同事件
- iOS学习之点击TableViewcell实现跳转
- android重写webview长按时选择文字然后点击搜索按钮的事件,默认是chrome接受点击事件,现在跳转到360搜索页面
- angular 之表单验证
- C Primer Plus6-15
- CENTOS6.5添加JAVA环境变量
- C Primer Plus6-16
- 继承“JdbcDaoSupport”后,报“The hierarchy of the type AccoutDaoImpl is inconsistent”的解决方案
- 实现类似微博@..的点击文字跳转事件 iOS
- 在Appdelegate中获取当前的ViewController
- Android 透明(沉浸)状态栏 各个版本的完全解决方案
- Hadoop的调度器总结
- LeetCode165. Compare Version Numbers
- 10大好用的Linux实用工具推荐
- C中获取当前时间
- Linux 命令 Knowhow
- android打开网页,使用手机浏览器