How to use CKStackLayoutComponent to align two labels horizontally? - componentkit

I've just started playing with ComponentKit and I'm having some difficulties aligning 2 labels horizontally.
I want the first one close to the left margin and the second close to the right.
With auto layout, I can do it with this set of constraints:
H:|-0-[_label1]-[_label2]-0-|
Everything I tried doesn't seem to be working, I always get the same result: both labels left-aligned.
This is the super-simple component:
+ (instancetype)newWithText1:(NSString *)text1 text2:(NSString *)text2
{
return [super
newWithComponent:
[CKStackLayoutComponent
newWithView:{}
size:{}
style:{
.direction = CKStackLayoutDirectionHorizontal,
.alignItems = CKStackLayoutAlignItemsCenter
}
children:{
{
[CKLabelComponent
newWithLabelAttributes:{
.string = text1,
.font = [UIFont systemFontOfSize:20],
.color = [UIColor blackColor]
}
viewAttributes:{
{#selector(setBackgroundColor:), [UIColor clearColor]}
}],
.alignSelf = CKStackLayoutAlignSelfStretch
},
{
[CKLabelComponent
newWithLabelAttributes:{
.string = text2,
.font = [UIFont systemFontOfSize:20],
.color = [UIColor redColor],
.alignment = NSTextAlignmentRight
}
viewAttributes:{
{#selector(setBackgroundColor:), [UIColor clearColor]}
}],
.alignSelf = CKStackLayoutAlignSelfStretch
}
}]];
}
If anyone has any advice that would be greatly appreciated.

Great find—this is a shortcoming in our flexbox implementation (CKStackLayoutComponent). You're looking for the equivalent of justify-content: space-between; but we don't support it yet.
If you'd like to submit a patch to the stack layout implementation to support this, it'd be much appreciated. Otherwise, I'll try to find someone to add support.
For now, you can fake it by putting in a spacer with flexGrow = YES:
+ (instancetype)newWithText1:(NSString *)text1 text2:(NSString *)text2
{
return [super
newWithComponent:
[CKStackLayoutComponent
newWithView:{}
size:{}
style:{
.direction = CKStackLayoutDirectionHorizontal,
.alignItems = CKStackLayoutAlignItemsCenter
}
children:{
{
[CKLabelComponent
newWithLabelAttributes:{
.string = text1,
.font = [UIFont systemFontOfSize:20],
.color = [UIColor blackColor]
}
viewAttributes:{
{#selector(setBackgroundColor:), [UIColor clearColor]}
}],
.alignSelf = CKStackLayoutAlignSelfStretch
},
{
[CKComponent new],
.flexGrow = YES,
},
{
[CKLabelComponent
newWithLabelAttributes:{
.string = text2,
.font = [UIFont systemFontOfSize:20],
.color = [UIColor redColor],
.alignment = NSTextAlignmentRight
}
viewAttributes:{
{#selector(setBackgroundColor:), [UIColor clearColor]}
}],
.alignSelf = CKStackLayoutAlignSelfStretch
}
}]];
}

Related

ios - set / unset button background color

Using the method:
- (IBAction)buttonClicked:(id)sender {
mybutton.backgroundColor = [UIColor blackColor];
}
I can set the black color to mybutton. How can I toggle between two color, each time button is clicked?
Tx in advance
In your h file initialize bool property
#property (nonatomic, assign) BOOL myBool;
In your m file put following line in viewDidLoad
self.myBool = YES;
Then in your IBAction,
- (IBAction)buttonClicked:(id)sender
{
if (self.myBool)
{
self.button.backgroundColor = [UIColor blackColor];
self.myBool = NO;
}
else
{
self.button.backgroundColor = [UIColor blackColor];
self.myBool = YES;
}
}

SKLabelHorizontalAlignmentMode

Can someone give me an example of how to use SKLabelHorizontalAlignmentMode?
Here's how I'm defining my label:
RunningLevelLabel = [SKLabelNode labelNodeWithFontNamed:#"Chalkduster" ];
RunningLevelLabel.text = [NSString stringWithFormat:#"%i",numberOfBonusAlienPoints];
RunningLevelLabel.fontSize = 36;
RunningLevelLabel.position = CGPointMake(-10,-50); // offscreen
RunningLevelLabel.fontColor = [SKColor grayColor];
[StartScreenWindow addChild:RunningLevelLabel];
thanks,
rich
First create a label:
SKLabelNode *scoreLabel = [SKLabelNode labelNodeWithFontNamed:#"Arial"];
scoreLabel.text = #"00000";
scoreLabel.fontSize = 18;
scoreLabel.fontColor = [UIColor blackColor];
scoreLabel.position = CGPointMake(200, 300);
[self addChild: scoreLabel];
Now, you can align the label with:
scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
OR
scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeRight;
OR
scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
This outputs are shown in figure below:
Keep coding............. :)
Hasn't been answered yet and I was just looking for this myself...
See the 2nd line...vertical alignment works the same way.
SKLabelNode *RunningLevelLabel = [SKLabelNode labelNodeWithFontNamed:#"Chalkduster" ];
[RunningLevelLabel setHorizontalAlignmentMode:SKLabelHorizontalAlignmentModeCenter];
RunningLevelLabel.text = [NSString stringWithFormat:#"%i",numberOfBonusAlienPoints];
RunningLevelLabel.fontSize = 36;
RunningLevelLabel.position = CGPointMake(-10,-50); // offscreen
RunningLevelLabel.fontColor = [SKColor grayColor];
[StartScreenWindow addChild:RunningLevelLabel];

iOS 7 Views reposition under status bar using topLayoutGuide

I have seen many confusing and heck solutions to deal with status bar in iOS 7 but I liked following solution as mentioned by Apple at - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926
....but it doesn't work! This method gets called as expected but childView is not moving down 20 pixels.
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
UIView *childView = self.childController.view;
id topGuide = self.topLayoutGuide;
CGFloat topBarOffset = self.topLayoutGuide.length; //I checked this valued is 20
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (childView, topGuide);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: #"V:[topGuide]-20-[childView]"
options: 0
metrics: nil
views: viewsDictionary
]
];
}
}
Following works for me but I would really like to solve this problem using AutoLayout using topLayoutGuide.
- (void)layoutSubviews
{
[super layoutSubviews];
self.backgroundColor = [UIColor blackColor];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
CGRect frame = self.bounds;
frame.origin.y = 20;
frame.size.height -= 20;
self.childView.frame = frame;
} else {
self.childView.frame = self.bounds;
}
}
Any ideas why using topLayoutGuide not working? Thanks.

start at the top of the page when changing page

ı have a problem about scrollview.
my project about a newspaper.my problem about : I want to start at the
top of the page when changing page.
scrollview have 8 news.ı want to beginning at the top of the text on
the page when change 2. or 3. or.. news..
please help me about it.because ı need to deliver the project on Monday.for this reason ı
must do today or tomorrow.
code of project in here
myScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
myScrollview.pagingEnabled = YES;
myScrollview.scrollEnabled =YES;
myScrollview.clipsToBounds = NO;
myScrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;
myScrollview.showsHorizontalScrollIndicator = YES;
myScrollview.backgroundColor = [UIColor blackColor];
myScrollview.delegate = self;
NSInteger viewcount=4;
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:#"photo1.png"],[UIImage imageNamed:#"photo2.png"],[UIImage imageNamed:#"photo3.png"],[UIImage imageNamed:#"photo4.png"],nil];
for (int i = 0; i <viewcount; i++)
{
CGFloat x = i * self.view.frame.size.width;
subView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];
[subView setBackgroundColor:[UIColor blackColor]];
[subView setCanCancelContentTouches:NO];
subView.clipsToBounds = NO; // default is NO, we want to restrict drawing within our scrollview
subView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
aImageView = [[UIImageView alloc ] initWithImage:[images objectAtIndex:i]];
aImageView.frame=CGRectMake(0, 0, 320, 900);
[aImageView setTag:viewcount];
[subView addSubview:aImageView];
[subView setContentSize:CGSizeMake(aImageView.frame.size.width, subView.frame.size.height)];
subView.minimumZoomScale = 1;
subView.maximumZoomScale = 9;
subView.delegate = self;
[subView setScrollEnabled:YES];
subView.contentSize = aImageView.frame.size;
[myScrollview addSubview:subView];
}
myScrollview.contentSize = CGSizeMake(self.view.frame.size.width*viewcount,self.view.frame.size.height);
[self.view addSubview:myScrollview];
https://biliyomusunuz.com

for-loop in uitableview

I am developing an application in which i am displaying data from back-end.
Now the problem is as i am filling my table with for loop, and every cell of loop is filling data by every call. suppose there are 9 entries in the database, i want them to display as per row but all the 9 rows are filling by 9 entries by overlapping each other.
As i m new to iphone development, Please help me.
this is my piece of code m trying but not succeeded.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if(requestType == 2)
{
if (self.uniqueCityCount > 0)
{
NSString *strCity = [self.arrayCityNames objectAtIndex:section]; //#"EventData" :#"EventCity"
NSPredicate *predicateSettings = [NSPredicate predicateWithFormat:#"(EventCity = %# )",strCity];
if ([self.arrayEventDescription count]>0)
{
[self.arrayEventDescription removeAllObjects];
}
self.arrayEventDescription = [CoreDataAPIMethods searchObjectsInContext:#"EventData" :predicateSettings :#"EventCity" :YES :self.managedObjectContext];
return [self.arrayEventDescription count];
}
}
return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImageView *imgEventLabel = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 480, 22)];
imgEventLabel.image = [UIImage imageNamed:#"city_date_place.png"];
UILabel *lblCity = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 200, 22)];
lblCity.font = [UIFont systemFontOfSize:14];
lblCity.backgroundColor = [UIColor clearColor];
lblCity.tag = 301;
//lblCity.backgroundColor = [UIColor redColor];
UILabel *lblDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 200, 22)];
lblDate.font = [UIFont systemFontOfSize:14];
lblDate.backgroundColor = [UIColor clearColor];
lblDate.tag = 302;
//lblDate.backgroundColor = [UIColor redColor];
UILabel *lblSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 00, 400, 22)];
lblSchool.font = [UIFont systemFontOfSize:14];
lblSchool.backgroundColor = [UIColor clearColor];
lblSchool.tag = 303;
//lblSchool.backgroundColor = [UIColor redColor];
[imgEventLabel addSubview:lblCity];
[imgEventLabel addSubview:lblDate];
[imgEventLabel addSubview:lblSchool];
return imgEventLabel;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 22;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [#"" stringByAppendingFormat:#"Cell%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
if(requestType == 2)
{
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
for (int j = 0 ; j < [self.arrayEventDescription count]; j++)
{
NSLog(#"%d",indexPath.row);
//EventData *data = [self.arrayEventDescription objectAtIndex:indexPath.row];
EventData *data = [self.arrayEventDescription objectAtIndex:j];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor clearColor];
// UIView *viewDescription = [[UIView alloc]initWithFrame:CGRectMake(00, 00, 480, 35)];
////////////////////// Labels for description of city events from database ////////////////////////////
UILabel *lblEvent = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 150, 30)];
lblEvent.font = [UIFont systemFontOfSize:12];
lblEvent.backgroundColor = [UIColor clearColor];
UILabel *lblEventAtDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 150, 30)];
lblEventAtDate.font = [UIFont systemFontOfSize:12];
lblEventAtDate.backgroundColor = [UIColor clearColor];
UILabel *lblEventAtSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 15, 150, 30)];
lblEventAtSchool.font = [UIFont systemFontOfSize:12];
lblEventAtSchool.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lblEvent];
[cell.contentView addSubview:lblEventAtDate];
[cell.contentView addSubview:lblEventAtSchool];
lblEvent.text = data.EventName;
//lblEventAtDate.text = data.EventDate;
lblEventAtSchool.text = data.EventPlace;
}
}
}
// Configure the cell...
return cell;
}
Don't loop through your array. Just get the element for the row that is being asked for (which is indexPath.row.)
cellForRowAtIndexPath: will be called as many times as you specify in numberOfRowsInSection:.
Just make one cell per call.

Resources