工作随笔——UIButton的EdgeInsets + Swift中的正则表达式;
1、UIButton的EdgeInsets
UIButton的EdgeInsets方法,是用来设置title和image对于上左下右四个方向的偏移,但是很奇怪的是,刚开始只有Image,titile也设置了结果没显示出来,问题出现的很奇怪,也没找到什么原因,代码也没有错,莫名其妙的,换了一个图试了试,果然就好了;最后网上也找了原因,没有说明的,只是说这种现象的出现是因为"Button的width > titleLabel的width + image的width(图片原大小)" ,附上链接:http://blog.csdn.net/dfqin/article/details/37813591
也就是:
1.当button.width < image.width时,只显示被压缩后的图片,图片是按fillXY的方式压缩。
2.当button.width > image.width,且 button.width < (image.width + text.width)时,图片正常显示,文本被压缩。
3.当button.width > (image.width + text.width),两者并列默认居中显示,可通过button的属性contentHorizontalAlignment改变对齐方式。
2、Swift中的正则表达式;
正则表达式主要使用的类还是NSRegularExpression,感觉对于正则的匹配上,重要的还是正则本身。
let patterm = "^[1-9]\\d*|0$" do{ let reqularEx = try NSRegularExpression(pattern: patterm, options: NSRegularExpressionOptions.CaseInsensitive) let match = reqularEx.matchesInString(string, options: [], range: NSMakeRange(0, 1)) if match.count > 0 { let newText = textField.text! + string let newValue = Double(newText)! * (profileRatio+1) profileLabel.text = NSString(format: "%.2f", newValue) as String return true }else{ return false } }catch{ return false }