Skip to main content

Posts

Create custom UITextFields and UITextViews with underline

Download In a recent iOS project I needed to create custom UITextView and UITextField. The look and feel had to be same as the Notes app, i.e. the text view and text field both should have horizontal lines as are present in a note. How to create a custom UIView To create a custom UIView we need to inherit from UIView class. We then need to override drawRect method. Override only if you need to change the way view is drawn as this is a performance intensive operation. Custom UITextField In order to create custom UITextField I inherited my class from UITextField. I need to draw a bottom border for this I need override drawRect method. //Get the current drawing context CGContextRef context = UIGraphicsGetCurrentContext(); //Set the line color and width CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); CGContextSetLineWidth(context, 0.5f); //Start a new Path CGContextBeginPath(context); // offset lines up -...

UITableView basic tutorial

In this tutorial we will create a table view with some dummy data, we will use disclosure indicator, change background color of cell, handle user selection, change separator color etc. There many tutorials which provides in depth information on UITableViews thus I will not touched that and instead targeted the practical How Tos. Create a new single view project and add a UITableView to the initial view controller which has been added to the storyboard automatically. In order to use UITableView in your viewcontroller you need to adapt to UITableViewDataSource and UITableViewDelegate.  UITableViewDelegate provides us with methods which let us handle user selection events, define row height, modifying view and much more. UITableViewDataSource provides data to UITableView to construct the view (to render content). Create table view cells, define the number of sections, number of rows in sections and much more. Adapt to required protocols @i...

Send Mail .NET

Allowing user to send mail (feedback, contact etc) is a very common feature in current web applications. In this series of post we will talk about some basic mail features and how to implement them in c#. Basics In order to send mail we need SMTP server configured as mails work over SMTP. You can configure SMTP server yourself or if you are using web hosting services you can get an account setup for you. In order to receive mails we need POP. For this tutorial series we will use gmail SMTP server.  .NET APIs .Net provides us with lots of classes present in System.Net.Mail to send an email. We will be using MailMessage, MailAddress and SmtpClient classes. We will create a MailHelper and will use it in Winforms, Asp.Net, Console etc. Mail Helper using System.Collections.Generic; using System.Net.Mail; namespace MailConsole { public class MailHelper { public static void SendMail(string subject,string message,List to) { var m...

Knowing Singelton

Singelton pattern in one of the most commonly used and talked about design pattern . Many appreciates it usefulness and many advocate against it. In this post we are not going to discuss all that but will concentrate on its implementation in c#. Type: It is a creational design pattern . Intent: It ensures that only one instance of the class exists. Usage: It is generally used when only one instance of a class is required. Thus, it can be seen to be used in logging frameworks, configuration objects etc. The ownership of instance creation lies with the class itself. This is because we can not ensure that only one instance will ever exists for the class if the responsibility lies with someone else. The class will instantiate the object when it will be used for the first time. This also ensures lazy initialization. Point to consider: Singelton object is mostly made accessible globally and thus being abused by being used as global variable. We must keep in mind that not onl...

C# Polymorphism - handle with care

Download Overriding is the base for most of the design patterns which exists. It provides us with an essential tool called  Polymorphism  . What is  Polymorphism  ? Polymorphism means one interface and many forms. It is a characteristics of being able to assign a different meaning or usage to something in different contexts specifically to allow an entity such as a variable, a function or an object to have more than one form.  There are two types of Polymorphism.  Compile time:  function or operator overloading  Runtime:  Inheritence & virtual functions Here, we are going to talk about the Runtime Polymorphism.  If you read the definition carefully you will see that there is going to be some type casting and compiler is going to made some assumptions. These assumptions may fail on execution. Let me explain this further. Type casting: There will be a parent interface (Interface, abstract class, class) and may be ...

c# reference types passed by value or reference

Download Time and again I have heard that in C#, method arguments are passed based upon there type. i.e. Value types are passed by value and reference types are passed by reference. This is so untrue. Reference types have nothing to do with pass by value. Let us talk some basic. Consider the assignment first. When you assign a value type to another its value is copied. This is because value type variable contains its data directly. When you assign a reference type variable to another only the reference gets copied. This is because a reference type variable does not contains its data directly. It only holds reference to data. Thus, when we pass a value type, its value gets copied and when we pass a reference type its reference gets copied. That is why if we change the value of a value type variable in the block (method), the change is not seen outside of it. On the other hand if we change the value of member of reference passed to the block from within the block the cha...

Callback in c#

Download Running tasks in background is the need of the hour when working on real world applications. There are few tasks which we can fire and forget but for few of them we will like to receive the feedback. This is when callbacks comes into the picture and provides a channel for these objects to communicate with each other. These tasks are very common in desktop and mobile applications. In this post we will talk about callback mechanism in c#. In future post we will build upon this and do lot more. Using interfaces for callbacks in c# using System; using System.Threading; namespace Callbacks { class Program { static void Main(string[] args) { var consumer = new Consumer(); consumer.DoWork(); Console.ReadLine(); } } interface IOnProcessCompleteListener { void OnProcessComplete(string message); } internal class Consumer: IOnProcessCompleteListener { private Work...