美文网首页
iOS 常用小技巧(二)

iOS 常用小技巧(二)

作者: 程序员学哥 | 来源:发表于2019-10-15 07:25 被阅读0次
1:iOS 导航栏rightBarButtonItems多个按钮之间的间距
    UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [editBtn addTarget:self action:@selector(editBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [editBtn setImage:[UIImage imageNamed:@"icon_zl_bj"] forState:UIControlStateNormal];
    [editBtn sizeToFit];
    self.editBtnItem = [[UIBarButtonItem alloc] initWithCustomView:editBtn];
   
    //创建一个空白占位bar(利用它来控制间距)
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -10;
   
    UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreBtn addTarget:self action:@selector(moreBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [moreBtn setImage:[UIImage imageNamed:@"icon_dian_more"] forState:UIControlStateNormal];
    [moreBtn sizeToFit];
    self.moreBtnItem = [[UIBarButtonItem alloc] initWithCustomView:moreBtn];
   
    self.navigationItem.rightBarButtonItems = @[self.moreBtnItem,spaceItem,self.editBtnItem];
2:YYLabel添加文字高亮点击

下面是文字初始化的例子,其余情况类似。直接复制即可使用

- (YYLabel *)userAgreementcLbl{
    
    if (_userAgreementcLbl == nil) {
        _userAgreementcLbl = [[YYLabel alloc] init];
        _userAgreementcLbl.userInteractionEnabled = YES;
        NSString *userAgreement = @"火萤动态壁纸《用户服务协议》《隐私声明》";
        UIFont *font = HYFontSize12;
   
        NSMutableAttributedString *muAtt1 = [[NSMutableAttributedString alloc] initWithString:userAgreement];
       /// 字体大小
        muAtt1.yy_font = font;
        /// 字体颜色
        muAtt1.yy_color = rgba(255,255,255,1);
        /// 行高
        NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
        /// 设置行高
        [muAtt1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, muAtt1.length)];
        /// 设置不是高亮部分的字体
        [muAtt1 addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, muAtt1.length)];
        
        KWeakSelf
        /// 添加"《用户服务协议》"的点击
        [muAtt1 yy_setTextHighlightRange:NSMakeRange(6, 8) color:rgba(255,62,41,1)
                         backgroundColor:[UIColor clearColor]
                               tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                                   KStrongSelf
                                    /// 跳转用户服务协议界面
                               }];

        /// 添加"《隐私声明》"的点击
        [muAtt1 yy_setTextHighlightRange:NSMakeRange(userAgreement.length - 6, 6) color:rgba(255,62,41,1)
                         backgroundColor:[UIColor clearColor]
                               tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                                   KStrongSelf
                                   /// 跳转隐私声明界面
                               }];
        
        /// 一定要在最后设置attributedText,否则没有效果
        _userAgreementcLbl.attributedText = muAtt1;
    }
    return _userAgreementcLbl;
}
3:iOS 边框变成虚线

类似这种效果


边框虚线.png

关键代码直接添加使用就可以

    CAShapeLayer * roundedrect  = [CAShapeLayer layer];
    roundedrect.frame           = CGRectMake(0, 0, frame.size.width, frame.size.height);
    roundedrect.fillColor       = [UIColor whiteColor].CGColor;
    roundedrect.strokeColor     = [UIColor colorWithRed:0.329 green: 0.329 blue:0.329 alpha:1].CGColor;
    roundedrect.lineDashPattern = @[@5.5, @4.5];
    roundedrect.path            = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, frame.size.width , frame.size.height) cornerRadius:20].CGPath;
    [self.layer addSublayer:roundedrect];

相关文章

网友评论

      本文标题:iOS 常用小技巧(二)

      本文链接:https://www.haomeiwen.com/subject/zzljmctx.html