UILabel通過富文本可以實現圖文混排,但是想要實現文字的點擊效果好像不容易實現,這里有2種方法可以達到效果
-
YYLabel -->YYText框架
參考我之前的博客:http://www.cnblogs.com/qqcc1388/p/6709336.html -
UITextView 系統原生的UI框架實現文字點擊效果
#import "ViewController.h"
@interface ViewController ()<UITextViewDelegate>
@property (nonatomic,strong) UITextView *textView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, self.view.bounds.size.width - 20, 44)];
textView.delegate = self;
textView.editable = NO;
textView.scrollEnabled = NO;
self.textView = textView;
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"客服電話:400-775-5333 轉4 (每天9:00-20:00)"];
[attr setAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(0, attr.length)];
//點擊了鏈接撥打電話
[attr setAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:0 green:147/255.0 blue:238/255.0 alpha:1],
NSFontAttributeName:[UIFont systemFontOfSize:14],
NSLinkAttributeName:@"telprompt://400-775-5333"}
//如果點擊了之后跳轉網頁可以將NSLinkAttributeName后面的修改為想要跳轉的網頁
range:NSMakeRange(5, 15)];
textView.attributedText = attr;
[self.view addSubview:textView];
}
#pragma mark - 點擊了鏈接會走這個方法 可以在這里處理操作
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
return YES;
}
@end
方法2效果是可以達到,但是由於textView本身點擊后會有UIMenuController彈出效果,比較影響體驗,找了很多方法也沒有找到很好的解決方案,加上實現上面偏復雜,掌握實現原理即可,推薦方法1來實現類似的效果。
