Swift之根据文字长度循环创建button

我们经常会循环创建button,很多时候我们需要根据文字的长度来创建不同宽度的button。下面我们开始创建:

1.创建button:
func createButton() {
        //dataSource是我的数据源
        for i in 0..<dataSource!.count {
            let button = UIButton(type: .Custom)
            button.backgroundColor = YYMain_Color
            button.layer.cornerRadius = 8.0
            button.layer.masksToBounds = true

            view.addSubview(button)
        }
}
2.获取文字的长度:
  //创建属性
  let attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(14)]
  //将String转换成NSString
  let keyword: NSString = NSString(CString: dataSource![i].keyword!.cStringUsingEncoding(NSUTF8StringEncoding)!, encoding: NSUTF8StringEncoding)!
  //获取文字的长度
  let length = keyword.boundingRectWithSize(CGSizeMake(SCREEN_WIDTH, 2000), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes, context: nil).size.width
3.把attribute给button:
  //为button赋值
  let attributeString = NSAttributedString(string: dataSource![i].keyword!, attributes: attributes)
  button.setAttributedTitle(attributeString, forState: .Normal)
4.设置button的大小:
  //先定义两个变量
  var w: CGFloat = 0
  var h: CGFloat = 90
  //设置button的frame
  button.frame = CGRectMake(10 + w, h, length + 15, 30)
  //当button的位置超出右侧边缘时换行
  if 10+w+length+15 > SCREEN_WIDTH {
          w = 0
          h = h + button.frame.size.height + 10
          button.frame = CGRectMake(10+w, h, length+15, 30)
    }
    w = button.frame.size.width + button.frame.origin.x
OK👌效果如下:
循环创建button.png
最终代码:
var w: CGFloat = 0
var h: CGFloat = 90
 func createButton() {

        for i in 0..<dataSource!.count {
            let button = UIButton(type: .Custom)
            button.backgroundColor = YYMain_Color
            button.layer.cornerRadius = 8.0
            button.layer.masksToBounds = true
            
            let attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(14)]
            let keyword: NSString = NSString(CString: dataSource![i].keyword!.cStringUsingEncoding(NSUTF8StringEncoding)!, encoding: NSUTF8StringEncoding)!
            let length = keyword.boundingRectWithSize(CGSizeMake(SCREEN_WIDTH, 2000), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes, context: nil).size.width
            //为button赋值
            let attributeString = NSAttributedString(string: dataSource![i].keyword!, attributes: attributes)
            button.setAttributedTitle(attributeString, forState: .Normal)
            //设置button的frame
            button.frame = CGRectMake(10 + w, h, length + 15, 30)
            //当button的位置超出右侧边缘时换行
            if 10+w+length+15 > SCREEN_WIDTH {
                w = 0
                h = h + button.frame.size.height + 10
                button.frame = CGRectMake(10+w, h, length+15, 30)
            }
            w = button.frame.size.width + button.frame.origin.x
            view.addSubview(button)
        }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,711评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,046评论 25 709
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,680评论 1 92
  • 晚上老大想把自己的浴巾想铺在秋千上面,怎么也铺不好,就说要把浴巾扔了,然后奔向垃圾桶,爸爸说,你扔了以后就没有了!...
    刘畅_apple阅读 3,547评论 0 0
  • 感情像在写一本小说 情节跌宕起伏却紧凑 角色的设定明明是主人公 开场两分钟就被ko 并没有过分的要求 能不能再给几...
    徐秀美阅读 1,072评论 0 0