How can I get the text to center in my scrollable UILabel? - ios

The scrollable label works fine, but for some reason I cant get the text in the UILabel to center in the UIScrollView, it always stays left aligned. Here is an image of what I am talking about, and my code. (P.S. the UILabel is a subview of the UIScrollView, and the UIScrollView is a subview of the UITableViewCell.
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(cell.frame.size.width/21.5, 25, cell.frame.size.width - 40 , 20)];
scrollView.showsHorizontalScrollIndicator = NO;
[cell addSubview:scrollView];
scrollLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, 20)];
scrollLabel.text = secondaryLabel.text;
scrollLabel.font = secondaryLabel.font;
scrollLabel.backgroundColor = [UIColor clearColor];
scrollLabel.textColor = [UIColor whiteColor];
scrollLabel.numberOfLines = 1;
scrollLabel.textAlignment = NSTextAlignmentCenter;
[scrollLabel sizeToFit];
[scrollView addSubview:scrollLabel];
scrollView.contentSize = CGSizeMake(scrollLabel.frame.size.width, 20);

I think the code you written for UILabel is pretty much right and the center TextAlignment is there but the problem is you need to pass the right X & Y for UILabel Frame's. For now they are getting set to 0-0 and are adding on the Scrollview's starting frame. This might can solve your problem.

Related

UIImageView size changes

I have navigation bar with custom view on it. UIImageView and UILabel are subviews of titleView. And I add UITapGestureRecognizer to titleView to make it show another UIViewController when the user taps on it. When you tap on titleView other UIViewController opens. But when you click on back button in my titleView size of UIImageView changes. Here is the code
UIView *titleView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 30)];
UILabel *title = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width/2, 30)];
title.text = _groupName;
[title setTextColor:[self colorFromHexString:#"#474747"]];
[title setFont:[UIFont fontWithName:#"Roboto-Bold" size:16]];
[title setTextAlignment:NSTextAlignmentCenter];
[title setBackgroundColor:[UIColor clearColor]];
_imageView.frame = CGRectMake(0, 0, 30, 30);
_imageView.layer.cornerRadius = 15.0;
_imageView.layer.masksToBounds = YES;
_imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
_imageView.layer.borderWidth = 0.1;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
title.frame = CGRectMake(title.frame.origin.x+20, title.frame.origin.y, self.view.frame.size.width/2, 30);
UITapGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(titleTapped:)];
titleView.userInteractionEnabled = YES;
[titleView addGestureRecognizer:recognizer];
[titleView addSubview:title];
[titleView setBackgroundColor:[UIColor clearColor]];
[titleView addSubview:_imageView];
self.navigationItem.titleView = titleView;
Here are the images where you can see the changes
I check your code Add clips to bounds and do not need to title frame two times.
_imageView.frame = CGRectMake(0, 0, 30, 30);
_imageView.layer.cornerRadius = 15.0;
_imageView.layer.masksToBounds = YES;
_imageView.clipsToBounds = true;
Rather than setting the frame of the views directly, you might try using Auto Layout; I suspect what's happening is that your custom view is getting re-layed-out after the back button, and it's reverting to the image view's instrinsicSize.
What you could do instead is turn off translatesAutoresizingMaskIntoConstraints on the subviews, and then add constraints to position them, and finally add width and height constraints to the image view.
Using Auto Layout is also recommended if you ever intend to internationalize your app -- if you set up your constraints correctly, then the translated title will fit and you won't have to ever play around with hardcoded values.

Adding a UILabel as a subview on top of a UITextView

I am trying to make a fake placeholder for a UITextView with a UILabel. Let's say there's a UITextView inside ViewController and this is what I do:
CGFloat frameWidth = self.postInputField.frame.size.width;
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
textViewPlaceholder.text = POST_PLACEHOLDER;
textViewPlaceholder.textColor = [UIColor lightGrayColor];
[self.view addSubview:textViewPlaceholder];
[super viewWillAppear:animated];
}
And there's an unnecessary margin on top of the UILabel as shown below:
I could set the y value to be negative something but I want to make sure why this is happening. One thing that I do when the view controller is loaded is self.automaticallyAdjustsScrollViewInsets = NO; to get rid of the margin inside the UITextView, but it shouldn't matter.
The exact x y values that work are (4, -16) for the UILabel.
I think the problem is the height of UILabel:
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
The height is too large. Try to reduce it. And using background color to see more.

Cant center UILabel in titleView of navigationItem iOS8

Im having trouble setting a programmatically allocated UILabel to be centered in my NavigationItem's titleView. Im even setting the NSAlignment to be NSAlignmentCenter. Here's an image of what it looks like:
As you can see.. I have no right UIBarButtonItem in use, but this shouldn't be keeping the text from being centered? Here's the code, in the viewDidLoad:
//Set the title of navBar----------
UILabel *navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; //0 0 320 10
navTitleLabel.backgroundColor = [UIColor clearColor];
navTitleLabel.textAlignment = NSTextAlignmentCenter;
navTitleLabel.textColor=[UIColor whiteColor];
navTitleLabel.text = #"IMG";
[navTitleLabel setFont: [UIFont fontWithName:#"BullettoKilla" size:16]];
self.navigationItem.titleView = navTitleLabel;
What am I doing wrong?
As pointed out by 0yeoj you have set the item width to 320. Try something smaller.
Try this
UILabel *navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];

CGRectMake( ) - y reference looks totally wrong

I am facing a really strange issue:
I am instantiating multiple UIImageView inside a for loop with the method CGRectMake, the y origin I am giving seems to be totally wrong on the screen:
Here is my code:
- (void)makeTheView
{
self.view.backgroundColor = [UIColor whiteColor];
UIScrollView *header = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 100)];
header.backgroundColor = [UIColor colorWithRed:254/255.0f green:255/255.0f blue:213/255.0f alpha:1.0f];
[self.view addSubview:header];
for (int i = 0; i < 10; i++) {
UIImageView *avatar = [[UIImageView alloc] initWithFrame:CGRectMake(5 + i * 75, 5, 70, 70)];
avatar.image = [UIImage imageNamed:#"bo_pic_baby5.jpg"];
[avatar.layer setCornerRadius:8.0];
avatar.layer.masksToBounds = YES;
NSLog(#"%f", avatar.frame.origin.y);
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, avatar.frame.size.width, 20)];
title.backgroundColor = [UIColor colorWithRed:148/255.0f green:148/255.0f blue:148/255.0f alpha:0.5f];
title.font = [UIFont fontWithName:#"Arial" size:15];
title.text = #"崔健";
title.textAlignment = NSTextAlignmentCenter;
title.textColor = [UIColor whiteColor];
[avatar addSubview:title];
[header addSubview:avatar];
}
}
According to this code avatar is within header at 5px from the top of header.
But the following is what I obtain visually:
note: when the white area begin, the header view stopped
This is not a real issue since I can reevaluate my frames like this :
CGRectMake(5 + i * 75, - 20, 70, 70)
but it looks really weird, and I am quite sure I am missing something totally trivial here...
I think this will be fixed by:
self.automaticallyAdjustsScrollViewInsets = NO;
Since iOS 7, view controllers automatically adjust scroll view insets so that the scroll view content is not hidden behind the navigation bar because it expects scroll views to start at the top of the screen.
However, the usual solution is to just set the scrollview frame.origin.y to 0.
Your Code is Absolutely Correct , As you are Adding the scrollview on (0,64) Position , So 64 will be count from Bottom of the Navigation Bar, If you want it on top (Just Below the Navigation bar), Change this declaration to as below :
UIScrollView *header = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];

Cannot scroll when adding UIScrollView to UIView

I'm trying to add a scrollview to my UIView, and put a label, textview, image on it but the scroll bar doesn't show up and I can't scroll.
//faux view
UIView* fauxView = [[[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)]autorelease] ;
[self.bgView addSubview: fauxView];
//the new panel
self.bigPanelView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bgView.frame.size.width, self.bgView.frame.size.height)]autorelease];
self.bigPanelView.center = CGPointMake( self.bgView.frame.size.width/2, self.bgView.frame.size.height/2);
self.bigPanelView.backgroundColor = self.initialBackgroundColor;
UIScrollView * scroll = [[UIScrollView alloc] initWithFrame:self.bigPanelView.bounds];
[scroll setContentSize:CGSizeMake(200, 200)];
[self.bigPanelView addSubview:scroll];
scroll.showsVerticalScrollIndicator = YES;
// add label
UILabel *lblTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 25, self.bgView.frame.size.width, 46)]autorelease];
// If I change the 25 to 235 the label will come under the screen but still I cannot scroll.
lblTitle.textAlignment = NSTextAlignmentCenter; // Align the label text in the center
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.text = self.initialTitle;
lblTitle.textColor = self.initialTitleColor;
lblTitle.font = [UIFont systemFontOfSize:31];
[scroll addSubview: lblTitle];
You have to make the scrollview's contentSize larger than the view:
[scroll setContentSize:CGSizeMake(200, 200)];
needs to be something like:
[scroll setContentSize:CGSizeMake(400, 400)];
Of course, make that the size of the content of whatever you're going to put the scroll view, and that way, it only scrolls if needs to.
For Scrolling - The scrollView contentSize should be larger then the scrollView frame.

Resources