iOS 编程:用 UIButton 实现 YYLabel 的 TextHighlight 效果

方案一:使用 YYText 框架

用如下方式实现文本链接效果:

- (void)viewDidLoad {
    [super viewDidLoad];

    YYLabel *label = [[YYLabel alloc] init];
    // 设置文本
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"查看链接地址"];
    attrStr.font = [UIFont systemFontOfSize:16];
    attrStr.underlineStyle = NSUnderlineStyleSingle;
    attrStr.underlineColor = HexColor(@"007AFF");
    [attrStr setTextHighlightRange:attrStr.rangeOfAll
                             color:HexColor(@"007AFF")
                   backgroundColor:HexColor(@"CECED2")
                         tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                             // Tap Action...
                         }];
    label.attributedText = attrStr;
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    // 使用 Masonry 框架进行自动布局
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view);
    }];
}

需要的效果是这样的,有下划线、点击后会有灰色背景:

实际上效果是,没有下划线、点击后也没有灰色背景:

失效原因:

一开始我以为是 Masonry 自动布局的原因:

如果用 [[YYLabel alloc] initWithFrame:CGRectMake(100, 300, 300, 21)]; 写成固定约束就没有问题。

后来发现原因只是这行代码:

label.textAlignment = NSTextAlignmentCenter; // 设置了居中显示

只要删掉这行代码就可以了。

方案二:UIButton 原生实现

当然,你也可以使用原生的 UIButton 类也可以实现以上效果。

@property (nonatomic, strong) UIButton *serviceAgreementButton;

- (UIButton *)serviceAgreementButton {
    if (!_serviceAgreementButton) {
        _serviceAgreementButton = [UIButton buttonWithType:UIButtonTypeCustom];
        // 默认标题
        NSDictionary *attributes = @{
                                     NSFontAttributeName:[UIFont systemFontOfSize:16],
                          NSForegroundColorAttributeName:HexColor(@"#007AFF"),
                           NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)
                                     };
        NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@"APP许可及服务协议" attributes:attributes];
        [_serviceAgreementButton setAttributedTitle:attrStr forState:UIControlStateNormal];
        // 高亮背景
        [_serviceAgreementButton setBackgroundImage:[UIImage imageWithColor:COLOR_BACKGROUND]
                                           forState:UIControlStateNormal];
        [_serviceAgreementButton setBackgroundImage:[UIImage imageWithColor:HexColor(@"#CECED2")]
                                           forState:UIControlStateHighlighted];
        [_serviceAgreementButton addTarget:self
                                    action:@selector(buttonTapAction:)
                          forControlEvents:UIControlEventTouchUpInside];
    }
    return _serviceAgreementButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self.view addSubview:self.serviceAgreementButton];
    [self.serviceAgreementButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view);
}];
}

🎉🎉🎉 I GET IT !

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,230评论 4 61
  • #初来乍到 7/30日,坐了8个小时的动车到达广州,和另外三个小伙伴集合,去往遥远的埃及。对于那时的我们来...
    嘟纯情阅读 2,833评论 0 0
  • 你好积极阳光,我可以认识你吗? 跟着老廖,有肉吃。 你有很多正能量,我想认识你。 几乎时不时就有这样的留言出现,大...
    9444e0643fbd阅读 41评论 0 0
  • 2017年12月9日 星期六 晴 今年暑假,你到厦门帮朋友的新居安装水电。那天晚上,你给我微信视频,彼时我已穿着睡...
    老草阅读 5,755评论 56 54
  • 爱是努力的动力,努力成就爱的方向!
    京宁阅读 968评论 0 0