IOS drawing text inside a subview - ios

im trying to draw a text on a subview, the view appearing fine but the text not, this is what im doing, the view have black color, the text have white color, text and rect value given in other method:
NSString* sumText = [[NSString alloc]initWithFormat:#"%0.1f",sum];
CGRect textRect = CGRectMake(chartLocation.x+chartLength+10,
chartLocation.y,
20,
20);
[self drawTextRect:sumText inRect:textRect];
-(void)drawTextRect:(NSString *)sumText inRect:(CGRect)textRect {
UIFont* font=[UIFont fontWithName:#"Arial" size:(20/2)];
UIColor* textColor = [UIColor whiteColor];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle]mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary* stringAttr=#{NSFontAttributeName:font,NSForegroundColorAttributeName:textColor,NSParagraphStyleAttributeName:paragraphStyle};
UIView* textView =[[UIView alloc] initWithFrame:textRect];
textView.backgroundColor = [UIColor blackColor];
[sumText drawInRect:textRect withAttributes:stringAttr];
[self.view addSubview:textView];}
am i do anything wrong? thanks

The drawInRect:withAttributes: method draws the string in the current graphics context. You have not defined a context, so your drawing goes nowhere.
If you want to draw the string yourself, you should subclass UIView, add the required properties (sumText), and overwrite drawRect:. A graphics context is automatically created for you in drawRect: so you can just go ahead and draw.
But really, you probably just want to use a UILabel instead.

there is another way you can achieve what you want .
UILabel* lblText = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
lblText.text = #"Hello World!";
lblText.backgroundColor = [UIColor blackColor];
lblText.textColor = [UIColor whiteColor];
[self.view addSubview:lblText];
And Output is:

Related

How can we fix UItextfields text and UIbuttons title labels exactly at center position

Hi i am beginner in ios and i am trying to insert UItextfield "text" data exactly at center of the y-axis as like below textfield image
And i would like to insert UIbutton "titleLabel text" exactly center of x-axis and center of y-axis as like below button image for this i tried lot but i did not get result please help me some one
my code:-
[self.TripIdTxt setValue:[UIColor blackColor] forKeyPath:#"_placeholderLabel.textColor"];
self.TripIdTxt.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);
self.TripIdTxt.backgroundColor = [UIColor whiteColor];
self.TripIdTxt.layer.cornerRadius = 3.0f;
self.TripIdTxt.layer.masksToBounds = YES;
[self.TripIdTxt setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[self.TripIdTxt setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
self.TripIdTxt.textColor = [UIColor blackColor];
self.TripIdTxt.font = [UIFont fontWithName:bitter_Regular size:14];
set
for UIButton
yourbtnName.titleLabel.textAlignment = NSTextAlignmentCenter;
for y-axis
[yourbtnName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
for X-axis
[yourbtnName setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
for UITextFields
yourtextfieldName.textAlignment = NSTextAlignmentCenter;
diectly you can access in storyboard like
for additional information see this link
Padding with TextField
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
TripIdTxt.leftView = paddingView;
self. TripIdTxt.leftViewMode = UITextFieldViewModeAlways;
or else use Transform
TripIdTxt.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);
Set the text alignmaent center of your lable property .
mainlabel.textAlignment = NSTextAlignmentCenter;
u set its alignment using xib by
u can also set its vertical alignment using xib by
Programmatically U can set Content's vertical alignment for textfield as well as button as
button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
txtField.contentVerticalAlignment = UIControlContentHorizontalAlignmentRight;
Hope it helps.
Happy Coding.. :)

UITextField sizeToFit leaves right margin

I have a UITextField that I'm trying to center in a UIView. However, when I call sizeToFit it leaves a small right margin in the textField which causes the text to be slightly off center when the textField background is clear. I've colored it here so you can see what I'm talking about:
messageField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
messageField.delegate = self;
messageField.font = [UIFont systemFontOfSize:15];
messageField.textColor = [UIColor darkGrayColor];
messageField.textAlignment = NSTextAlignmentLeft;
messageField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"not fitting right" attributes:#{NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
[messageField layoutSubviews];
[messageField sizeToFit];
messageField.backgroundColor = [UIColor lightGrayColor];
And the textfield looks like this (I would post a picture but need more reputation points, this is basically the same though):
not fitting right
Any help would be greatly appreciated!
I should not that if I create a UILabel of the same font size and text, call sizeToFit on the label and then assign the UITextField frame to be the same as the UILabel, it works like a charm.

How to get text for in self drawn view from viewcontroller

I've got self drawn view with text:
NSString* text = #"text";
UIFont* font = [UIFont systemFontOfSize:14.f];
NSShadow* shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(1, 1);
shadow.shadowColor = [UIColor whiteColor];
shadow.shadowBlurRadius = 0.5;
NSDictionary* attirbutes =
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor grayColor], NSForegroundColorAttributeName,
font, NSFontAttributeName,
shadow, NSShadowAttributeName, nil];
CGSize textSize = [text sizeWithAttributes:attirbutes];
CGRect textRect = CGRectMake(0,
0,
textSize.width, textSize.height);
textRect = CGRectIntegral(textRect);
[text drawInRect:textRect withAttributes:attirbutes];
In my ViewController I want to get it from another class and draw it in my selfDrawnView. How Can I perform it?
Not really clear what exactly are you asking. If you subclassed UIView with your new view class and overridden drawRect method there you need to add a property to hold the text you're drawing.
Then in your view controller you just pass the text to that property and call [myCustomView setNeedDisplay] to force redrawing.

Animate UILabel to Change Colour?

I would like to make the text in my UILabel called coloursLabel change text colour by itself. I have tried the following method as another SO answer suggested (albeit for the backgroundColor) but it still doesn't change. Am I missing something silly?
UILabel *coloursLabel = [[UILabel alloc] init];
coloursLabel.text = #"This sentence is colourful!";
[coloursLabel sizeToFit];
coloursLabel.center = self.view.center;
coloursLabel.frame = CGRectOffset(coloursLabel.frame, timeForPage(6), 0);
[UIView animateWithDuration:2.0 animations:^{
coloursLabel.textColor = [UIColor greenColor];
coloursLabel.textColor = [UIColor redColor];
coloursLabel.textColor = [UIColor blueColor];
coloursLabel.textColor = [UIColor yellowColor];
coloursLabel.textColor = [UIColor blackColor];
} completion:NULL];
coloursLabel shows the correct text but the text is in black.
Any help would be much appreciated!
UILabel's textColor is not animatable. You could write your own Label class, using a CATextLayer as mask and animate its layer's backgroundColor.

Effect in Text - objective-c

I have the sources of these letters and I am trying to create the gray effect around in objective-c.
But I can not, does anyone have an idea how to do this?
Take a look at GSBorderLabel.
It's good because it adds outer border and not inner border on characters and it's very easy to customise it.
For example:
GSBorderLabel *myLabel = [[GSBorderLabel alloc] initWithFrame:CGRectMake(0, 50, 300, 100)];
myLabel.textColor = [UIColor yellowColor];
myLabel.textAlignment = NSTextAlignmentCenter;
myLabel.borderColor = [UIColor grayColor];
myLabel.borderWidth = 20;
myLabel.text = #"Review";
myLabel.font = [UIFont fontWithName:#"Party LET" size:60.0];
If you're targeting iOS 6 and up, there isn't much over head for this. You can simply use NSAttributedString to add a stroke to an existing font. Mind you, this will not add the stroke to the font, just render it on screen.
NSDictionary *attributes = #{NSStrokeColorAttributeName: [UIColor grayColor], NSStrokeWidthAttributeName : #3};
NSString *inputText = #"Some text";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:inputText attributes:attributes];
[self.someLabel setAttributedText:attributedString];
You can try this:
#import <QuartzCore/QuartzCore.h>
....
titleLabel.layer.shadowColor = [[UIColor grayColor] CGColor];
titleLabel.layer.shadowRadius = 3;
titleLabel.layer.shadowOpacity = 1;
titleLabel.layer.shadowOffset = CGSizeMake(0, 0);

Resources