2015年3月31日 星期二

設定textView的邊界留白

//在scrollView是contentInset,但textView被改到textContainerInset屬性
self.textView.textContainerInset = UIEdgeInsetsMake(25, 25, 25, 25)

2015年3月26日 星期四

Literal Expression

println(__FILE__) //顯示當下檔案的路徑
println(__LINE__) //顯示當下在這支檔案的那一行
println(__COLUMN__) //顯示當下在這支檔案的那一列
println(__FUNCTION__) //顯示當下func的名稱

全域函式 vs 巢狀函式 vs 閉包

全域函式
在swift中你可以直接開一個檔案來宣告func,這些func可提供全域使用

巢狀函式
一般宣告在class的 func都是,這種可以取用他domain range中的其他變數來用

閉包
這裡不需要宣告func name,也只可以取用宣告給它的參數來用

2015年3月19日 星期四

想在非主線程加入NSTimer,要放進Runloop中跑才能執行function

//建立Timer
let timer = NSTimer(timeInterval: self.responseTimeoutInterval, target: self, selector: Selector("responseTimeout:"), userInfo: Int(packet.header.transactionId), repeats: false)
            NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

//接收事件的function
dynamic func responseTimeout(timer: NSTimer) {
        logDebug(TAG, "responseTimeout")
        if let transactionId = timer.userInfo as? Int {
            self.resetResponseTimer(transactionId)
        }
        self.delegate?.onError(NSError(domain: fusionErrorDomain, code: Int(FusionErrorCode.ResponseTimeout.rawValue), userInfo: nil))
        self.socket?.disconnect()
    }

2015年3月18日 星期三

統一收notification再以delegate分派出去的設計方式

//在UIEvents統一接收notification
@objc protocol UIEvents {
    func onError(notification: NSNotification)
}

class UIEventHelper {

    class func registerFusionEvents(eventReceiver: AnyObject) {
        NSNotificationCenter.defaultCenter().addObserver(
            eventReceiver, selector: "onFusionError:",
            name: FusionService.fusionErrorNotification, object: nil)

    }
}

//在你要使用的class,加入註冊跟實作delegate
//viewWillAppear
self.registerNotifications()
//viewWillDisappear
self. unregisterNotifications()

    // MARK: - Notification
func registerNotifications() {
    UIEventHelper.registerFusionEvents(self)
}
    
func unregisterNotifications() {
    UIEventHelper.unregisterUIEvents(self)
}

func onError(notification: NSNotification) {
//TODO:實作你的code
}


2015年3月17日 星期二

學習資源

讀得多不如讀得好,iOS 開發者的 15 個閱讀清單http://punnode.com/archives/28835

十個必備的swift open source
http://m.csdn.net/article/2014-10-14/2822083-swift-ios-open-source-projects

swift學習資料大全
https://github.com/ipader/SwiftGuide/blob/master/README.md

2015年3月16日 星期一

在attributedString中加入subAttributed

//http://stackoverflow.com/questions/19551856/how-would-i-create-a-uibutton-with-two-lines-of-text-and-the-first-line-of-text

const CGFloat fontSize = 13;
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
//UIColor *foregroundColor = [UIColor whiteColor];

// Create the attributes
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                               regularFont, NSFontAttributeName, nil];
NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                                  boldFont, NSFontAttributeName, nil];
const NSRange range = NSMakeRange(iFrom,iTo - iFrom);

// Create the attributed string (text + attributes)
NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:aText
                                               attributes:attrs];
[attributedText setAttributes:subAttrs range:range];

// Set it in our UILabel and we are done!
[aLabel setAttributedText:attributedText];