網頁

2012年7月8日 星期日

iOS開發筆記 ﹣UIButton上title蓋在image上

這陣子在撰寫個程式,希望按鈕上有字也有底圖,本來以為很簡單,但卻發現,要嘛就有字,要嘛就只有圖,無法使用setImage,改用setBackgroundImage,但我要的字在底端不是中間,而且會被拉長放大等問題,查到最後只好使用UIEdgeInsetsMake,大致的寫法如下

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self action:@selector(xxxx:) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(20, 20, 96, 96); //圖的大小在90x90左右
[btn setTitle:@"圖示" forState:UIControlStateNormal];
UIImage *btnImg = [UIImage imageNamed:@"icon.png"];

[btn setImage:btnImg forState:UIControlStateNormal];

//title從中間往下65px,靠左為btnImg的寬度,負數為放大
[btn setTitleEdgeInsets:UIEdgeInsetsMake(65, -btnImg.size.width, 0, 0)];
//image從中間往上20px,靠右為titleLabel的寬度
[btn setImageEdgeInsets:UIEdgeInsetsMake(-20, 0, 0, -btn.titleLabel.bounds.size.width)];


要特別注意的是,設定title及image的順序,若反過來,記得在寬高的設定上也要反過來,兩者是互相影响的。


參考網址:
http://blog.sina.com.cn/s/blog_6de1899201013bnv.html

2012年7月6日 星期五

iOS開發筆記 ﹣Html Hex Code轉換成RGB

最近有個案子要將html的hex code餵進手機中,然後手機原生的layout button要能直接更動顏色,但手機原生用的是RGB,html是這樣的code: #ccff00,只好自己動手寫出個轉換的function囉

整個function內容如下,先使用NSScanner來轉換成int,不過這邊倒是遇到個問題,提供的function接受的string是要0x開頭,所以得自行拿掉"#"號,並加上"0x",這樣就可以使用了。

/*
 * 轉換html色碼成rgb
 */
+(UIColor*) transferHexColorCode:(NSString*)colorCode
{
//    NSLog(@"color: %@",colorCode);
    /*
     *  無法直接使用NSScanner *scanner2 = [NSScanner scannerWithString:colorCode];
     *  要先轉換成0xFF0000這樣的型式
     */
    NSString *color = [NSString stringWithFormat:@"0x%@",[colorCode substringFromIndex:1]];
    NSScanner *scanner2 = [NSScanner scannerWithString:color];
    [scanner2 setCharactersToBeSkipped:[NSCharacterSet symbolCharacterSet]];
    unsigned int baseColor;
    [scanner2 scanHexInt:&baseColor];
//    NSLog(@"scanner2 %@",scanner2);
//    NSLog(@"int: %i",baseColor);
    CGFloat red   = ((baseColor & 0xFF0000) >> 16) / 255.0f;
    CGFloat green = ((baseColor & 0x00FF00) >>  8) / 255.0f;
    CGFloat blue  =  (baseColor & 0x0000FF) / 255.0f;
    
//    NSLog(@"red: %f, green: %f, blue: %f",red,green,blue);
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}

參考網址:
http://stackoverflow.com/questions/3010216/how-can-i-convert-rgb-hex-string-into-uicolor-in-objective-c
http://stackoverflow.com/questions/7360958/objective-c-html-rgb-color-not-correct-in-uicolor

2012年7月2日 星期一

CentOS筆記-修正系統時間及自動修正

修正時間,其實用date這個指令即可,

格式:date MMDDhhmmYYYY
例如:date 070505112012 -> 2012年7月15日5點11分

但這次我想要的是系統自動update及修正,所以需要ntp server,在cent os上很容易做
1. yum install htp2. chkconfig ntpd on
3. ntpdate pool.ntp.org <-- 手動updater
4. /etc/init.d/ntpd start <-- 將server啟動


參考網址:
http://www.cyberciti.biz/faq/howto-install-ntp-to-synchronize-server-clock/

2012年6月20日 星期三

iOS開發筆記 ﹣NSIndexPath indexPathForRow: inSection: 的注意事項

使用[NSIndexPath indexPathForRow:(NSInteger) inSection:(NSInteger)]這個function,若使用在整個object的範圍下,要注意是會隨時被autorelease,不在自己的控制下,所以若要在整個object的使用範圍,最好assign給此object的member時寫成以下方式,如:

_curIndex = [[NSIndexPath indexPathForRow:_curIndex.row+1 inSection:0] copy]

2012年6月13日 星期三

CentOS筆記-安裝svn


yum install mod_dav_svn subversion

利用上述的指令已安裝好subversion了。


參考網頁
http://wiki.centos.org/HowTos/Subversion