ios lable中电话号码点击拨打电话
来源:互联网 发布:重复文件 知乎 编辑:程序博客网 时间:2024/04/28 16:30
1.核心代码,找到电话号码的位置
#pragma mark-<获取电话号码的坐标>+ (CGRect)boundingRectForCharacterRange:(NSRange)range andLable:(UILabel *)lable lableSize:(CGSize)lableSize{// NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:contentStr];// NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];// paraStyle.lineSpacing = 6;// NSDictionary *attrs =@{NSFontAttributeName : [UIFont systemFontOfSize:12.0], NSParagraphStyleAttributeName : paraStyle};// [attributeString setAttributes:attrs range:NSMakeRange(0, contentStr.length)]; NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:lable.attributedText]; NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; [textStorage addLayoutManager:layoutManager]; NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:lableSize]; textContainer.lineFragmentPadding = 0; [layoutManager addTextContainer:textContainer]; NSRange glyphRange; [layoutManager characterRangeForGlyphRange:range actualGlyphRange:&glyphRange]; CGRect rect = [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];// CGFloat yOfset = rect.origin.y;// rect.origin.y = yOfset + 3; return rect;}2.用法示例
{ // 添加拨打电话
self.tipsLabel.userInteractionEnabled =YES;
[self.viewlayoutIfNeeded];
NSRange range = [_tipsLabel.textrangeOfString:@"4006668800"];
UIControl *phoneControl = [[UIControlalloc] initWithFrame:[ZYToolsboundingRectForCharacterRange:range andLable:self.tipsLabellableSize:self.tipsLabel.frame.size]];
phoneControl.tag = 1234;
[phoneControl addTarget:selfaction:@selector(phoneLink)forControlEvents:UIControlEventTouchUpInside];
[_tipsLabel addSubview:phoneControl];
}
#pragma mark-点击拨打电话
- (void)phoneLink{
NSString *str = [NSStringstringWithFormat:@"tel:%@",CustomerServiceTelephone];
dispatch_async(dispatch_get_main_queue(), ^(){
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:str]];
});
}
3.注意label的attributedText一定要设置字体大小
- (UILabel *)tipsLabel
{
if (!_tipsLabel) {
_tipsLabel = [[UILabelalloc] init];
_tipsLabel.text =@"1、请输入正确的运营商服务密码,如果忘记请点击\"忘记密码\"\n2、运营商认证需要2-3分钟,请耐心等待\n3、如果重置密码失败,请拨打客服热线:4006668800";
_tipsLabel.font =UIFONT_12;
_tipsLabel.numberOfLines =0;
_tipsLabel.textColor =colorWith(@"#939ca5");
_tipsLabel.textAlignment =NSTextAlignmentLeft;
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStylealloc] init];
paraStyle.lineSpacing = 6;
NSDictionary *dict =@{NSParagraphStyleAttributeName : paraStyle};
NSMutableAttributedString *attr = [[NSMutableAttributedStringalloc] initWithString:_tipsLabel.textattributes:dict];
NSRange range = [_tipsLabel.textrangeOfString:@"4006668800"];
[attr addAttribute:NSForegroundColorAttributeNamevalue:[UIColorcolorWithHexString:@"#FB6F5C"]range:range];
[attr addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:12.0]range:NSMakeRange(0,_tipsLabel.text.length)];
_tipsLabel.attributedText = attr;
}
return_tipsLabel;
}
- ios lable中电话号码点击拨打电话
- IOS中拨打电话
- 手机访问页面电话号码,点击弹出拨打电话
- iOS-程序中拨打电话
- iOS开发点击数字拨打电话
- ios代码拨打电话时,电话号码格式兼容性处理问题
- ios代码拨打电话时,电话号码格式兼容性处理问题:
- 关于在textvie中提取电话号码,点击跳转拨打界面
- iOS中实现拨打电话功能
- IOS中实现Lable点击事件
- IOS加载网页后点击页面号码拨打电话实例
- iOS 点击按钮实现拨打电话,发送短信功能
- iOS 点击号码拨打电话三种方式
- android 电话号码的获取及拨打电话
- 实现Label文本内容中电话号码特殊颜色显示并点击电话号码能拨打
- 实现Label文本内容中电话号码特殊颜色显示并点击电话号码能拨打
- ios 拨打电话
- IOS开发 拨打电话
- CSS Sticky footer布局
- JS編程規範 積累
- python实现LDAP中组(group)添加用户(user)功能
- [从C到C++] 1.3 C++布尔类型(bool)
- python+scrapy+selenium结合
- ios lable中电话号码点击拨打电话
- 六数码
- Android自动生成表格,丰富配置
- hash算法原理详解
- Oracle 执行计划(Explain Plan) 说明
- 怎么利用产品的基线和功能架构来做冒烟测试(快速测试)
- GNU C中__attribute__
- NOIP2017普及总结(滚粗记)
- 关于Windows创建进程的过程