Mar 24, 2009

(XCode) How to setup a bracket / brace macro in XCode

Before i used XCode and learned Objective-C, i never thought the bracket is a horrible symbol, there is a solution of creating text macro to insert bracket / brace:

1. Open ObjectiveC.xctxtmacro with an text editor, you can find this file with this path:
/Developer/Applications/Xcode.app/Contents/PlugIns/TextMacros.xctxtmacro/Contents/Resources/ObjectiveC.xctxtmacro 

2.Add follow code in the file:
{
Identifier = objc.bracket;
BasedOn = objc;
IsMenuItem = YES;
Name = "Bracket Expression";
TextString = "[<#!expression!#>]";
CompletionPrefix = bra;
},
This scope create a text macro "bra" to insert "[<#!expression!#>]", you need to type bra in your code and presse "esc" button, there is a window shows "Bracket Expression", press enter "[<#!expression!#>]" will be inserted.

You can create a similar macro for Brace, here is an example:
{
Identifier = objc.brace;
BasedOn = objc;
IsMenuItem = YES;
Name = "Brace Expression";
TextString = "{<#!expression!#>}";
CompletionPrefix = bra;
},
Here you have a same text macro "bra" to choose bracket or brace.

Update: In the XCode 3.2 which is released for Snow Leopard, the above lines wouldn't work until add this line in each section:

OnlyAtBOL = YES;

(Android) First application released

I just released my first application on the Android Market. It is Voice Dictionary which is a very useful tool for translating words or phrases between 17 different languages (Chinese, English, French, German, Italian, Spanish etc), the Voice Dictionary also supports Text-To-Speech to give you a full experience of pronunciation.

It uses Google Translate service API and eyes-free TTS engine for Android.

Here is a web version Android Market which contains this app's information.

Mar 17, 2009

(Android) How to write your code with a good style

I saw an excellent article in Android Code Source site, it is a Code Style Guide, which is Google Android team asks their developer to respect. Writing code with a good style helps others to read your code, and developer should have a good habit of writing code too.

Code Style Guide

Feb 13, 2009

(Objective-C) How to sort an array

Here is an example to sort an array:

- (NSArray *) sortArray:(NSArray *)array :(BOOL)inAscending {
if (inAscending) {
return [array sortedArrayUsingSelector:@selector(compare:)];
}
else {
NSArray *ascendArray = [array sortedArrayUsingSelector:@selector(compare:)];
return [[ascendArray reverseObjectEnumerator] allObjects];
}
}

Dec 24, 2008

(Android) How to use horizontal scrolling in a ListView

ListView is a widget which shows items in a vertically scrolling list. I figure out a solution to add horizontal scrolling in a ListView. Here is my new object called MyListView, i used OnGestureListener's onScroll methods to manage the horizontal scrolling.


public class MyListView extends LinearLayout implements OnGestureListener {
private GestureDetector mGestureDetector;
private ListView mListView;

public MyListView(Context context) {
super(context);
mGestureDetector = new GestureDetector(this);
mGestureDetector.setIsLongpressEnabled(false);
mListView = new ListView(context);
mListView.setItemsCanFocus(true);
String[] items = createStrins();
mListView.setAdapter(new ArrayAdapter(context, android.R.layout.simple_list_item_single_choice, items));
this.addView(mListView, new LinearLayout.LayoutParams(350, LayoutParams.FILL_PARENT));
}

@Override
public boolean onDown(MotionEvent arg0) {
return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return true;
}

@Override
public void onLongPress(MotionEvent e) {
//empty
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
int scrollWidth = mListView.getWidth() - this.getWidth();
if ((this.getScrollX() >= 0) && (this.getScrollX() <= scrollWidth) && (scrollWidth > 0)) {
int moveX = (int)distanceX;
if (((moveX + this.getScrollX()) >= 0) && ((Math.abs(moveX) + Math.abs(this.getScrollX())) <= scrollWidth)) {
this.scrollBy(moveX, 0);
}
else {
if (distanceX >= 0) {
this.scrollBy(scrollWidth - Math.max(Math.abs(moveX), Math.abs(this.getScrollX())), 0);
}
else {
this.scrollBy(-Math.min(Math.abs(moveX), Math.abs(this.getScrollX())), 0);
}
}
}
return true;
}

@Override
public void onShowPress(MotionEvent e) {
//empty
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev){
mGestureDetector.onTouchEvent(ev);
return true;
}

private String[] createStrins() {
return new String[] {
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance"};
}
}

Dec 19, 2008

(iPhone) How to localize your application

iPhone 3G embeds several language setting, it is pretty cool to write an application which has multi language support. Here are 2 solutions we could use to set an UIButton bt's title:

First:
- Find the device default language

NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:@"AppleLanguages"];
NSString* preferredLang = [languages objectAtIndex:0];

preferredLang returns the default language, it looks like: "en" for English, "fr" for French, "zh-Hans" for Chinese Simp, "zh-Hant" for Chinese Trad etc.

- Set up the text by different language

if ([preferredLang isEqualToString:@"en"]) {
[bt setTitle:@"Hello" forState:UIControlStateNormal];
}
else if ([preferredLang isEqualToString:@"fr"]) {
[bt setTitle:@"Bonjour" forState:UIControlStateNormal];
}
.......
else if ([preferredLang isEqualToString:@"zh-Hans"]) {
[bt setTitle:@"你好" forState:UIControlStateNormal];
}

Second:
- Create a strings file for your project (Add-New File-Strings File), Localizable.strings is a default strings file name, of course you can use the other names

- Make File Localizable: "Get Info" your strings file and click "Make File Localizable" on the left-bottom. You will see "English" shows on the Localizations list, and you can add the other languages by clicking "Add Localization". There are only 4 languages on the default list "English; French, Japnese and German". For the chinese, you should enter "zh_CN" for Chinese Simp and "zh_TW" for Chinese Trad, be careful it is different compare the first solution. After you have done this, there are several files under your strings file.

- Add the keys and values in the languages files. The format is:
in English
"hello" = "Hello !";

in French
"hello" = "Bonjour !";
etc.

Use the same keys for every language and just change the values. The iPhone will detect its language setting to load the right language file.

- Get the right text. If your strings file's name is Localizable, use NSLocalizedString(@"hello", nil) to get the value of "hello", if you use the other name, you should use NSLocalizedStringFromTable(@"hello", @"File name', nil) to return the text.

So the set bt's title, there is only one line of code:

[bt setTitle:NSLocalizedString(@"hello", nil) forState:UIControlStateNormal];