为了账号安全,请及时绑定邮箱和手机立即绑定

swift——一些有用的小Tips

标签:
Swift

UITableView

  • 有时候UI需要tableView距离上方的元素间隙为0,加上这段代码就行
    self.tableView.tableHeaderView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))

webp

  • UI会设计出各种颜色的箭头,如下图。


    webp

    向右的箭头为蓝色


    一般情况都会选择自定义Cell,但是Cell特别简单完全不想自定义,就可以自定义accessoryView,上代码。

cell.accessoryType = .none
cell.accessoryView = getBlueRightArrow()

func getBlueRightArrow() -> UIView{
        let vi = UIImageView.init(frame: CGRect.init(x: 0, y: 14, width: 16, height: 16))
        vi.image = UIImage.init(named: "mine_RightArrow")        return vi
}

上图就是用这种方法实现的效果。

webp

  • 有时候一些特殊的cell可以不必自己去画(自定义)


    webp

    UI要的效果


    这是UI给的图,根据数据来决定是空心圆圈还是实心圆圈。
    但是我不想自定义,麻烦!然后我就尝试了一件事,试着能否打出圆圈,发现是可以的。
    "  " ,"  " 圆圈在此,那就方便了,富文本走一个。

/// 左侧圆圈富文本func leftCircle(_ label: UILabel, attrTitle:String){
        label.textAlignment = .left
        label.textColor = UIColor.textBlack
        label.font = UIFont.systemFont(ofSize: 15)
        let attrStr = NSMutableAttributedString.init(string: attrTitle )        /// 圆圈长度为1
        attrStr.addAttribute(NSForegroundColorAttributeName, value:UIColor.mainBlue, range:NSRange.init(location:0, length: 1))
        label.attributedText = attrStr
    }/// 调用leftCircle(label, attrTitle: "  你的标题")

webp

  • 有时候一些特殊的cell的配图不能用文字打出来,但还是不想去画图,用cell自带的image又达不到UI要求的效果(间距问题),怎么办?还是富文本!


    webp

    效果图

func imageTitleLabel(_ label: UILabel, _ imageStr: String) {
        
        label.textAlignment = .left
        
        let attributes = [            NSFontAttributeName: UIFont.systemFont(ofSize: 15.0),            NSForegroundColorAttributeName: UIColor.textBlack,            /// 向上偏移量
            NSBaselineOffsetAttributeName: 5
            ] as [String : Any]
        
        let attachment = NSTextAttachment()
        attachment.image = UIImage.init(named: imageStr)
        attachment.bounds = CGRect(
            x: 0, y: 0,
            width: attachment.image!.size.width,
            height: attachment.image!.size.height
        )
        let attributedStringWithImage = NSAttributedString(attachment: attachment)
        
        let string = NSMutableAttributedString()
        string.append(attributedStringWithImage)
        string.append(NSAttributedString(string: " \(label.text!)", attributes: attributes))
        
        label.attributedText = string
    }

上面的图就是用富文本做的!

webp

  • 将section的footer、header 之间的间隙设置为透明色,保证与背景色一致

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        
        view.tintColor = UIColor.clear
        
    }
    
    func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
        
        view.tintColor = UIColor.clear
        
    }

webp

UITextField

  • 修改placeholder的字体颜色和大小
    field.attributedPlaceholder = NSAttributedString.init(string:"蓝色且字体大小为15的请输入", attributes: [NSForegroundColorAttributeName:UIColor.blue, NSFontAttributeName:UIFont.systemFont(ofSize:15)])

关于Label的富文本可以去看看这个swift——富文本文字的简单使用



作者:Bart_Simpson
链接:https://www.jianshu.com/p/5feac2869a8d



点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消