Can't get both Numberoflines and textAlignment to work - xcode

When I first created the UIButton, the NSTextAlignment was always correct.
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,landscapes?450:380, 120)];
label.frame = CGRectMake(0, 0,landscapes?450:380, 120);
label.font = [UIFont fontWithName:#"Futura" size:60];
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.minimumScaleFactor = 0.2;
label.adjustsFontSizeToFitWidth = true;
label.adjustsLetterSpacingToFitWidth = true;
label.textAlignment = NSTextAlignmentCenter;
NSLog(#"%i",label.textAlignment);
label.text = n;
[self addSubview:label];
However, every time I put in a parameter for the number of lines
label.numberOfLines = 2;
The label is not text aligned.

Take a look at this other stackoverflow post about vertical alignment. I think using sizeToFit with numberOfLines=0 and then specifying the width will help you get it horizontally centered too - Vertically align text to top within a UILabel .

Related

UIImageView filling entire UICollectionView cell

I have a UICollectionView displaying a horizontal layout of images. Under each image, I'm displaying a label. In the storyboard, I've extended the height of the cell so that the label will be displayed underneath the cell. I've also set the height of the UIImageView in storyboard to be 20pts less than the cell. However, no matter what I do, the images take up the entire cell and the label is displayed on top of the image. Should I be setting the size of the imageview elsewhere? I found this thread which I thought would help since it's basically, identical, but the solution did not help me.
Here is my code...
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell"; // string value identifier for cell reuse
ImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.layer.borderWidth = 1.0;
cell.layer.borderColor = [UIColor grayColor].CGColor;
NSString *myPatternString = [self.imageNames objectAtIndex:indexPath.row];
cell.imageView.image = [self.imagesArray objectAtIndex:indexPath.row];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
CGSize labelSize = CGSizeMake(CellWidth, 20);
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.bounds.size.width/2, cell.bounds.size.height-labelSize.height, cell.bounds.size.width, labelSize.height)];
testLabel.text = myPatternString;
[cell.contentView addSubview:testLabel];
return cell;
}
Don't know if this will work for you, but I do this will a subclass of UIView which contains two subviews -- a UIImageView for the image and a UILabel for the label. Here is the essence of that subclass below (don't be bothered by the rounding of bottoms; in some areas I need the bottoms to be rounded, in others I don't). I just add this subview to the contentView of the cell. Don't know if it helps you but here it is. BTW, imageView is a class variable here but you might define it as a property.
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl roundBottoms:(BOOL)roundBottoms;
{
self = [super initWithFrame:frame];
if (self)
{
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height-25.0f)];
if (roundBottoms)
{
imageView.layer.cornerRadius = 12;
imageView.layer.masksToBounds = YES;
}
else
{
CAShapeLayer * maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: imageView.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii: (CGSize){12.0, 12.}].CGPath;
imageView.layer.mask = maskLayer;
}
[imageView setImage:img];
imageView.contentMode = UIViewContentModeScaleAspectFill; // default
[self addSubview:imageView];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 25.0f, frame.size.width, 25.0f)];
label.text = lbl;
labelText = lbl;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.50f;
label.backgroundColor = [UIColor clearColor];
self.tag = imageItem; // 101
[self addSubview:label];
}
return self;
}
I have another version of it I use where I calculate the height of the label based on the overall frame height that is passed in. In this one, the cell sizes are static so the label heights can be as well.

UITextView, setContentOffset and missing bottom part

For some strange reason, my UITextView's text appears cropped when the scrollofset is set.
Here's what it looks like:
This happens after doing:
textview.contentInset = UIEdgeInsetsZero;
[textview setContentOffset:CGPointMake(0, startypos + offset_yshift) animated:NO];
I tried manually setting contentSize.height, but that introduced another strange behavior, where the content offset seems to be ignored..
''Edit'': This is the code used to instantiate the textfield:
CGRect myImageRect = CGRectMake(-50.0f, -50.0f, 40.0f, 40.0f);
textview = [[UITextView alloc] initWithFrame: myImageRect];
textview.autocapitalizationType = UITextAutocapitalizationTypeNone;
[textview setScrollEnabled:YES];
textview.hidden = YES;
textview.contentInset = UIEdgeInsetsZero;
textview.opaque = NO;
textview.backgroundColor = [UIColor clearColor];
textview.textColor = [UIColor colorWithWhite:1 alpha:1];
textview.textAlignment = NSTextAlignmentCenter;
textview.frame = CGRectMake(0, 0, 250, 30);
textview.scrollEnabled = NO;
And the "update" code that checks the content positioning every frame:
// setting the actual size here
UITextPosition * pos = [textview positionFromPosition: textview.endOfDocument offset:nil];
CGRect therect = [textview caretRectForPosition:pos];
CGRect frame = textview.frame;
if([textview.text length] == 0){
frame.size.height = 30;
} else {
frame.size.height = therect.origin.y + therect.size.height;
}
// and here, we're changing the frame variable's height to max to 50
if(frame.size.height > 50){
frame.size.height = 50;
}
frame.size.width = desiredwidth; // some other variable
textview.frame = frame;
/*
... snip, unrelated code ...
*/
// later on
textview.contentInset = UIEdgeInsetsZero;
[textview setContentOffset:CGPointMake(0, startypos + offset_yshift) animated:NO];
As you can imagine, the setContentOffset bit there is what's causing the issue.
What is going on?
please try this from prev post
CGPoint offset = CGPointMake(0, self.textView.contentSize.height - self.textView.frame.size.height);
[self.textView setContentOffset: CGPointMake(0,0) animated:NO];
or
[self.textView setContentOffset:bottomOffset animated:YES];
[post]: UITextView contentOffset on iOS 7 "prev post"\
I ran into a very similar issue in iOS7 with UITextField - looks almost exactly the same. Mine had to do with content insets as well.
The solution (for me) - was to eliminate the UITextField from nib/storyboard and instantiate it and add it to the view purely from code. Behavior worked as expected afterwards.
I don't know what was up other than it probably being a iOS7 / XCode 5 bug... Curious to know if that solves it for you too.
My thougts would be, to adjust the size of the UITextView based on its content.
Like asked here: how-do-i-size-a-uitextview-to-its-content
Code taken from jhibberd ´s answer
- (void)textViewDidChange::(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
}

How can I crop my UIPickerView?

Here is my horizontal UIPickerView (by rotate & scale the ori UIpicker) : htp://s7.postimage.org/qzez9d0pn/Original.png
And now, what I want to do is cut off these two spaces : http://s7.postimage.org/bryzp08uz/Process.png
To have this better look result : http://s7.postimage.org/6ulf3w6vv/Result.png
Somebody please help me how to do it...?
my rotate & scale code :
thePickerView.delegate = self;
thePickerView.showsSelectionIndicator =YES;
thePickerView.center = CGPointMake(xVar, yVar); // position
CGAffineTransform rotate = CGAffineTransformMakeRotation(-3.14/2); // rotate
rotate = CGAffineTransformScale(rotate, 0.1, 1); // scale
[thePickerView setTransform:rotate];
UILabel *theview[num];
CGAffineTransform rotateItem = CGAffineTransformMakeRotation(3.14/2);
for (int i=0;i<num;i++) {
theview[i] = [[UILabel alloc] init];
theview[i].text = [NSString stringWithFormat:#"%d",i+1];
theview[i].textColor = [UIColor blackColor];
theview[i].frame = CGRectMake(0,0, 100, 100);
theview[i].backgroundColor = [UIColor clearColor];
theview[i].textAlignment = UITextAlignmentCenter;
theview[i].shadowColor = [UIColor whiteColor];
theview[i].shadowOffset = CGSizeMake(-1,-1);
theview[i].adjustsFontSizeToFitWidth = YES;
theview[i].transform = CGAffineTransformScale(rotateItem, 1, 10);
}
itemArray = [[NSMutableArray alloc] init];
for (int j=0;j<num;j++) {[itemArray addObject:theview[j]];}
[thePickerView selectRow:row inComponent:0 animated:NO];
[self.view addSubview:thePickerView];
My temporary solution while searching & waiting for better coding answer is create an overlay background image then transparent the desire space of PickerView =.=

Image size resets to original after performing pich using UIPichGestureRecognizer in iphone

I have one view controller in which I have added UIScrollView & UIImageView programatically. I have added UIPichGestureRecognizer to the UIImageView. My UIImageView is added to UIScrollView as a subview.
My problem is when I try to pinch the image , it zoom in. But when I release the touches from screen it again come to its default size. I can not find the error in code. Please help me.
Below is my code
- (void)createUserInterface {
scrollViewForImage = [[UIScrollView alloc]initWithFrame:CGRectMake(20.0f, 60.0f, 280.0f, 200.0f)];
scrollViewForImage.userInteractionEnabled = YES;
scrollViewForImage.multipleTouchEnabled = YES;
scrollViewForImage.backgroundColor = [UIColor redColor];
scrollViewForImage.autoresizesSubviews = YES;
scrollViewForImage.maximumZoomScale = 1;
scrollViewForImage.minimumZoomScale = .50;
scrollViewForImage.clipsToBounds = YES;
scrollViewForImage.delegate = self;
scrollViewForImage.bouncesZoom = YES;
scrollViewForImage.contentMode = UIViewContentModeScaleToFill;
[self.contentView addSubview:scrollViewForImage];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 280.0f, 200.0f)];
[imageView setBackgroundColor:[UIColor clearColor]];
imageView.userInteractionEnabled = YES;
imageView.multipleTouchEnabled = YES;
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinch:)];
[pinchRecognizer setDelegate:self];
[imageView addGestureRecognizer:pinchRecognizer];
//[self.contentView addSubview:imageView];
[self.scrollViewForImage addSubview:imageView];
scrollViewForImage.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);
}
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)inScroll {
return imageView;
}
-(void)pinch:(id)sender {
[self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
From what I can see the problem lies here:
scrollViewForImage.maximumZoomScale = 1;
You are setting the maximum zoom scale of the image to 1 x its full size. This means once you finish pinching the image, it will scale back to 1 x its size.
If you want to be able to zoom the image to a larger size, try settings this value to be higher than 1. e.g.
scrollViewForImage.maximumZoomScale = 3;

UITextField doesn't work

In a TabBar-Application I have on the view a small one, in which I can draw. Works perfect.
With that code:
UITextField * textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(10, 45.0, 260.0, 50)];
textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
textFieldRounded.textColor = [UIColor blackColor]; //text color
textFieldRounded.font = [UIFont systemFontOfSize:17.0]; //font size
textFieldRounded.placeholder = #"<enter text>"; //place holder
textFieldRounded.backgroundColor = [UIColor whiteColor]; //background color
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textFieldRounded.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
textFieldRounded.returnKeyType = UIReturnKeyDone; // type of the return key
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldRounded.userInteractionEnabled = YES;
[skizzenFeldOutlet addSubview:textFieldRounded];
textFieldRounded.delegate = self;
I create a textfield. It works fine, but I can't put text on the textfield. The textfield is visible but I can draw in the view under the textfield!
Any tips?
Best regards
Andreas
because of this.
[skizzenFeldOutlet addSubview:textFieldRounded];
your textField is been flat added in your view.

Resources