AXAttributedLabel 0.2.10

AXAttributedLabel 0.2.10

测试测试
语言语言 _objcObjective C
许可证 MIT
发布最后发布2016年11月

devedbox 维护。



  • 作者:
  • 艾星

摘要

AXAttributedLabel 是一个基于 TextKit、使用 UITextView 作为结构的轻量级属性文本工具。使用 AXAttributedLabel,您可以在交互视图中以属性文本的形式显示文本内容,包括电话/地址/日期。您可以向 标签 中添加自定义链接,并捕获链接的触摸动作。从 iOS9.0(iPhone 6s)和更高版本的平台开始,您可以通过 PeekPop 与链接进行交互,以预览链接内容。

sample2

特性

支持数据检测。

使用 PeekPop 预览链接。

将任意图像插入到文本内容中。

长按以显示菜单并自定义编辑菜单。

添加可自定义的链接。

需求

AXAttributedLabel 用于 iOS 7.0 及更高版本的系统。它需要:

  • Foundation.framework
  • UIKit.framework

当您将标签用于项目时,最好使用 xcode 的最新版本。

将 AXAttributedLabel 添加到您的项目

源文件

或者,您可以直接将 AXAttributedLabel.hAXAttributedLabel.m 源文件添加到您的项目中。

  1. 下载最新的代码版本 [master.zip](https://github.com/devedbox/AXAttributedLabel/archive/master.zip),或将仓库添加为 git 子模块到您的 git 追踪项目中。
  2. 在 Xcode 中打开您的项目,然后拖拽 AXAttributedLabel.hAXAttributedLabel.m 到您的项目(使用“产品导航窗口”)。如果您将代码存档从项目中提取,请确保选择复制项目时选择“复制项目”。
  3. 在需要的地方使用 #import "AXAttributedLabel.h" 包含 AXAttributedLabel。

许可证

本代码依据 MIT 许可证的条款和条件分发。

用法

AXAttributedLabelUILabelUITextView一样简单易用。在使用前,请确保正确初始化,例如:

_label = [AXAttributedLabel attributedLabel];
_label.translatesAutoresizingMaskIntoConstraints = NO;
_label.attribute = self;
_label.text = @"Multiple availability att[sss]ributes can be placed on a declaration, which may correspond to different platforms. Only the availability attr[sss]ibute with the platform[sss] correcorre 明天 sponding to the target pla[sss]tform will be used. https://www.baidu.com the availability 15680002585 any others wil[sss]l be ignored. If no 成都市成华区二仙桥东三路1号 availability attribute specifies availability for the cu[sss]rrent target platform, the a[sss]vailability attributes are ignored.";

并确保_label.attributedEnabled = NO;已经被设置。attributedEnabled用于控制数据的检测。默认值为YES,用于检测数据;而当它是NO时,则不检测数据。

设定数据检测器

如下所示:

_label.detectorTypes |= AXAttributedLabelDetectorTypeImage|AXAttributedLabelDetectorTypeLink|AXAttributedLabelDetectorTypeTransitInformation|AXAttributedLabelDetectorTypeAddress;

与链接交互

如果您想与链接交互,请在AXAttributedLabel的初始化器之后添加以下代码:

_label.shouldInteractWithURLs = YES;
_label.shouldInteractWithExclusionViews = YES;

使用3D Touch预览链接

确保将交互启用并添加以下代码:

_label.allowsPreviewURLs = YES;

这将生效。

将图片添加到内容中

示例

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*.5-45, 45, 90, 90)];
imageView.image = [UIImage imageNamed:@"avatar.jpg"];
_label.exclusionViews = @[imageView];

添加自定义编辑菜单选项

示例

_label.showsMenuItems = YES;
AXMenuItem *item = [AXMenuItem itemWithTitle:@"aaa" handler:^(AXAttributedLabel * _Nonnull label, AXMenuItem * _Nonnull item) {
        NSLog(@"aaa");
    }];
AXMenuItem *item1 = [AXMenuItem itemWithTitle:@"bbb" handler:^(AXAttributedLabel * _Nonnull label, AXMenuItem * _Nonnull item) {
        NSLog(@"bbb");
    }];
AXMenuItem *item2 = [AXMenuItem itemWithTitle:@"ccc" handler:^(AXAttributedLabel * _Nonnull label, AXMenuItem * _Nonnull item) {
        NSLog(@"ccc");
    }];
AXMenuItem *item3 = [AXMenuItem itemWithTitle:@"ddd" handler:^(AXAttributedLabel * _Nonnull label, AXMenuItem * _Nonnull item) {
        NSLog(@"ddd");
    }];
AXMenuItem *item4 = [AXMenuItem itemWithTitle:@"eee" handler:^(AXAttributedLabel * _Nonnull label, AXMenuItem * _Nonnull item) {
        NSLog(@"eee");
    }];
AXMenuItem *item5 = [AXMenuItem itemWithTitle:@"fff" handler:^(AXAttributedLabel * _Nonnull label, AXMenuItem * _Nonnull item) {
        NSLog(@"fff");
    }];
AXMenuItem *item6 = [AXMenuItem itemWithTitle:@"hhh" handler:^(AXAttributedLabel * _Nonnull label, AXMenuItem * _Nonnull item) {
        NSLog(@"hhh");
    }];
[_label setMenuItems:@[item, item1, item2, item3, item4, item5, item6]];

只需确保先将showsMenuItems设置为YES

使用代理

示例

#pragma mark - AXAttributedLabelDelegate
- (UIImage *)imageAttachmentForAttributedLabel:(AXAttributedLabel *)attl result:(NSTextCheckingResult *)result {
    return [UIImage imageNamed:@"avatar.jpg"];
}
- (void)attributedLabel:(AXAttributedLabel *)attributedLabel didSelectURL:(NSURL *)url {
    [AXPickerView showInView:self.view.window animated:YES style:0 items:@[url.absoluteString] title:@"打开链接?" tips:[NSString stringWithFormat:@"点击打开链接:%@", url.absoluteString] configuration:NULL completion:^(AXPickerView *pickerView) {
        pickerView.tipsLabel.textAlignment = NSTextAlignmentCenter;
    } revoking:NULL executing:^(NSString *selectedTitle, NSInteger index, AXPickerView *inPickerView) {
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
    }];
}
- (void)attributedLabel:(AXAttributedLabel *)attributedLabel didSelectAddress:(NSDictionary *)addressComponents {
    [AXPickerView showInView:self.view.window animated:YES style:0 items:[addressComponents allValues] title:@"地址" tips:@"显示所有地址" configuration:NULL completion:^(AXPickerView *pickerView) {
        pickerView.tipsLabel.textAlignment = NSTextAlignmentCenter;
    } revoking:NULL executing:NULL];
}
- (void)attributedLabel:(AXAttributedLabel *)attributedLabel didSelectPhoneNumber:(NSString *)phoneNumber {
    [AXPickerView showInView:self.view.window animated:YES style:0 items:@[phoneNumber] title:@"拨打电话?" tips:[NSString stringWithFormat:@"点击拨打电话:%@", phoneNumber] configuration:NULL completion:^(AXPickerView *pickerView) {
        pickerView.tipsLabel.textAlignment = NSTextAlignmentCenter;
    } revoking:NULL executing:^(NSString *selectedTitle, NSInteger index, AXPickerView *inPickerView) {
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumber]]]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumber]]];
        }
    }];
}
- (void)attributedLabel:(AXAttributedLabel *)attributedLabel didSelectDate:(NSDate *)date {
    NSLog(@"date:%@",date);
}
- (void)attributedLabel:(AXAttributedLabel *)attributedLabel didSelectDate:(NSDate *)date timeZone:(NSTimeZone *)timeZone duration:(NSTimeInterval)duration {
    NSLog(@"date:%@",date);
}
- (void)attributedLabel:(AXAttributedLabel *)attributedLabel didSelectTransitInformation:(NSDictionary *)components {

}
- (void)attributedLabel:(AXAttributedLabel *)attributedLabel didSelectAttachment:(NSTextAttachment *)attachment {
    NSLog(@"attachment:%@",attachment);
}
- (void)attributedLabel:(AXAttributedLabel *)attributedLabel didSelectTextCheckingResult:(NSTextCheckingResult *)result {

}
- (void)attributedLabel:(AXAttributedLabel *)attributedLabel didSelectExclusionViewAtIndex:(NSUInteger)index {
    NSLog(@"selected exclusion view at index: %@", @(index));
}

联系我

如果您发现任何错误或需要新的功能,请告知我。

微信&电话:15680002585

微博:@devedbox

GitHub:https://github.com/devedbox

LinkedIn:艾星

邮箱:[email protected]