I have a view where I programmatically set a UILabel in viewDidLoad. The text on that label is blurry. The code:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self setTitle:tAddSystemScreenTitle];
CGRect rect = CGRectMake(10,0,300,80);
UILabel *messageLabel = [[UILabel alloc] initWithFrame:rect];
[messageLabel setOpaque:NO];
if (shouldShowControlForRemoteAccess) {
[messageLabel setText:kRemoteAccessInfoMessage];
[messageLabel setNumberOfLines:2];
} else {
[messageLabel setText:kOneSystemAlreadyAssociatedAlertString];
[messageLabel setNumberOfLines:4];
rect.size.height += 30;
[messageLabel setFrame:rect];
rect = dataTable.frame;
rect.origin.y += 30;
rect.size.height -= 30;
[dataTable setFrame:rect];
}
NSLog(#"x: %f, y: %f, w: %f, h: %f", messageLabel.frame.origin.x, messageLabel.frame.origin.y, messageLabel.frame.size.width, messageLabel.frame.size.height);
[messageLabel setBackgroundColor:[UIColor clearColor]];
[messageLabel setShadowColor:[UIColor blackColor]];
[messageLabel setTextColor:[UIColor blackColor]];
[messageLabel setTextAlignment:UITextAlignmentLeft];
[messageLabel setShadowOffset:CGSizeMake(0,-1)];
[self.view addSubview:messageLabel];
NSLog(#"x: %f, y: %f, w: %f, h: %f", self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[dataTable setScrollEnabled:NO];
[messageLabel release], messageLabel = nil;
}
When I checked what the frame values were, I got the following:
x: 10.000000, y: 0.000000, w: 300.000000, h: 80.000000
Checking the same for self.view gets:
x: 0.000000, y: 20.000000, w: 320.000000, h: 460.000000
The only other questions I could find on this issue suggested that I needed integer numbers for the frame, but as you can see I already have integer numbers. I tried the solution they recommended, using CGRectIntegral(rect) to replace all the places I had just rect, but that didn't work. How do I prevent the text from blurring?
I had the same problem. In my case the reason was that I was calling setShouldRasterize on the parent view.
Perhaps it is the fact that you have black text with an ever so slightly offset black shadow, which makes it look blurry. Try removing your shadowColor and shadowOffset lines and see how it looks.
Related
I have added some images in scroll view I got prom is images not displaying on scroll view so please help, my code is below.
-(void)addscroll
{
CGFloat btnX = 160.0;
int numberOfButton = 10;
for (UIView *subview in scrollView.subviews) {
[subview removeFromSuperview];
}
for (int i = 1 ; i <= numberOfButton; i++)
{
UIImageView *img=[[UIImageView alloc]init];
img.image=[UIImage imageNamed:#"1.png"];
//img.tag = i;
//[button setTitle:#"Excavations" forState:UIControlStateNormal];
img.frame = CGRectMake(160.0, 200.0, 160.0, 40.0);
[scrollView addSubview:img];
btnX = btnX + 165.0;
}
scrollView.contentSize = CGSizeMake(btnX + 50, 150);
}
Try this,
-(void)addscroll
{
CGFloat btnX = 160.0;
int numberOfButton = 10;
for (UIView *subview in scrollView.subviews) {
[subview removeFromSuperview];
}
for (int i = 1 ; i <= numberOfButton; i++)
{
UIImageView *img=[[UIImageView alloc]init];
img.image=[UIImage imageNamed:#"1.png"];
img.frame = CGRectMake(btnX, 105, 160.0, 40.0);
[scrollView addSubview:img];
btnX = btnX + 165.0;
}
scrollView.contentSize = CGSizeMake(btnX + 50, 150);
}
You set x of img frame to 160.0, so all img will be added to the same position, change it to btnX and update btnX on each iteration. Next is contentSize of scrollView, its content height is set to 150 and img is added to y of 200. So content beyond the height of scrollview cannot be visible.
You are calculating wrong frame of image view
img.frame = CGRectMake(btnX, 200.0, 160.0, 40.0);
Your scrollview's height is 150, and your buttons y value is 200, that means is out of visible frame.
-(void)addscroll
{
CGFloat btnX = 160.0;
int numberOfButton = 10;
for (UIView *subview in scrollView.subviews) {
[subview removeFromSuperview];
}
for (int i = 0 ; i < numberOfButton; i++)
{
UIImageView *img=[[UIImageView alloc]init];
img.image=[UIImage imageNamed:#"1.png"];
//img.tag = i;
//[button setTitle:#"Excavations" forState:UIControlStateNormal];
img.frame = CGRectMake(i*btnX +10, 200.0, 160.0, 40.0);
[scrollView addSubview:img];
}
scrollView.contentSize = CGSizeMake((btnX + 10)*numberOfButton, 160);
}
Set img.frame = CGRectMake(160.0, 0, 160.0, 40.0);
By setting y = 200, you are making it out of visible parent's frame. Scroll's max height is 198, your image's y origin should be before that in order to make subview visible.
Use this code. Also make sure that img.image is not nil and "1.png" exists. You have limited the scroll to less than 200 while image start from 200 in y
-(void)addscroll
{
CGFloat btnX = 0;
int numberOfButton = 10;
for (UIView *subview in scrollView.subviews) {
[subview removeFromSuperview];
}
for (int i = 1 ; i <= numberOfButton; i++)
{
UIImageView *img=[[UIImageView alloc]init];
img.image=[UIImage imageNamed:#"1.png"];
img.frame = CGRectMake(btnX, 200.0, 160.0, 40.0);
[scrollView addSubview:img];
btnX = btnX + 165.0;
}
scrollView.contentSize = CGSizeMake(btnX + 50, 250);}
I have a simple textView who's data gets populated dynamically. I want to resize the height of the textview once the data is populated so that I don't see a vertical scroll nor the text gets clipped.I want to do this task programatically. I have a label which should be placed 20 px below height of textview like "interested".I am trying to making the code.But i got some problems like Alignment issues. If i can run the program the output will display like this.
this is my Program.please help me anybody.
lblHobbies = [[UILabel alloc]initWithFrame:CGRectMake(10, 310, 300, 20)];
lblHobbies.text=#"Hobbies";
lblHobbies.font=[UIFont systemFontOfSize:16.0];
lblHobbies.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1];
[scrollView addSubview:lblHobbies];
lblInterests = [[UILabel alloc]initWithFrame:CGRectMake(10, 420, 300, strSize.height+30)];
lblInterests.text=#"Interests";
lblInterests.font=[UIFont systemFontOfSize:16.0];
lblInterests.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1];
[scrollView addSubview:lblInterests];
NSDictionary *attributes = #{NSFontAttributeName: [UIFont systemFontOfSize:14]};
CGRect rect =
[tViewhobbies.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
tViewhobbies=[[UITextView alloc]init];
tViewhobbies.frame=CGRectMake(10, 330,300, rect.size.height);
[tViewhobbies setUserInteractionEnabled:NO];
tViewhobbies.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
tViewhobbies.delegate=self;
[scrollView addSubview:tViewhobbies];
NSDictionary *attributes1 = #{NSFontAttributeName: [UIFont systemFontOfSize:14]};
CGRect rect1 =
[tViewInterests.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes1
context:nil];
tViewInterests=[[UITextView alloc]init];
tViewInterests.frame=CGRectMake(10, 450, 300, rect1.size.height);
[tViewInterests setUserInteractionEnabled:NO];
tViewInterests.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
tViewInterests.delegate=self;
[scrollView addSubview:tViewInterests];
go through below code hope helps u
self.scrollView.contentSize = CGSizeMake(320, 700);
UILabel *lblHobbies = [[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.bounds.origin.x + 2, self.scrollView.bounds.origin.y + 2, 75, 40)];
lblHobbies.text=#"Hobbies";
lblHobbies.font=[UIFont systemFontOfSize:16.0];
lblHobbies.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1];
[self.scrollView addSubview:lblHobbies];
//add this after the hobbies textview so u need to calculate the height, so wy dont you put some helper method that returns the height for the text
//i took some long text that u want to display
NSString *str = #"BARCELONA, Spain -- Nokia is targeting emerging markets with three low-cost smartphones that use Google's Android operating system rather than the Windows Phone software from Microsoft, which is about to buy Nokia's phone business";
CGSize hobbiesTextViewSize = [self calculateHeightForString:str];
//now set the hobbiesTextView frame and also the text
UITextView *tViewhobbies = [[UITextView alloc]initWithFrame:CGRectMake(self.scrollView.bounds.origin.x + 2, self.scrollView.bounds.origin.y + 40, hobbiesTextViewSize.width, hobbiesTextViewSize.height+ 5)];
tViewhobbies.font = [UIFont systemFontOfSize:17.0f];
tViewhobbies.text = str;//set the text hear
[self.scrollView addSubview:tViewhobbies];
//now add the lblInterests label
UILabel *lblInterests = [[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.bounds.origin.x + 2, self.scrollView.bounds.origin.y + lblHobbies.frame.size.height + hobbiesTextViewSize.height + 2, 75, 40)];
lblInterests.text=#"Interests";
lblInterests.font=[UIFont systemFontOfSize:16.0];
lblInterests.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1];
[self.scrollView addSubview:lblInterests];
NSString *str1 = #"Nokia will ditch many of the Google services that come with Android, which Google lets phone makers customize at will. Instead, the new Nokia X line announced Monday will emphasize Microsoft services such as Bing search, Skype communications and OneDrive file storage. Its home screen sports larger, resizable tiles resembling those on Windows phone.";
//now calculate the height for Interests text
CGSize interestTextViewSize = [self calculateHeightForString:str1];
UITextView *tViewInterests = [[UITextView alloc]initWithFrame:CGRectMake(self.scrollView.bounds.origin.x + 2, self.scrollView.bounds.origin.y + lblHobbies.frame.size.height + lblInterests.frame.size.height + tViewhobbies.frame.size.height + 4, interestTextViewSize.width + 2, interestTextViewSize.height+ 2)];
tViewInterests.font = [UIFont systemFontOfSize:17.0f];
tViewInterests.text = str1;
[self.scrollView addSubview:tViewInterests];
}
//our helper method
- (CGSize)calculateHeightForString:(NSString *)str
{
CGSize size = CGSizeZero;
UIFont *labelFont = [UIFont systemFontOfSize:17.0f];
NSDictionary *systemFontAttrDict = [NSDictionary dictionaryWithObject:labelFont forKey:NSFontAttributeName];
NSMutableAttributedString *message = [[NSMutableAttributedString alloc] initWithString:str attributes:systemFontAttrDict];
CGRect rect = [message boundingRectWithSize:(CGSize){320, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];//you need to specify the some width, height will be calculated
size = CGSizeMake(rect.size.width, rect.size.height + 5); //padding
return size;
}
Here is how I did it:
+(CGFloat)getLabelDymanicHeightOfStringWithText:(NSString *)text andFont:(UIFont *)font andFrame:(CGRect )frame {
CGSize maxSize = CGSizeMake(frame.size.width, 999999.0);
int height = 0;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
font, NSFontAttributeName,
nil];
if (IS_IOS_6)//iOS 6 macro
{
CGSize size = [text sizeWithFont:font
constrainedToSize:maxSize
lineBreakMode:NSLineBreakByWordWrapping];
height = size.height;
}
else
{
CGRect frame = [text boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
height = frame.size.height;
}
return height+5;
}
Pass this method your text text font and frame.
You can use custom UITextView to fix the problem:
Kindly check it out: https://github.com/HansPinckaers/GrowingTextView
Textview will set height dynamically based on input string from the user.
Hope, This will help you,
:)
enter code hereYou can use label.
myLabel.text = #"Your String";
CGSize labelSize = [myLabel.text sizeWithFont:myLabel.font
constrainedToSize:CGSizeMake(320,MAX_HEIGHT_YOU_WANT_TO_SET)
lineBreakMode:NSLineBreakByWordWrapping];
CGRect frame = myLabel.frame;
frame.size.height = labelSize.height;
myLabel.frame = frame;
you can simply change the height of the textView to the height of the textView.contentSize.height. Doing this you textView will update and show all the text it contains.
textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width,textView.contentSize.height);
Hope this helps you.
Happy Coding :)
I'm working on an iPad app and part of the UI is a scrollview that has buttons for its contents. I'm working on adding images to these buttons, but when I do so, I only ever get the image in one spot, when I should be seeing it on all the buttons. This is what I'm doing:
float scrollCurrTop = 0;
CGRect currProcedureButtonFrame = CGRectMake(0,
scrollCurrTop,
self.fProceduresView.frame.size.width,
self.fLabelAndButtonHeight);
PatientIDButton* currProcedureButton = [PatientIDButton buttonWithType:UIButtonTypeCustom];
[currProcedureButton setFrame:currProcedureButtonFrame];
[currProcedureButton.layer setBorderColor: [self.fPanelViewBorderColor CGColor]];
[currProcedureButton.layer setBorderWidth: self.fBorderWidth];
currProcedureButton.titleLabel.font = self.fLabelFont;
NSString* displayName = [grabbing name];
if (displayName == nil)
{
displayName = currPlanName;
}
[currProcedureButton setTitle:displayName
forState:UIControlStateNormal];
[currProcedureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
currProcedureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
currProcedureButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
// is the plan approved?
if ([self isPlanApproved:currPlanName])
{
// add the checkmark to this plan button
CGRect currPlanButtonFrame = currProcedureButton.frame;
float originX = currPlanButtonFrame.size.width - (currPlanButtonFrame.size.width/3.0f);
float originY = currPlanButtonFrame.origin.y;
float width = currPlanButtonFrame.size.width - originX;
float height = currPlanButtonFrame.size.height;
CGRect currPlanApprovalImageFrame = CGRectMake(originX, originY, width, height);
UIImageView* currPlanApprovalImage = [[UIImageView alloc] initWithFrame:currPlanApprovalImageFrame];
[currPlanApprovalImage setBackgroundColor:[UIColor colorWithRed:(63.0f/255.0f)
green:(179.0f/255.0f)
blue:(79.0f/255.0f)
alpha:1.0f]];
[currPlanApprovalImage setImage:self.fCheckMarkIcon];
[currProcedureButton addSubview:currPlanApprovalImage];
}
[self.fProceduresView addSubview:currProcedureButton];
scrollCurrTop += self.fLabelAndButtonHeight;
Where 'fProceduresView' is the scrollview that houses the buttons. What am I doing wrong?
It seems that you're misunderstanding the logic behing setting the frame of the imageview's those you're trying to add
CGRect currPlanButtonFrame = currProcedureButton.frame;
float originX = currPlanButtonFrame.size.width - (currPlanButtonFrame.size.width/3.0f);
float originY = currPlanButtonFrame.origin.y;
float width = currPlanButtonFrame.size.width - originX;
float height = currPlanButtonFrame.size.height;
CGRect currPlanApprovalImageFrame = CGRectMake(originX, originY, width, height);
UIImageView* currPlanApprovalImage = [[UIImageView alloc] initWithFrame:currPlanApprovalImageFrame];
You don't need to set originY to currPlanButtonFrame.origin.y;
All subviews have relative coordinates to their's superviews.
So in your case originY should be 0
For example:
// Image is visible
Button frame [0, 0, 100, 100]
image in button [0, 0, 100, 100]
// Button is visible, but image is not because it will be clipped by bounds of the button.
Button frame [100, 100, 100, 100]
image in button [100, 100, 100, 100]
Also you will be able to "see" your images, if you set
currProcedureButton.clipsToBounds = NO
I'm trying to load a UITextField with rounded corners into a table view for iOS. For the most part, everything is working fine. However, the left-most character gets partially cut off due to the corner radius property. Is there a way to set a margin on the text that's inputted into a UITextField, so that it displays properly? Here's my code:
textInput = [[UITextField alloc] init];
textInput.backgroundColor = [UIColor whiteColor];
textInput.placeholder = #"example#gmail.com";
textInput.textColor = [UIColor blackColor];
textInput.keyboardType = UIKeyboardTypeEmailAddress;
textInput.returnKeyType = UIReturnKeyDone;
[[textInput layer] setBorderColor:[[UIColor whiteColor] CGColor]];
[[textInput layer] setBorderWidth:2.3];
[[textInput layer] setCornerRadius:15];
[textInput setClipsToBounds: YES];
[textInput setDelegate:self];
[self.contentView addSubview:textInput];
[textInput release];
I figured out a solution. I created a custom textfield, subclassed from UITextField:
#import "CR_customTextField.h"
#implementation CR_customTextField
- (CGRect)textRectForBounds:(CGRect)bounds {
int margin = 10;
CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
return inset;
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
int margin = 10;
CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
return inset;
}
#end
Use this
[textInput sizeToFit];
it May Helps you
textInput.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);
//helps to start writing text from given pixel
If I do :
view.transform = CGAffineTransformScale(view.transform, s, s);
My view is scaled, but also its subviews... I don't want the subview to change size. Apparently autoresizesSubviews has no effect on this matter
Suppose that your UIView's stack is arranged as follow:
#interface UIResizableView : UIView <UIGestureRecognizerDelegate> {
UILabel *innerView;
CGPoint startPoint;
}
#end
#implementation UIResizableView
- (id)initWithFrame:(CGRect)aFrame {
self = [super initWithFrame:aFrame];
if (self) {
innerView = [[UILabel alloc] initWithFrame: CGRectMake(10, 10, 115, 50)];
[innerView setFont:[UIFont systemFontWithSize:7]];
[innerView setTextColor:[UIColor blackColor]];
[innerView setBackgroundColor:[UIColor clearColor]];
[innerView setTextAlignment:UITextAlignmentCenter];
[innerView setText:#"hello world!"];
[self innerView];
[innerView release];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[self addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
}
return self;
}
#end
Then your scale method would look like the following. Note that the "reframing" of innerView is required to center again the subview in the original position.
-(void)scale:(id)sender {
static float lastScale = 0.0;
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = self.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[self setTransform:newTransform];
CGAffineTransform originalSize = CGAffineTransformInvert(self.transform);
CGAffineTransform originalPositionAndSize = CGAffineTransformTranslate(originalSize, self.frame.origin.x, self.frame.origin.y);
[innerView setTransform:originalPositionAndSize];
innerView = CGRectMake(10, 10, 115, 60);
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
Perhaps you are adding the subview (and using the parent view's frame values to set the size) after you have applied the transform on the parent view. At least that was what I was doing.
I had to compensate for that by applying a inversion
CGRect parentOriginalFrame = CGRectApplyAffineTransform(parentView.frame, CGAffineTransformInvert(parentView.transform));
Now with the parent's original frame, you can set the right frame value for the child.