Text with different colored background - ios

How to achieve the below design in iOS , is any controls available to achieve this. I am not that much experienced to achieve this design in native . Is it developed in native or kind of hybrid technology is used
Please find the below image for reference

I made a programmatic Example (Kind of "Hello World") for you
(it is programatic for a reason, that I believe it is easier to understand how things are working.
It is not perfect solution it can be done better, (performance wise) but I choose it as a simple example to get you started (Personally I would use CALayer for the backgrounds).
so here:
make a new class as follows:
MyView.h
#import <UIKit/UIKit.h>
#interface MyView : UIView
#end
MyView.m
#import "MyView.h"
#implementation MyView
- (instancetype)initWithFrame:(CGRect)frame
{
if(self = [super initWithFrame:frame])
{
self.backgroundColor = [UIColor grayColor];
//This rect is in self coordinate system
UIView * viewPurple = [[UIView alloc]initWithFrame:CGRectMake(0, 200, frame.size.width, 50)];
viewPurple.backgroundColor = [UIColor purpleColor];
[self addSubview:viewPurple];
//This rect is in viewPurple's coordinate system
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width/2, 50)];
[label setText:#"Hello World!"];
[label setTextAlignment:(NSTextAlignmentCenter)];
[label setTextColor:[UIColor whiteColor]];
[viewPurple addSubview:label];
//All the frame's rects are in it's `superView`/ parent 's coordinate system.
UIView * viewGreen = [[UIView alloc]initWithFrame:CGRectMake(frame.size.width/2, 200, frame.size.width, 50)];
viewGreen.backgroundColor = [UIColor cyanColor];
[self addSubview:viewGreen];
UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width/2, 50)];
[label2 setText:#"World Hello!"];
[label2 setTextAlignment:(NSTextAlignmentCenter)];
[label2 setTextColor:[UIColor whiteColor]];
[viewGreen addSubview:label2];
}
else
{
//Can't allocate the UIView - quite a rare problem.
}
return self;
}
#end
#import "myView.h" in your view controller
and add this snippet to the - (void)viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MyView * view = [[MyView alloc]initWithFrame:self.view.frame];
[self.view addSubview:view];
}
the result will be as follow

Related

How to Call the Custom UIView from ViewController using Frame Size

I want to show the CustomUIView From my ViewController.How to call using frame?I am getting Confused in Frame as am a newbie.
My Theme is,I Want to show the LoginViewKarnataka and usernameLabel in my ViewController in the y Value 150.
This is My Code
ViewController.m
LoginViewKarnataka *loginView = [[LoginViewKarnataka alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
[self.view addSubview:loginView];
LoginViewKarnataka(CustomUIView)
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
NSLog(#"frame==>>%f",frame);
if (self)
{
UILabel *usernameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 20)];
[usernameLabel setText:#"username"];
[usernameLabel setTextColor:[UIColor blackColor]];
}
}
Change your viewController code to
LoginViewKarnataka *loginView = [[LoginViewKarnataka alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 150)];
[self.view addSubview:loginView];
In your LoginViewKarnataka view
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setBackgroundColor:[UIColor redColor]];
UILabel *usernameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 20)];
[usernameLabel setText:#"username"];
[usernameLabel setTextColor:[UIColor blackColor]];
[self addSubview:label];
}
return self;
}
In your above code, you are adding a label at the positions of x: 20, y: 20.
To print frame of any view use following code.
NSLog(#"frame : %#",NSStringFromCGRect(self.view.frame));
To print size of any view
NSLog(#"frame : %#",NSStringFromCGSize(self.view.frame.size));
Your code is fine. All that was missing was adding usernameLabel as a subview to your custom view.
[self addSubview:usernameLabel];
P.S. If you need to log any frame values then you can simply log the view. The frame value is printed in the description of the view. Also you can use DCIntrospect for UI debugging if you have created any complex UI.
Thanks.

How to create gmail recipient format in iOS?

I need to create a view like below from the array of emails that I receive which also has the delete option.
Something similar to that of gmail's mail recipient except in my this should be scrollview. My main issue is to create the background with delete button which stretches based on the email length. My current approach is to use 3 images one for the beginning, one for the end with delete button and one in general for middle that will stretch. Is there any other or better way to do this?
NOTE:Need to support from iOS 5 and above
Here you go, I have created a SuperLabel for you though it might need some tweak.. But it will help you for sure..
SuperLabel.h
#import <UIKit/UIKit.h>
#interface SuperLabel : UIView
- (id)initWithFrame:(CGRect)frame andTitle:(NSString *)title;
#end
SuperLabel.m
#import "SuperLabel.h"
#define MAX_HEIGHT 25.0
#implementation SuperLabel
- (id)initWithFrame:(CGRect)frame andTitle:(NSString *)title
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//Design your label view
self.backgroundColor=[UIColor colorWithRed:.8 green:.8 blue:.8 alpha:1.0];
self.layer.borderColor=[[UIColor orangeColor] CGColor];
self.layer.borderWidth=1.0;
self.layer.cornerRadius=5.0;
//Add a Label
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, 100.0, MAX_HEIGHT)];
label.font=[UIFont systemFontOfSize:12.0];
label.textColor=[UIColor grayColor];
label.text=title;
[label sizeToFit];
[self addSubview:label];
//We will get resized frame after using sizeToFit after setting the text in the label.
CGRect rect=label.frame;
//Add a button
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:#"x" forState:UIControlStateNormal];
btn.titleLabel.font=[UIFont systemFontOfSize:12.0];
btn.titleLabel.textColor=[UIColor grayColor];
rect.origin.x=rect.origin.x+rect.size.width;
rect.origin.y=0;
rect.size=CGSizeMake(25.0, MAX_HEIGHT);
[self addSubview:btn];
[btn addTarget:self action:#selector(deleteMe:) forControlEvents:UIControlEventTouchDragInside];
btn.frame=rect;
//Change the whole frame of the label view
frame.size.height=MAX_HEIGHT;
frame.size.width=btn.frame.origin.x+btn.frame.size.width;
self.frame=frame;
}
return self;
}
-(IBAction)deleteMe:(id)sender{
[self removeFromSuperview];
}
#end
And finally add to your view by using following code..
SuperLabel *label=[[SuperLabel alloc] initWithFrame:CGRectZero andTitle:#"myemail#gmail.com"];
[self.view addSubview:label];
Cheers..

addSubview is not displayed UIView

I described [self.view addSubView:myView];,
but, myView is not displaying self.view.
ViewController.h
#interface ViewController : UIViewController{
UIView *myView;
}
#property (nonatomic, strong) UIView *myView;
ViewController.m
#synthesize myView;
- (void)viewDidLoad
{
[super viewDidLoad];
myView.frame = CGRectMake(-10, 70, 320, 480);
[self.view addSubview:myView];
myView.backgroundColor = [UIColor redColor];
[self.view bringSubviewToFront:myView];
}
Do you have any solutions?
You need to allocate your view first:
- (void)viewDidLoad
{
[super viewDidLoad];
// If you use custom view change UIView to the custom class name
myView = [[UIView alloc] initWithFrame:CGRectMake(-10, 70, 320, 480)];
[self.view addSubview:myView];
myView.backgroundColor = [UIColor redColor];
[self.view bringSubviewToFront:myView];
}

UIView - UIScrollView odd behaviour

I have a UIViewController named MenuViewController in which i create an UIView MessageView and pass it a NSString:
MessageView *mess = [[MessageView alloc]initWithFrame:CGRectMake(0, 0, 1024, 768)];
[mess messageString:#"A:text\nM:text\nA:text\nM:text"];
[self.view addSubview:mess];
In MessageView i am creating an UIScrollView and several UILabels; my problem is the following and i can't understand the behaviour:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(540, 50, 360, 160)];
scrollView.backgroundColor = [UIColor redColor];
[self addSubview:scrollView];
}
return self;
}
- (void)messageString:(NSString *)string
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height/6)];
>>>>> HERE is the problem <<<<<<
if i try to add label to scrollView it does not appear
[scrollView addSubview:label];
if i try to add label to self it appears
[self addSubview:label];
}
I would like to create all of the content i would like to show here in MessageView and just call it from the view controller. Does anybody have an idea of what may be the problem ?
This line:
UIImageView *label = [UILabel alloc]initWithFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height/6)];
Should be producing an error and a warning. For one, you're missing a starting bracket [, and on top of that, you're creating a pointer to a UIImageView object, and then assigning a label to it. Try again with the line written out like this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height/6)];
This is of course assuming that the method is even being called, and the scroll view is non-nil and has a proper frame when you try to access it.
You are adding the label outside of scrollview's visible frame: Replace
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height/6)];
with
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,0, scrollView.frame.size.width, scrollView.frame.size.height/6)];

UIScrollView not showing iOS7

I'm try to add a scrollview totally by code to a modal view, but it doesn't show.
I've tried different ways but I can't see the mistake,
Somebody could help me?
here my code:
#interface AddWithScrollOrizontal ()<UIScrollViewDelegate>{
//IBOutlet UIScrollView*scroll;
}
#property (strong, nonatomic) UIScrollView*scroll;
#implementation AddWithScrollOrizontal
#synthesize scroll;
- (void)viewDidLoad
{
self.scroll = [[UIScrollView alloc]init];
self.scroll.delegate = self;
if (IS_IPHONE_5) {
self.scroll.frame = CGRectMake(0, 58, 320, 270);
}else{
self.scroll.frame = CGRectMake(0, 20, 320, 270);
}
self.scroll.backgroundColor = [UIColor redColor];
self.scroll.pagingEnabled = YES;
self.scroll.contentSize =CGSizeMake(1280, 270);
UIView*firstView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 270)];
...
UIView*fourthView = [[UIView alloc]initWithFrame:CGRectMake(960, 0, 320, 270)];
[self.scroll addSubview:firstView];
[self.scroll addSubview:secondView];
[self.scroll addSubview:thirdView];
[self.scroll addSubview:fourthView];
[self.view addSubview:self.scroll];
}
You say you're adding it totally by code, but you're making an IBOutlet to scroll. Did you create it in IB? If not, your problem is that you never instantiate the scroll view. Also, there's no need to create an ivar for scroll, just create the property inside the #interface declaration for your class extension, and refer to it with self.scroll.

Resources