<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>foobarpig.com</title>
	
	<link>http://foobarpig.com</link>
	<description>tasty bits and pieces from the swill of my code</description>
	<lastBuildDate>Thu, 02 Jun 2011 09:55:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/foobarpig" /><feedburner:info uri="foobarpig" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>HTTP POST example (submiting a simple form)</title>
		<link>http://feedproxy.google.com/~r/foobarpig/~3/BDrAEbU9ca0/http-post-example-submiting-a-simple-form.html</link>
		<comments>http://foobarpig.com/android-dev/http-post-example-submiting-a-simple-form.html#comments</comments>
		<pubDate>Mon, 17 Jan 2011 03:17:22 +0000</pubDate>
		<dc:creator>Foobar Pig</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[BasicResponseHandler]]></category>
		<category><![CDATA[HttpClient]]></category>
		<category><![CDATA[HttpPost]]></category>
		<category><![CDATA[ResponseHandler]]></category>

		<guid isPermaLink="false">http://foobarpig.com/?p=123</guid>
		<description><![CDATA[I&#8217;ll be super brief this time and post only the code snippet of my postComment function, which does, what it says that it does &#8211; collects data from several form fields and submits it via POST request to defined URL (script, which saves that data in a database or does whatever you&#8217;d like it to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll be super brief this time and post only the code snippet of my postComment function, which does, what it says that it does &#8211; collects data from several form fields and submits it via POST request to defined URL (script, which saves that data in a database or does whatever you&#8217;d like it to do).</p>
<pre class="brush: java; title: ;">
private void postComment(){
    EditText nameEdit = (EditText)findViewById(R.id.edit_name);
    EditText commentEdit = (EditText)findViewById(R.id.edit_comment);
    String name = nameEdit.getText().toString();
    String comment = commentEdit.getText().toString();

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(&quot;http://example.com/data/feedmepostrequests.php&quot;);

    // set values you'd like to send
    List pairs = new ArrayList();
    pairs.add(new BasicNameValuePair(&quot;name&quot;, name));
    pairs.add(new BasicNameValuePair(&quot;comment&quot;, comment));
    pairs.add(new BasicNameValuePair(&quot;article_id&quot;, articleID));

    // you probably won't need these two lines below, but I have to work with web service
    // which has very annoying caching issues, so I usually pass some random argument
    // to avoid possible failure
    double r = Math.random();
    pairs.add(new BasicNameValuePair(&quot;rand&quot;, &quot;r&quot;+r));

    try {
        post.setEntity(new UrlEncodedFormEntity(pairs));
        // set ResponseHandler to handle String responses
        ResponseHandler&lt;String&gt; responseHandler = new BasicResponseHandler();
        String response = client.execute(post, responseHandler);
        Log.v(&quot;HttpPost&quot;, &quot;Response: &quot; + response);
        if (response.contains(&quot;SUCCESS&quot;)){
            // express your joy here!
        } else {
            // pop a sad Toast message here...
        }
    } catch (Exception e) {}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://foobarpig.com/android-dev/http-post-example-submiting-a-simple-form.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://foobarpig.com/android-dev/http-post-example-submiting-a-simple-form.html</feedburner:origLink></item>
		<item>
		<title>Disabling Activity “slide” animation on StartActivity, Finish() and BackPressed</title>
		<link>http://feedproxy.google.com/~r/foobarpig/~3/ULrylBf1JfI/how-to-disable-animation-on-startactivity-finish-and-backpressed.html</link>
		<comments>http://foobarpig.com/android-dev/how-to-disable-animation-on-startactivity-finish-and-backpressed.html#comments</comments>
		<pubDate>Wed, 05 Jan 2011 20:04:26 +0000</pubDate>
		<dc:creator>Foobar Pig</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[FLAG_ACTIVITY_NO_ANIMATION]]></category>
		<category><![CDATA[onBackPressed]]></category>
		<category><![CDATA[overridePendingTransition]]></category>
		<category><![CDATA[StartActivity]]></category>

		<guid isPermaLink="false">http://foobarpig.com/?p=120</guid>
		<description><![CDATA[This post is about Android development, because I&#8217;m currently doing both. Hopefully this year I&#8217;ll have enough time to update my blog more frequently. Anyway, back to programming: how to disable &#8220;slide&#8221; animation on various Activity related events. Disable animation on StartActivity Before starting your activity, you have to set flag FLAG_ACTIVITY_NO_ANIMATION to your Intent. [...]]]></description>
			<content:encoded><![CDATA[<p>This post is about Android development, because I&#8217;m currently doing both. Hopefully this year I&#8217;ll have enough time to update my blog more frequently. Anyway, back to programming: how to disable &#8220;slide&#8221; animation on various Activity related events.</p>
<h2>Disable animation on StartActivity</h2>
<p>Before starting your activity, you have to set flag FLAG_ACTIVITY_NO_ANIMATION to your Intent.</p>
<pre class="brush: java; title: ;">Intent myIntent = new Intent();
myIntent.setClassName(this, ExampleActivity.class.getName());
myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(myIntent); </pre>
<p>Unfortunately, this will not disable animations caused by back button or, if you&#8217;ve started SubActivity, on finish().</p>
<h2>Disable animation on finish() and back button pressed</h2>
<p><b>After</b> your finish() function put overridePendingTransition(0, 0):</p>
<pre class="brush: java; title: ;">Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
overridePendingTransition(0, 0);</pre>
<p>Same function helps with handling back button animation. Just override onBackPressed in your activity</p>
<pre class="brush: java; title: ;">	@Override
	public void onBackPressed() {
	  this.finish();
	  overridePendingTransition(0, 0);
	}</pre>
]]></content:encoded>
			<wfw:commentRss>http://foobarpig.com/android-dev/how-to-disable-animation-on-startactivity-finish-and-backpressed.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://foobarpig.com/android-dev/how-to-disable-animation-on-startactivity-finish-and-backpressed.html</feedburner:origLink></item>
		<item>
		<title>How to resize UIImage</title>
		<link>http://feedproxy.google.com/~r/foobarpig/~3/qxJ-V99FPJQ/uiimage-resize.html</link>
		<comments>http://foobarpig.com/iphone/uiimage-resize.html#comments</comments>
		<pubDate>Wed, 08 Sep 2010 21:53:34 +0000</pubDate>
		<dc:creator>Foobar Pig</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[UIImage]]></category>

		<guid isPermaLink="false">http://foobarpig.com/?p=117</guid>
		<description><![CDATA[I was looking for a simple way to resize an UIImage without too much mess and code, so this StackOverflow post really did help. The actual code: + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } Works like charm!]]></description>
			<content:encoded><![CDATA[<p>I was looking for a simple way to resize an UIImage without too much mess and code, so <a href="http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage/2658801#2658801">this StackOverflow post</a> really did help.</p>
<p>The actual code:</p>
<pre class="brush: cpp; title: ;">+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
</pre>
<p>Works like charm!</p>
]]></content:encoded>
			<wfw:commentRss>http://foobarpig.com/iphone/uiimage-resize.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://foobarpig.com/iphone/uiimage-resize.html</feedburner:origLink></item>
		<item>
		<title>Iterating through NSDictionary</title>
		<link>http://feedproxy.google.com/~r/foobarpig/~3/7nMsVJ4wxe4/iterating-through-nsdictionary.html</link>
		<comments>http://foobarpig.com/iphone/iterating-through-nsdictionary.html#comments</comments>
		<pubDate>Thu, 29 Jul 2010 06:26:50 +0000</pubDate>
		<dc:creator>Foobar Pig</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone-dev]]></category>
		<category><![CDATA[NSDictionary]]></category>
		<category><![CDATA[NSEnumerator]]></category>

		<guid isPermaLink="false">http://foobarpig.com/?p=113</guid>
		<description><![CDATA[Iteration through NSDictionary could be achieved at least in two ways: using NSArray with [NSDictionary allKeys] or NSEnumerator. Snippets: NSArray *keyArray = [bigUglyDictionary allKeys]; int count = [keyArray count]; for (int i=0; i &#60; count; i++) { NSDictionary *tmp = [bigUglyDictionary objectForKey:[ keyArray objectAtIndex:i]]; } NSEnumerator *enumerator = [bigUglyDictionary keyEnumerator]; id key; while ((key = [...]]]></description>
			<content:encoded><![CDATA[<p>Iteration through NSDictionary could be achieved at least in two ways: using NSArray with [NSDictionary allKeys] or NSEnumerator.<br />
Snippets:</p>
<pre class="brush: cpp; title: ;">	NSArray *keyArray =  [bigUglyDictionary allKeys];
	int count = [keyArray count];
	for (int i=0; i &lt; count; i++) {
		NSDictionary *tmp = [bigUglyDictionary objectForKey:[ keyArray objectAtIndex:i]];
	}
</pre>
<pre class="brush: cpp; title: ;">	NSEnumerator *enumerator = [bigUglyDictionary keyEnumerator];
	id key;
	while ((key = [enumerator nextObject])) {
		NSDictionary *tmp = [bigUglyDictionary objectForKey:key];
	}
</pre>
<p>Second way is a little bit faster, so if you work with huge dictionaries and have no need of array with their keys &#8211; use it.</p>
]]></content:encoded>
			<wfw:commentRss>http://foobarpig.com/iphone/iterating-through-nsdictionary.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://foobarpig.com/iphone/iterating-through-nsdictionary.html</feedburner:origLink></item>
		<item>
		<title>CoreText CTFramesetterSuggestFrameSizeWithConstraints vs UILabel sizeWithFont</title>
		<link>http://feedproxy.google.com/~r/foobarpig/~3/DIp6gQzA5M8/using-ctframesettersuggestframesizewithconstraints-and-sizewithfont-to-calculate-text-height.html</link>
		<comments>http://foobarpig.com/iphone/using-ctframesettersuggestframesizewithconstraints-and-sizewithfont-to-calculate-text-height.html#comments</comments>
		<pubDate>Wed, 28 Jul 2010 20:10:11 +0000</pubDate>
		<dc:creator>Foobar Pig</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[CFAttributedString]]></category>
		<category><![CDATA[CFRange]]></category>
		<category><![CDATA[CoreText]]></category>
		<category><![CDATA[CTFramesetterSuggestFrameSizeWithConstraints]]></category>
		<category><![CDATA[iphone-dev]]></category>
		<category><![CDATA[sizeWithFont]]></category>
		<category><![CDATA[UILabel]]></category>

		<guid isPermaLink="false">http://foobarpig.com/?p=108</guid>
		<description><![CDATA[I ran in to trouble with UILabel&#8217;s sizeWithFont while trying to determine how many CoreText columns will I need for my text. Apparently UILabel and CoreText, while uses same font and font size, has a little bit different line heights and that messes up any calculations. I was quite desperate and too lazy to study [...]]]></description>
			<content:encoded><![CDATA[<p>I ran in to trouble with UILabel&#8217;s sizeWithFont while trying to determine how many <a href="http://foobarpig.com/iphone/coretext-example-column-layout-with-correctly-inverted-text.html">CoreText columns</a> will I need for my text. Apparently UILabel and CoreText, while uses same font and font size, has a little bit different line heights and that messes up any calculations. I was quite desperate and too lazy to study various CoreText related references, so I ended up drawing invisible frames filled with text and counting them to get correct number of frames I will actually need.</p>
<p>Today I finally discovered CTFramesetterSuggestFrameSizeWithConstraints which can help with two important things: to get size of the frame you need for your CFAttributedString or to get how many characters of your string would fill frame with particular CGSize.</p>
<p>So, here is some code to illustrate different height results from both functions:</p>
<pre class="brush: cpp; title: ;">	//	test string
	NSString *text = @&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent pellentesque viverra est, molestie vehicula turpis consectetur sed. In hac habitasse platea dictumst. Maecenas vel dolor dolor, et hendrerit libero. Aliquam malesuada, erat vitae tempus tincidunt, ipsum tortor ultrices nunc, a rhoncus diam ligula nec purus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec nisi libero. Phasellus viverra imperdiet urna. Ut mollis vulputate metus sed sollicitudin. Aenean viverra tellus vitae tortor placerat imperdiet. Integer porta magna ut mauris euismod in tincidunt justo ornare. Donec ac leo augue. Quisque luctus vehicula nisi id ornare. Pellentesque quis mi ac arcu semper cursus at eu purus. &quot;;

	CGContextRef context = (CGContextRef)UIGraphicsGetCurrentContext();

	//	creating and formatting CFMutableAttributedString
	CFMutableAttributedStringRef attrStr = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
	CFAttributedStringReplaceString (attrStr, CFRangeMake(0, 0), (CFStringRef) text);
	CTFontRef font = CTFontCreateWithName(CFSTR(&quot;Times New Roman&quot;), fontSize, NULL);
	CTTextAlignment alignment = kCTJustifiedTextAlignment;
	CTParagraphStyleSetting _settings[] = {	{kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &amp;alignment} };
	CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(_settings, sizeof(_settings) / sizeof(_settings[0]));
	CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTParagraphStyleAttributeName, paragraphStyle);
	CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTFontAttributeName, font);
	CFRelease(paragraphStyle);
	CFRelease(font);	

	CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrStr);
	CFRelease(attrStr);

	int textLength = [text length];
	CFRange range;
	CGFloat maxWidth	= 100.0f;
	CGFloat maxHeight	= 10000.0f;
	CGSize constraint	= CGSizeMake(maxWidth, maxHeight);

	//	checking frame sizes
	CGSize labelSize	= [article.body sizeWithFont:[UIFont fontWithName:@&quot;Times New Roman&quot; size:16] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
	CGSize coreTextSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, textLength), nil, constraint, &amp;range);	

	//	printing results
	NSLog(@&quot;Label.sizeWithFont: %f x %f&quot;, labelSize.width, labelSize.height);
	NSLog(@&quot;CoreText.SuggestFrameSize: %f x %f&quot;, coreTextSize.width, coreTextSize.height);
	NSLog(@&quot;Character count in constraint frame: %d (total: %d)&quot;, range.length, textLength);
</pre>
<p>Results:</p>
<pre class="brush: cpp; title: ;">Label.sizeWithFont: 100.000000 x 1160.000000
CoreText.SuggestFrameSize: 100.000008 x 1059.507812
Character count in constraint frame: 718 (total: 718)</pre>
<p>100px is significant difference and it increases proportionally with text size. Also to demonstrate CFRange usefulness I&#8217;ll use 100&#215;100 constraint:</p>
<pre class="brush: cpp; title: ;">Label.sizeWithFont: 99.000000 x 100.000000
CoreText.SuggestFrameSize: 100.000000 x 84.390625
Character count in constraint frame: 66 (total: 718)</pre>
<p>SuggestFrameSize results are interesting in this one. I guess, we get 84.39 height, because CoreText couldn&#8217;t fit another line, so updated constraint frame with REAL height.</p>
]]></content:encoded>
			<wfw:commentRss>http://foobarpig.com/iphone/using-ctframesettersuggestframesizewithconstraints-and-sizewithfont-to-calculate-text-height.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://foobarpig.com/iphone/using-ctframesettersuggestframesizewithconstraints-and-sizewithfont-to-calculate-text-height.html</feedburner:origLink></item>
		<item>
		<title>CoreText: set text font and alignment to CFAttributedString</title>
		<link>http://feedproxy.google.com/~r/foobarpig/~3/FxwBm_1_5RU/coretext-set-text-font-and-alignment-to-cfattributedstring.html</link>
		<comments>http://foobarpig.com/iphone/coretext-set-text-font-and-alignment-to-cfattributedstring.html#comments</comments>
		<pubDate>Mon, 12 Jul 2010 09:19:04 +0000</pubDate>
		<dc:creator>Foobar Pig</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[CFAttributedString]]></category>
		<category><![CDATA[CoreText]]></category>
		<category><![CDATA[CTFontCreateWithName]]></category>
		<category><![CDATA[CTParagraphStyle]]></category>
		<category><![CDATA[CTTextAlignment]]></category>

		<guid isPermaLink="false">http://foobarpig.com/?p=102</guid>
		<description><![CDATA[For a person like me, who is not familiar with working with CoreText most common tasks can be really painful. I hope this post saves you some time if you&#8217;re looking for information on how to set font and align your CFAttributedString. For text alignment you have to assign CTTextAlignment to CTParagraphStyle first and then [...]]]></description>
			<content:encoded><![CDATA[<p>For a person like me, who is not familiar with working with <strong>CoreText</strong> most common tasks can be really painful. I hope this post saves you some time if you&#8217;re looking for information on how to set font and align your <strong>CFAttributedString</strong>. For text alignment you have to assign <strong>CTTextAlignment</strong> to <strong>CTParagraphStyle</strong> first and then set that <strong>CTParagraphStyle</strong> to your <strong>CFAttributedString</strong>. Setting font has more direct approach &#8211; <strong>CFAttributedString</strong> has special attribute for that.</p>
<p>In clearer terms aka code form:</p>
<pre class="brush: cpp; title: ;">//    create attributed string
 CFMutableAttributedStringRef attrStr = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
 CFAttributedStringReplaceString (attrStr, CFRangeMake(0, 0), (CFStringRef) textString);

 //    create font
 CTFontRef font = CTFontCreateWithName(CFSTR(&quot;Times New Roman&quot;), 16, NULL);

 //    create paragraph style and assign text alignment to it
 CTTextAlignment alignment = kCTJustifiedTextAlignment;
 CTParagraphStyleSetting _settings[] = {    {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &amp;alignment} };
 CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(_settings, sizeof(_settings) / sizeof(_settings[0]));

 //    set paragraph style attribute
 CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTParagraphStyleAttributeName, paragraphStyle);

 //    set font attribute
 CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTFontAttributeName, font);

 //    release paragraph style and font
 CFRelease(paragraphStyle);
 CFRelease(font);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://foobarpig.com/iphone/coretext-set-text-font-and-alignment-to-cfattributedstring.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://foobarpig.com/iphone/coretext-set-text-font-and-alignment-to-cfattributedstring.html</feedburner:origLink></item>
		<item>
		<title>CoreText example: Column Layout (correctly inverted text)</title>
		<link>http://feedproxy.google.com/~r/foobarpig/~3/eEiv_bipjLw/coretext-example-column-layout-with-correctly-inverted-text.html</link>
		<comments>http://foobarpig.com/iphone/coretext-example-column-layout-with-correctly-inverted-text.html#comments</comments>
		<pubDate>Wed, 30 Jun 2010 19:43:18 +0000</pubDate>
		<dc:creator>Foobar Pig</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[CGContextScaleCTM]]></category>
		<category><![CDATA[CGContextTranslateCTM]]></category>
		<category><![CDATA[CoreText]]></category>
		<category><![CDATA[iphone-dev]]></category>
		<category><![CDATA[layout]]></category>

		<guid isPermaLink="false">http://foobarpig.com/?p=91</guid>
		<description><![CDATA[You can find an &#8220;Columnar layout&#8221; example at apple.com library, but as for today it&#8217;s targeted to Mac OSX, not iPhone or iPad development, and causes lots of confusion and even more errors. I&#8217;ve changed it a bit and after some trial and error managed to create working CoreText example, which puts text in two [...]]]></description>
			<content:encoded><![CDATA[<p>You can find an <a href="http://developer.apple.com/iphone/library/documentation/StringsTextFonts/Conceptual/CoreText_Programming/Operations/Operations.html#//apple_ref/doc/uid/TP40005533-CH4-SW1">&#8220;Columnar layout&#8221;</a> example at apple.com library, but as for today it&#8217;s targeted to Mac OSX, not iPhone or iPad development, and causes lots of confusion and even more errors.</p>
<p>I&#8217;ve changed it a bit and after some trial and error managed to create working CoreText example, which puts text in two columns. Oh, and letters are proper side up &#8211; looks like CoreText and iPhone works in different coordinate system. Most important lines to fix this issue are:</p>
<pre class="brush: cpp; title: ;">CGContextTranslateCTM(context, 0, _columnHeight);
CGContextScaleCTM(context, 1.0, -1.0);</pre>
<p><strong>CGContextTranslateCTM</strong> &#8211; Changes the origin of the user coordinate system in a context. It&#8217;s important to set your columns and CGContextTranslateCTM argument to the same height. I&#8217;ve spent too much time trying to figure out why everything is messed up while my columns was much shorter then the height I was passing to this function.<br />
<strong>CGContextScaleCTM</strong> &#8211; Changes the scale of the user coordinate system in a context.</p>
<p>Result before these coordinate changes:<br />
<img class="size-full wp-image-93" title="Screen shot 2010-06-30 at 22.33.44" src="http://foobarpig.com/wp-content/uploads/2010/06/Screen-shot-2010-06-30-at-22.33.44.png" alt="" width="393" height="300" /></p>
<p>Before that massive chunk of code, I just want to be clear, that I&#8217;ve declared some variables in .h file, so use your own:</p>
<pre class="brush: cpp; title: ;">_columnCount = 2;
_columnHeight = 300;
</pre>
<p>And full example bellow:</p>
<pre class="brush: cpp; title: ;">- (CFArrayRef)createColumns {
    CGRect bounds = CGRectMake(0, 0, 400, _columnHeight);
    int column;

    CGRect* columnRects = (CGRect*)calloc(_columnCount, sizeof(*columnRects));

    // Start by setting the first column to cover the entire view.
    columnRects[0] = bounds;

    // Divide the columns equally across the frame's width.
    CGFloat columnWidth = CGRectGetWidth(bounds) / _columnCount;

    for (column = 0; column &lt; _columnCount - 1; column++) {
        CGRectDivide(columnRects[column], &amp;columnRects[column],&amp;columnRects[column + 1], columnWidth, CGRectMinXEdge);
    }

	//	add margin
    for (column = 0; column &lt; _columnCount; column++) {
        columnRects[column] = CGRectInset(columnRects[column], 10.0, 10.0);

    }

	// Create an array of layout paths, one for each column.
    CFMutableArrayRef array = CFArrayCreateMutable(kCFAllocatorDefault, _columnCount, &amp;kCFTypeArrayCallBacks);
    for (column = 0; column &lt; _columnCount; column++) {
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddRect(path, NULL, columnRects[column]);
        CFArrayInsertValueAtIndex(array, column, path);
        CFRelease(path);
    }

    free(columnRects);
    return array;
}

- (void)drawRect:(CGRect)rect {
    [[UIColor whiteColor] set];
	CGContextRef context = (CGContextRef)UIGraphicsGetCurrentContext();

    CGContextTranslateCTM(context, 0, _columnHeight);
    CGContextScaleCTM(context, 1.0, -1.0);

    [UIBezierPath bezierPathWithRect:[self bounds]];

	CFStringRef string = (CFStringRef) @&quot;Long text.\n\nfoobarpig.com&quot;;
	CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(CFAttributedStringCreate(NULL, string, NULL));

    CFArrayRef columnPaths = [self createColumns];
    CFIndex pathCount = CFArrayGetCount(columnPaths);

    CFIndex startIndex = 0;

    int column;
    for (column = 0; column &lt; pathCount; column++) {

        CGPathRef path = (CGPathRef)CFArrayGetValueAtIndex(columnPaths, column);

        // Create a frame for this column and draw it.
        CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(startIndex, 0), path, NULL);
        CTFrameDraw(frame, context);

        // Start the next frame at the first character not visible in this frame.
        CFRange frameRange = CTFrameGetVisibleStringRange(frame);
        startIndex += frameRange.length;
        CFRelease(frame);
    }
    CFRelease(columnPaths);
}
</pre>
<p>Final result:<br />
<img class="size-full wp-image-94" title="Screen shot 2010-06-30 at 22.34.02" src="http://foobarpig.com/wp-content/uploads/2010/06/Screen-shot-2010-06-30-at-22.34.02.png" alt="" width="393" height="289" /></p>
<p>Last thing: do not forget to add <strong>CoreText</strong> framework to your project and <strong>#import &lt;CoreText/CoreText.h&gt;.</strong> That&#8217;s all I guess.</p>
]]></content:encoded>
			<wfw:commentRss>http://foobarpig.com/iphone/coretext-example-column-layout-with-correctly-inverted-text.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://foobarpig.com/iphone/coretext-example-column-layout-with-correctly-inverted-text.html</feedburner:origLink></item>
		<item>
		<title>Changing UITableView width</title>
		<link>http://feedproxy.google.com/~r/foobarpig/~3/dzqQ17Pyhdg/how-to-change-uitableview-row-width.html</link>
		<comments>http://foobarpig.com/iphone/how-to-change-uitableview-row-width.html#comments</comments>
		<pubDate>Mon, 28 Jun 2010 17:04:59 +0000</pubDate>
		<dc:creator>Foobar Pig</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone-dev]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[UITableViewController]]></category>

		<guid isPermaLink="false">http://foobarpig.com/?p=86</guid>
		<description><![CDATA[I&#8217;m back to programming and currently developing application for iPad. I needed something, what looked like a simplest thing &#8211; UITableView taking a portion of screen and SplitViewController wasn&#8217;t attractive option. To my surprise non of the obvious to me solutions worked until I&#8217;ve tried not-so-elegant reallocating of tableView. So, in myTableViewController I created custom [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m back to programming and currently developing application for iPad. I needed something, what looked like a simplest thing &#8211; <strong>UITableView</strong> taking a portion of screen and <strong>SplitViewController</strong> wasn&#8217;t attractive option. To my surprise non of the obvious to me solutions worked until I&#8217;ve tried not-so-elegant reallocating of <em>tableView</em>.</p>
<p>So, in <em>myTableViewController</em> I created custom <em>init</em> function:</p>
<pre class="brush: cpp; title: ;">- (id) initWithFrame:(CGRect)frm {
	if ((self = [super initWithStyle: UITableViewStylePlain])){
		self.tableView = [[UITableView alloc] initWithFrame:frm style:UITableViewStylePlain];
	}
	return self;
}</pre>
<p>Obviously, you can use default or your own <em>init</em> function or avoid passing frame argument and hardcode dimensions right here, but the magical line is:</p>
<pre class="brush: cpp; title: ;">self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(x, y, width, height) style:UITableViewStylePlain];</pre>
<p>Well, at least it worked for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://foobarpig.com/iphone/how-to-change-uitableview-row-width.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://foobarpig.com/iphone/how-to-change-uitableview-row-width.html</feedburner:origLink></item>
		<item>
		<title>Explode/split NSString</title>
		<link>http://feedproxy.google.com/~r/foobarpig/~3/FAM2G1eqw8k/explode-split-nsstring.html</link>
		<comments>http://foobarpig.com/iphone/explode-split-nsstring.html#comments</comments>
		<pubDate>Thu, 18 Mar 2010 10:56:51 +0000</pubDate>
		<dc:creator>Foobar Pig</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[componentsSeparatedByCharactersInSet]]></category>
		<category><![CDATA[componentsSeparatedByString]]></category>
		<category><![CDATA[NSCharacterSet]]></category>
		<category><![CDATA[NSString]]></category>

		<guid isPermaLink="false">http://foobarpig.com/?p=80</guid>
		<description><![CDATA[I&#8217;m back to iPhone programming and, yeah, sometimes I tend to forget the simplest things. So here it is, a way to split a string by some separator string (in our case &#8211; &#8220;/&#8221;) componentsSeparatedByString NSString *exampleURL = @&#34;http://foobarpig.com/iphone/&#34;; NSArray *pieces = [exampleURL componentsSeparatedByString:@&#34;/&#34;]; NSLog(@&#34;%@&#34;, pieces); And result is a nice array of strings separated [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m back to iPhone programming and, yeah, sometimes I tend to forget the simplest things. So here it is, a way to split a string by some separator string (in our case &#8211; &#8220;/&#8221;)</p>
<h2>componentsSeparatedByString</h2>
<pre class="brush: cpp; title: ;">NSString *exampleURL = @&quot;http://foobarpig.com/iphone/&quot;;
NSArray *pieces = [exampleURL componentsSeparatedByString:@&quot;/&quot;];
NSLog(@&quot;%@&quot;, pieces);
</pre>
<p>And result is a nice array of strings separated by &#8220;/&#8221;:</p>
<pre class="brush: cpp; title: ;">
&quot;http:&quot;,
&quot;&quot;,
&quot;foobarpig.com&quot;,
iphone,
&quot;&quot;
</pre>
<h2>componentsSeparatedByCharactersInSet</h2>
<p>In cases, when you need to split your string by more than one separator, use componentsSeparatedByCharactersInSet function.</p>
<pre class="brush: cpp; title: ;">NSString *exampleURL = @&quot;http://foobarpig.com/iphone/&quot;;
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@&quot;/:.&quot;];  // note 3 separators: &quot;/&quot;, &quot;:&quot; and &quot;.&quot;
NSArray *pieces = [exampleURL componentsSeparatedByCharactersInSet:charSet];
NSLog(@&quot;%@&quot;, pieces);</pre>
<p>Result:</p>
<pre class="brush: cpp; title: ;">
    http,
    &quot;&quot;,
    &quot;&quot;,
    foobarpig,
    com,
    iphone,
    &quot;&quot;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://foobarpig.com/iphone/explode-split-nsstring.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://foobarpig.com/iphone/explode-split-nsstring.html</feedburner:origLink></item>
		<item>
		<title>TouchXML installation guide</title>
		<link>http://feedproxy.google.com/~r/foobarpig/~3/XRvGiwTt6jo/touchxml-installation-guide.html</link>
		<comments>http://foobarpig.com/iphone/touchxml-installation-guide.html#comments</comments>
		<pubDate>Fri, 05 Feb 2010 11:49:10 +0000</pubDate>
		<dc:creator>Foobar Pig</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone-dev]]></category>
		<category><![CDATA[libxml]]></category>
		<category><![CDATA[TouchXML]]></category>

		<guid isPermaLink="false">http://foobarpig.com/?p=72</guid>
		<description><![CDATA[TouchXML is a libxml API wrapper written in Objective-C and usually helps with all your project XML needs. While writing my post about parsing XML element attributes and putting up demo project I realized that I tend to forget how to add TouchXML to new project, so here goes step-by-step of that procedure: 1. Get [...]]]></description>
			<content:encoded><![CDATA[<p>TouchXML is a libxml API wrapper written in Objective-C and usually helps with all your project XML needs. While writing my post about <a title="parsint xml attributes with touchxml" href="http://foobarpig.com/iphone/parsing-xml-element-attributes-with-touchxml.html" target="_self">parsing XML element attributes</a> and putting up demo project I realized that I tend to forget how to add TouchXML to new project, so here goes step-by-step of that procedure:</p>
<h2>1. Get TouchXML</h2>
<p><del datetime="2011-06-02T09:53:34+00:00">You can find archives to download in <a href="http://code.google.com/p/touchcode/downloads/list" target="_blank">touchcode project downloads</a> .</del> Go to <a href="https://github.com/TouchCode/TouchXML">TouchCode github page</a> and download TouchXML archive and extract it anywhere you like. It&#8217;s common practice to keep such libraries and classes in <em>Developer/ExtraLibs</em> directory.</p>
<h2>2. Enable <em>libxml2</em> library</h2>
<p>First things first, before we actually add TouchXML files, we need to do some project configuration changes, so our project could use libxml library.</p>
<p>1. Go to &#8220;Project -&gt; Edit project settings&#8221;</p>
<p>2. Activate &#8220;Build&#8221; tab</p>
<p>3. Search for &#8220;Header search paths&#8221; setting and add <strong><em>/usr/include/libxml2</em></strong> value to it</p>
<p><img class="size-full wp-image-73 alignnone" title="header search paths" src="http://foobarpig.com/wp-content/uploads/2010/02/header-search-paths.png" alt="libxml header search paths" width="593" height="222" /></p>
<p>4. Search for &#8220;Other linker flags&#8221; setting and add <strong><em>-lxml2</em></strong> value</p>
<p><img class="alignnone size-full wp-image-74" title="other linker flags" src="http://foobarpig.com/wp-content/uploads/2010/02/other-linker-flags.png" alt="touchxml other linker flags" width="601" height="200" /></p>
<p>P.s. notice that search function is really useful for finding settings you need faster.</p>
<h2>3. Add TouchXML classes</h2>
<p>1. Right click (option click) on your projects &#8220;Classes&#8221; folder and go to &#8220;Add -&gt; Existing files&#8230;&#8221;</p>
<p><a href="http://foobarpig.com/wp-content/uploads/2010/02/Screen-shot-2010-02-05-at-13.19.03.png"><img class="alignnone size-full wp-image-75" title="Add existing files" src="http://foobarpig.com/wp-content/uploads/2010/02/Screen-shot-2010-02-05-at-13.19.03.png" alt="" width="494" height="319" /></a></p>
<p>2. Navigate to the directory where extracted TouchXML is kept and browse  deeper to &#8220;Common -&gt; Source&#8221;. Select everything! And click &#8220;Add&#8221; obviously.</p>
<p><img class="alignnone size-full wp-image-76" title="Screen shot 2010-02-05 at 13.24.23" src="http://foobarpig.com/wp-content/uploads/2010/02/Screen-shot-2010-02-05-at-13.24.23.png" alt="touchxml classes" width="660" height="464" /></p>
<p>3. Confirm.</p>
<p><img class="alignnone size-full wp-image-77" title="Screen shot 2010-02-05 at 13.25.37" src="http://foobarpig.com/wp-content/uploads/2010/02/Screen-shot-2010-02-05-at-13.25.37.png" alt="" width="403" height="379" /></p>
<p>Now you should see a bunch of new files in your project. I usually group them by selecting files I wish to group and then selecting &#8220;Group&#8221; in context (right click/option click) menu.</p>
<h2>4. Import TouchXML to your project</h2>
<pre class="brush: cpp; title: ;">#import &quot;TouchXML.h&quot;</pre>
<p>That is all the &#8220;magic&#8221; and you&#8217;re good to go. Since, I am not going to write about actually using TouchXML, you can see a nice working example in <a title="TouchXML in action" href="http://foobarpig.com/iphone/parsing-xml-element-attributes-with-touchxml.html" target="_self">my previous post</a>.</p>
<h2>5. Common errors</h2>
<h3>Error: libxml/tree.h: No such file or directory</h3>
<p>&#8230; and hundreds of something missing errors. It means that something went wrong with &#8220;Header search paths&#8221;. Maybe you didn&#8217;t added <em>/usr/include/libxml2<br />
</em> or added incorrectly? Check it.</p>
<h3>Error: &#8220;_xmlDocDumpFormatMemory&#8221;, referenced from:- [CXMLDocument description] in CXMLDocument.o</h3>
<p>&#8230; and tens of errors like this. While errors by them selfs aren&#8217;t very expressive, they wish to inform you, that you did not added <em>-lxml2</em> flag to &#8220;Other linker flags&#8221;</p>
<p>And that is all for now!</p>
]]></content:encoded>
			<wfw:commentRss>http://foobarpig.com/iphone/touchxml-installation-guide.html/feed</wfw:commentRss>
		<slash:comments>55</slash:comments>
		<feedburner:origLink>http://foobarpig.com/iphone/touchxml-installation-guide.html</feedburner:origLink></item>
	</channel>
</rss>
