UILabel with horizontal padding? - objective-c

I'm new to ios development and i'm struggling to make something that should be trivial.
I want simple container with background color and single line text inside with padding and i prefer to make it programmatically (no IB).
What i've already tried:
simple UILabel vertical padding is not a problem with fixed height but no horizontal;
also UIView with UILabel as subview - adding background color to UIView and the label is just text sizeToFit;
When i am using UIView with UILabel as subview i've set:
[self.myTestView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
but because i'am using NSLayoutConstraint when i set self.myTestView.translatesAutoresizingMaskIntoConstraints = NO; i don't have proper autoresizing.
If it helps i'am trying this into UICollectionViewCell?
Just for additional information i know this can be achieved with UIButton and using UIEdgeInsets, but i don't want to use UIButton for simple text container.
Does anybody have an idea or direction?
Thanks in advance!

Why you just didn't add constraint to your label inside uiview in IB, with padding what you need?!
http://i.stack.imgur.com/8qTGt.png

Related

How do I programmatically change the size a UIImageView that was created on storyboard with autolayout?

I have an outlet to a UIImageView hooked up from my storyboard to my header file. I am trying to alter the frame of the UIImageView programmatically from my implementation file. I have done the following:
-(void)viewDidLoad{
[super viewDidLoad];
if (IS_IPHONE_5) {
someImageView.frame = CGRectMake(someImageView.frame.origin.x, someImageView.frame.origin.y, someImageView.frame.size.width - 50, someImageView.frame.size.height - 50);
}
}
No matter what I change my width and height to it doesn't seem to change. Could it be that I am using autolayout and have constraints hooked up to the imageView? If so, how do I override these to change the image view's height and width?
Is there a simpler way to do this just from the storyboard?
Thanks in advance.
You might want to reevaluate the constraints you set up in IB. Make sure you don't have width or height constraints, but instead top/bottom/leading/trailing constraints. Then you probably don't have to do anything programmatically. But if you must resize programmatically, create IBOutlet references to the constraints in the storyboard, and then you can adjust the constant property of the constraint. But don't attempt to change the frame if you're using constraints.

When NSTableview Load second time its customcell Subview's change it's position how to solve

I am new for cocoa OSX application might be this question is simple but i try my best to find out this issue and at the end i asking question here.
I am creating NSWindowViewController and in side it i Used NSTableview with Customcell. In customeCell i used NSView (customView) and all Cell IBOutlet put in side NSView. When First time that NSWindowViewController load that show fine but after close the window and again i open it. its NSTextField IBOutlet change its position top to bottom of the cell.
I try to find this some property change but did not fix it i attach its screen example what was happen with my cocoa osx Application.
I notice that this happen when i used following code for setting NSview's background color from property Inspector i setting NSView Core animation like.
And set background color in viewForTableColumn like:
cellView.bgview.layer.backgroundColor = [NSColor whiteColor].CGColor;
Color set properly and all working fine when i reload table view that top label going to bottom and that will display revers UI order for cell as i show in my question.
And i tested with if i remove NSView setting background color code as i mention above then working perfect no re-order and all things. I am not using Auto-layout.
So after then i create custom subclass of NSview for setting color like following:
- (void)drawRect:(NSRect)dirtyRect {
[[NSColor whiteColor] setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
// Drawing code here.
}
I remove my old code and use this one for setting background by creating NSView subclass and my issue fix.

Vertical UISlider in iOS with autolayout

As per my iPad app requirement, i've to show the UISlider vertically.
I'm using iOS7 compiler and deployment target is iOS6.
In the story board I added horizontal UISlider of width 600 pixels. I created IBOutlet in my view controller. I didn't set any auto layout constraints. I'm using the below code to rotate and make it vertical.
self.slider.transform = CGAffineTransformMakeRotation(M_PI_2);
After and before rotation I'm printing the frame size of the slider which is correct. But the slider is not looking proper. Its just showing only knob in the center. How can I rotate the UISlider?
I got a vertical slider working with iOS 8 and Xcode 6 with only 3 constraints in the storyboard and one line of code. Here's a cropped screencap of the interface:
There are 3 constraints between the vertical slider and the UIImageView next to it:
vSlider.Center Y = Image View.Center Y
vSlider.Width = Image View.Height
vSlider.Center X = Image View.Trailing + 16
And of course the one line of code is:
self.vSlider.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
It's easy to set up these constraints in the storyboard in Xcode 6, but I think it should be simple to write these constraints in code to support iOS 7 or 6.
I got it to work this way:
In viewDidLoad: I added
[self.slider removeConstraints:self.slider.constraints];
[self.slider setTranslatesAutoresizingMaskIntoConstraints:YES];
so that it's called before rotating the slider with
self.slider.transform=CGAffineTransformRotate(self.slider.transform,270.0/180*M_PI);
and there is no need to remove and re-add it to superview.
This is an old topic, but here is a Swift solution with autolayout constraints in storyboard and nothing else.
1/ You need to add rotation to the IBOutlet:
#IBOutlet weak var mySlider: UISlider! {
didSet {
mySlider.transform = CGAffineTransform(rotationAngle: -CGFloat.pi/2)
} // didSet
} // IBOutlet
2/ Define in storyboard the constraints, keeping in mind that the Slider will be rotated around its center.
For instance if you want to locate mySlider on the left side of myView, you need three constraints.
myView.Leading = mySlider.CenterX - 20
mySlider.width = myView.Height (with a multiplier of 0.8 for instance)
mySlider.CenterY = myView.CenterY
mySlider will of course appear horizontal in storyboard, but will have the correct sizing, and the center will be correctly positioned.
Uncheck Auto-Layout on your ViewController, there is no other option under the SDK 7.0 to make it work vertically :(
There are so many possible solutions around about putting UISlider vertical. Here is my summary for iOS7 in XCode5 with autoLayout enabled(defaultly in storyboard):
in viewDidLoad add method
self.slider.transform = CGAffineTransformMakeRotation(M_PI_2);
define your autoLayout constraints about slider explicitly in storyboard as whatever you like
In your viewDidLoad, try:
UIView *superView = self.sizeSlider.superview;
[self.sizeSlider removeFromSuperview];
[self.sizeSlider removeConstraints:self.view.constraints];
self.sizeSlider.translatesAutoresizingMaskIntoConstraints = YES;
self.sizeSlider.transform = CGAffineTransformMakeRotation(M_PI_2);
[superView addSubview:self.sizeSlider];
It does not work with constraints, so the trick is to remove the constraints for your uislider.
You might have to resize it manually by setting its frame property.
You can't use storyboard to build up a UISlider.
Build up UISlider by coding.
slider = [[UISlider alloc] initWithFrame:CGRectMake(640, 150, 600, 400)];
[slider.layer setAnchorPoint:CGPointMake(0.0f, 0.0f)];
slider.transform = CGAffineTransformMakeRotation(M_PI/2);
[self.view addSubview:slider];
Try this :-
self.slider.transform=CGAffineTransformRotate(slideToUnlock.transform,-90.0/180*M_PI);
Try below code to Rotate the UISlider in Vertical Position..
//To rotate the slider in Vertical Position
CGAffineTransform sliderRotation = CGAffineTransformIdentity;
sliderRotation = CGAffineTransformRotate(sliderRotation, -(M_PI / 2));
sliderBrightness.transform=sliderRotation;
For me a two-step process worked best (incorporating some of the previous solutions)
Autolayout step)
I added a vertical view in IB and used autolayout to link it to neighboring views. Then I added a slider in the view and simply hooked it up to the center of the view. Then hooked up the width of the slider to the height of the view. Finally control-dragged the slider outlet to my ViewController code (as slider)
Code step)
Then simply added the to my viewWillAppear (swift-code):
let trans = CGAffineTransformMakeRotation(CGFloat(M_PI_2));
slider.transform = trans;

UILabel gets hidden behind UIImageView

I have made a subclass for UITableViewCell and I am implementing Subtitle TableViewCell with a thumbnail image.
Following are the UITableViewCell contents:
The issue I am facing is when the data loads in TableViewCell, the subtitleLabel text gets hidden upto the height of the imageView. But when I select any Cell, it shows subtitleLabelText completely.
I have added the screenshot of the same for complete reference:
The UIImageView has frame = CGRectMake(0,0,40,40);
Try to give a clearColor background color for the cell title label -
cell.textLabel.backgroundColor = [UIColor clearColor];
It turns out I was using TableViewCell style as subtitle instead of custom. The style settings in subtitle was making the other labels to hide below them. What a silly miss!
In your nib or storyboard file, make sure that the label is below the image view in the list of subview components (it is in the left of the screen). The first subview in that list will be at the lowest level (behind every other subview, if they overlap).
write one line of code
Titlelabel.backgroundcolor = [UIColor ClearColor];
because your label has white background..and Titlelabel height is too large so label is colliding.
Let me know working or not!!!
Happy Coding!!!
What is the frame of Title Label? if its height is more, then also it may possible that it hides your subtitle Label
Here's a great tutorial which helped me when I was trying to do something like you want :
http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/
You can adapt the size of the different components (ImageView, TitleLabel, Subtitle,...)

How to draw graphics on a UIButton UIButtonTypeRoundedRect?

How to draw graphics on a UIButton UIButtonTypeRoundedRect without subclassing... or do I have no choice? Please note that the graphics around the rounded corners will vary with other functions... so the UIButton cannot interfere... otherwise I could have just used jpg images.
Or can I limit the border of the jpg image around the UIButton?
thx
If I got your question correctly, you need to create an UIButton with an image inside (png or jpg) with rounded corners.
It could be easier if you subclass UIButton (so you can reuse it), but if you don't want to do it, just suppose we have
UIButton *myButton;
pointing to your UIButton with images and other properties correctly setted.
First, don't forget to
#import <QuartzCore/QuartzCore.h>
and then simply set:
myButton.layer.cornerRadius = 5.0f; //or any CGFloat values you need
If it seems not working, check the UIButton "Clip Subviews" property or use:
myButton.clipsToBounds = YES;