How To Create a UIButton in xCode With Two Lines of Text and Centered

June 1, 2010

Simply using Interface Builder, I could not find a way to make my buttons center multiple lines of text. If there is a super easy way, then let me know and I’ll update this post (but I don’t currently think there is). So I figured out how to do it programmatically, which is what I’m showing here in this post.

Go ahead and create your button using Interface Builder (or programmatically). If you use Interface Builder, then make sure you create the IBOutlets in the header file and appropriately link those attributes to the UIButtons in Interface Builder.

Once you’ve got the buttons created and linked, you’re ready to set the text for the button labels. To center the text you’ll need to use:

myCustomButton.titleLabel.textAlignment = UITextAlignmentCenter;

And to set the text so that it appears in two lines you’ll need to use:

[myCustomButton setTitle:@"theTopText\nBottomText" forState:UIControlStateNormal];

“myCustomButton” would be replaced by whatever name you’ve given your UIButton. Obviously you can use dot syntax or [brackets] for either line.

Notice that the “\n” is used as a line break so that your button label will read “the Top Text” on the top and “Bottom Text” below it. Both will be centered as a result of you setting the text alignment.

Here is a snippet of my code and the resulting buttons:
Code Sample of Setting Text and Centering UIButton labelUIButtons with centered text and multiple=



The buttons shown in my example happen to be in a custom UITableViewFooter that I created using Interface Builder, but the tricks taught in this post should work with any and all UIButtons.
1

Fixing “Potential null dereference. According to coding standards in ‘Creating and Returning NSError Objects’ the parameter ‘error’ may be null” in SFHFKeychainUtils running the Static Analyzer

May 28, 2010

Real quick post- if you run static analyzer, Clang, etc and it’s giving you this issue, it’s an easy fix. The error references the documentation that you can look at: “Creating and Returning NSError Objects” -see section 3-5.

This is what the error will look like:
SFHFKeychainUtils Potential Null dereference
This is how you fix it:
Fixing the potential null dereference error
Pretty simple just put an if statement around it to make sure error isn’t null already!

Just saw this:
http://github.com/yllan/scifihifi-iphone/commit/9816a98f674b2933588e0447a9148ecd626882eb (unfortunately it’s not incorporated yet into the Master)

0

Simple iPhone Tutorial: Password Management using the keychain by using SFHFKeychainUtils

May 23, 2010

Keychain is really a pain to use, and I didn’t want to spend the time to figure it all out, so I went looking for a framework that would do the dirty work for me. I found SFHFKeychainUtils and it has been super slick! In this tutorial, I’ll show you how to leverage SFHFKeychainUtils in order to save a username/password to keychain and also retrieve the password given the username. Just follow these simple steps:

1. Get SFHFKeychainUtils

I found it on GitHub at http://github.com/ldandersen/scifihifi-iphone/tree/master/security/ – just get the SFHFKeychainUtils.h and SFHFKeychainUtils.m files and add them to your project.

2. Import it

In the implementation file where you want to save or retrieve the password, be sure you import the SFHF Keychain Utils header file as shown below:

#import “LoginViewController.h”
#import “SFHFKeychainUtils.h”
@implementation LoginViewController

3. Save a Username/Password

xCode SFHFKeychainUtils iPhone Keychain Example





Here you can see how simple it is to add the username/password to the iPhone’s keychain. The storeUsername:andPassword:forServiceName:updateExisting:error method will take care of all the work for us if we give it the correct parameters. I am getting the username and password from two UITextFields in the app called usernameField and passwordField, respectively. The Service Name can be anything you want, as long as you remember it as you’ll need to enter that same string value in order to retrieve the password (see step 4).

4. Retrieve the Password from Keychain

xCode SFHFKeychainUtils iPhone Keychain Example




As you can see, it’s not any more difficult to retrieve the password from keychain using SFHFKeychainUtils. I specified my username “Gorgando” and the same service name we used before “myApp”. The password that corresponds to this username and service name in the keychain will be returned as an NSString.

So don’t be afraid to use the keychain! It is the most secure way to store passwords in an iPhone application. Both plists and the Settings.Bundle are very insecure ways to store passwords because the passwords are stored in plaintext, visible to anyone who accesses them. Let me know if you have any questions – good luck!

0

Robin Hood – The Untold Story of How the Man Became A Legend: Review – Another Good Movie

May 22, 2010

Just got back from watching the new Robin Hood movie. I had heard some negative reviews from friends and families saying it had “a weak plot” or was “slow at times” or “too violent”… Except for the violence, I’m wondering if they saw the same movie I did because I was really impressed.

This Robin Hood movie is definitely a new spin that I’ve never seen or read anywhere on famous outlaw Robin Hood. Be prepared to think of King Richard and Maid Marion and their relationship with Robin in a different light. I thought the plot was VERY well done. The plot was complex, well developed, and tied up very nicely in the end while cleverly laying the groundwork for the real Robin Hood story. The acting was great, and the movie was action-packed. I never once thought to myself, “come on let’s get this movie moving…” like I did in Iron Man 2, which has a much weaker plot.

I give it 4.2/5 Stars (Good)

The Good:

  • Great plot
  • Superb acting by Russell Crowe, Cate Blanchett, and cast
  • Promoted good morals: integrity, honesty, loyalty, courage, liberty/freedom, etc (much different from a lot of the filth the media offers us these days)
  • Nearly non-stop action
  • All-around very enjoyable to watch
  • I don’t recall a single swear word, which is rare for an action movie with battle scenes
  • No sex scenes

The Bad:

  • Too violent for kids (in my opinion)
  • The violence is pretty graphic (i.e. showing arrows go through necks, etc)
  • Some suggestiveness but really it was a pretty clean movie in that regard

Go see it!

0

Easy Excel VBA Tutorial: How to Make a Simple Splash Screen for Excel Workbook using VBA

May 16, 2010

It is actually very simple to create a splash screen for your Excel workbook. Just follow the steps below:

1. Create the User Form

In Excel hit Alt-F11 to bring up the Visual Basic Editor. In the Project Explorer Window, right click on your project, select Insert > User Form. I recommend you rename the user form something like “SplashScreen” in the Properties box. Drag an Image control from the toolbox onto your user form. In the Properties, select the cell next to “Picture” and click on the button with the “…”. This will allow you to select your image. Resize the image and form to your heart’s content.

2. Set Up the Splash Screen

Right-click on your user form and select “View Code”. It should automatically create a sub routine that looks like this:

Private Sub SplashScreen_Click()
End Sub

Change that code so that it looks like the following (or just add the following code below it):

Private Sub UserForm_Initialize()

Application.OnTime Now + TimeValue(“00:00:02″), “KillForm”

End Sub

[Brief explanation - this is the Initialize sub routine that is called whenever the user form opens. Application.OnTime means "Do what I tell you when I tell you to do it". The "Now + TimeValue("00:00:02")" is the when - basically do it right now plus 2 additional seconds, or "wait two seconds to do it". The "KillForm" just tells the form to disappear. To sum it up, this sub is just making the form disappear 2 seconds after it is launched.]

Feel free to change the duration that the splash screen stays visible by changing the 00:00:02 to something else.

3. Show the Splash Screen at Launch

In the Project Explorer Window (we’re still in the Visual Basic Editor), open the file called “ThisWorkbook”. It will most likely be empty if you have not previously added code here. Enter the following code, but change “SplashScreen” to be whatever you named your user form:

Private Sub Workbook_Open()

SplashScreen.Show

End Sub

[Brief explanation - this Workbook_Open sub will run every time the Excel workbook opens. Your user form name .Show just displays the user form, which will then trigger the Initialize sub that we set up in step 2.]

Make sure to save as a Macro-Enabled Workbook (xls or xlsm) and YOU’RE DONE!

Every time it’s opened, you’re splash screen should display for the amount of time you set. Let me know if you have any questions and good luck!

0

Iron Man 2: Review – Good but not Great

May 8, 2010

Watched Iron Man 2 last night and it didn’t quite live up to the hype. Half way through the movie, I leaned over to Jessica and whispered, “It’s tough to make a good Sequel,” and that’s how I felt about the movie overall. Make sure you stay and watch all the credits because there is a final scene afterwards that hints at what’s to come :) .

I give it 4/5 stars (Good).

The good:

  • Action-packed with some sweet special effects
  • Great acting
  • Fairly clean (not much language, sex)
  • It’s Iron Man, so of course it’s a lot of fun to watch
  • Good humor

The bad:

  • Weak plot
  • Predictable plot – I thought that the villain was way too predictable
  • A little too much “I’m dying and I feel sorry for myself” going on… (too much destructive behavior)

I recommend you go see it, but you probably won’t be dying to see it a second time.

2

Simple Example-Tutorial: REGEX (Regular Expressions) in iPhone app to find and replace a value in NSString

May 8, 2010

Since there is no native ability in Objective C in the Cocoa Framework to use REGEX (Regular Expressions), you need to use a framework. I found that RegexKitLite was the perfect solution for my app. You can use it in 3 simple steps:

1. Add the RegexKit Framework to your app’s project in XCode

You can download the source at http://regexkit.sourceforge.net/#Downloads. Then just add the RegexKitLite.h and RegexKitLite.m files to your project. Also, you’ll need to add the libicucore.dylib Framework to your project. This can be done by right clicking on the Frameworks folder in xCode, selecting Add > Add Existing Framework and then selecting to add libicucore.dylib. Be sure to import the RegexKitLite.h file wherever you plan to use it: #import “RegexKitLite.h”.

2. Create an NSString with the Regular Expression

For example I needed a regular expression that would find all the HTML tags in an NSString and get rid of them by replacing them with an empty string “”. It looks like this:

NSString *myregex = @”<[^>]*>”; //regex to remove any html tag

3. Use the StringByReplacingOccurencesOfRegex method to replace your NSString

I think the best way to explain this is to just show it:

NSString *description = @”<html><body><p>This is the description! </p></body></html>”;
description = [description stringByReplacingOccurrencesOfRegex:myregex withString:@""];

The final description variable will just say “This is the description! ” as the tags are stripped out. All you have to do is plug in your regex NSString variable from step 2 where I have “myregex” and plug in whatever string you want to replace it with where I have @”" (an empty string to delete the occurrence rather than replace it).

Hope this is helpful- good luck!

0

Excel – Using Data Validation Lists to Create Dropdown Lists within Cells

May 4, 2010
Cell dropdown list or combo box using data validation in Excel

Data validation list in Excel cell

Using data validation is extremely simple and you don’t even have to use VBA to do it. I’m using Excel 2010, but the process is the same in 2007. I’ll show you how to set up the data validation and two ways to populate the list, dynamically and hard-coded.

With Hard-coded list values:

  1. Go to Data > Data Validation > Data Validation and the Data Validation dialog box should appear.

    Data Validation Icon

    The Data Validation Icon

  2. In the “Allow:” list choose the “List” option. The Ignore Blanks check box allows the field to be empty if an option has not yet been chosen. Keep the “in-cell dropdown” option checked so that the dropdown list will appear within the cell.
  3. In the “Source:” field, enter in the list values, separated by commas.

    Hard-coded Data Validation List

    Enter comma separated values to appear in the list

  4. If desired, you can customize a message displayed to the user when the cell is selected and/or customize the error message displayed to the user when a value other than those in the lits is entered in the other two tabs of the Data Validation dialog box.
  5. Click OK and you’re good to go!  You can now copy this cell anywhere else and it will keep the same data validation rules.

With Dynamic values:

  1. Go to Data > Data Validation > Data Validation and the Data Validation dialog box should appear.
  2. Data Validation Icon

  3. In the “Allow:” list choose the “List” option. The Ignore Blanks check box allows the field to be empty if an option has not yet been chosen. Keep the “in-cell dropdown” option checked so that the dropdown list will appear within the cell.
  4. Instead of typing a hard-coded list in the “Source:” field, select on your worksheet the cells that you would like to use in the list. A trick I learned, is that any empty cells selected will not show up in the list. This is handy so that you can select empty cells and later add data to them so that the new data is added to the list (Dynamic!). Also, it is important to note that the references are absolute references with the dollar signs (like $E$2:$E$24) and the worksheet is specified (=Other! since the worksheet is named “Other”). That’s all there is to it!

    Dynamic Data Validation List

    Select the cells to populate the list

  5. If desired, you can customize a message displayed to the user when the cell is selected and/or customize the error message displayed to the user when a value other than those in the lits is entered in the other two tabs of the Data Validation dialog box.
  6. Click OK and you’re good to go!  You can now copy this cell anywhere else and it will keep the same data validation rules.
0

Cascade Golf Center – Orem, Utah

April 24, 2010

Despite having an absolutely awful website (at least for Chrome users), I have had great experiences on their golf course. I’ve golfed here several times. The back 9 or “Mountain 9″ is a very fun challenge as you lose plenty of balls off of cliffs or trying to hit the ball over a mountain… This morning I played the front 9 (walking). The course was in a little rough shape after the storms this week, but I would still recommend it. The price is really good (under $30 for two of us) and it’s a fun place to play. It’s a family-friendly course that is not too difficult to bring beginners. They also have a nice driving range, putting/chipping area, and miniature golf area.

Website: www.cascadegolfcenter.com
Location: Map

0

“Not In” Subquery with ASP.Net LINQ in VB

April 3, 2010
Tags: , , ,

I looked all over to find a way to do subqueries and “not in” statements using LINQ. This post is to share the simplest way that I’ve found.

Here is the SQL query that I was trying to recreate in LINQ:

SELECT DISTINCT lu.leagueid, lu.id, AS Expr1, l.name
FROM            leagueuser AS lu INNER JOIN
league AS l ON lu.leagueid = l.id
WHERE        (l.name NOT IN
(SELECT        league.name
FROM            league INNER JOIN
leagueuser ON league.id = leagueuser.leagueid
WHERE        (leagueuser.userid = 4)))

(The formatting was autofixed by visual studio to look like this, but the query works)

The vb.net code to create this query in LINQ is actually two parts as follows:

Dim firstquery = From b In dc.leagueusers Where b.userid = currentUser Select b.leagueid
Dim leaguesnotjoined = From p In dc.leagueusers Where Not firstquery.Contains(p.leagueid) Select New With {p.league.id, p.league.name} Distinct

You can see that the second query uses the first query. Again, it’s not the most elegant solution, but it’s pretty straightforward and the only way I’ve successfully gotten it to work.  If you wanted to make it an “in” statement, you’d just have to remove the word “not” from the second query.
0