2015年4月24日 星期五

在textField輸入時,即時抓取text

//要用stringByReplacingCharactersInRange把textField delegate的回傳值給拚起來

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        var username = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
        if self.checkUsernameRule(username) {
            self.submitButton.enabled = true
        } else {
            self.submitButton.enabled = false
        }
        
        // return NO to not change text
        return true

    }

func checkUsernameRule(username: String) -> Bool {
        //Username should be 6–20 characters long, and start with a letter
        println(username)
        if username =~ "^[a-zA-Z]{1}(\\w{5,20})$" {
            return true
        }
        return false
    }

2015年4月23日 星期四

swift inout 參數

swift會把參數都用let來設,如果要改變參數原來的值的話,就要把參數設inout。

2015年4月15日 星期三

build app指令

xcodebuild clean archive -scheme migme-debug -workspace iOSClient.xcworkspace -archivePath archive/migme.xcarchive -destination generic/platform=iOS
mkdir -p ipa

xcodebuild -exportArchive -archivePath archive/migme.xcarchive -exportPath ipa/migme.ipa -exportProvisioningProfile "migme adhoc distribution"

2015年4月14日 星期二

計算NSAttributedString的size

如果用先new好的textview來放,就會一直失敗
internal func calculateTextSize(attributedText: NSAttributedString, maxWidth: CGFloat) -> CGSize {
        var measureTextView = UITextView()
        measureTextView.attributedText = attributedText
        let size = measureTextView.sizeThatFits(CGSizeMake(maxWidth, CGFloat(FLT_MAX)))
        return size

    }

2015年4月11日 星期六

inline function 內嵌函數

在你宣告的function之前加上inline關鍵字
代表的是你想要在編譯時,直接把這個函式的內容寫進呼叫它的函式中。
原因是
在機器碼中,每個函式會寫到不同的記憶體位置,程式在執行呼叫時,會先停下來目前的狀態,跳到被呼叫的函式的記憶體區塊,執行完之後再跳回來。
在這段跳轉的過程中是會消耗系統資源的......
所以如果你的函式很短,或沒有要做什麼複雜計算,可以在編譯時,把被呼叫函式中的程式碼直接寫到目前的函式之中,來省下這段記憶體跳轉所造成的消耗。

note. inline只能用來建議編譯器你"想"這麼做,實際結果還是要看編譯器考慮到有沒有什麼限制而定。

2015年4月4日 星期六

判斷iOS現在是否為24時制

class func is24HourTimeFormat() -> Bool {
        var dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = NSDateFormatterStyle.NoStyle
        dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
        dateFormatter.locale = (NSLocale.currentLocale())
        var dateString = dateFormatter.stringFromDate(NSDate())
        let amRange = dateString.rangeOfString(dateFormatter.AMSymbol)
        let pmRange = dateString.rangeOfString(dateFormatter.PMSymbol)
        return (amRange == nil && pmRange == nil)

    }