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.
Related
I am new in iOS.And I am facing a problem regarding to change the colour of font awesome label.My code is like this
UILabel *lab =[[UILabel alloc] init];
lab.text = [NSString awesomeIcon:FaChild];
UIImage *listImage2 = [UIImage imageNamed:#"Image.png"];
UIButton *listButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
listButton2.backgroundColor=[UIColor whiteColor];
[[listButton2 layer] setBorderWidth:0.5f];
listButton2.layer.borderColor =[[UIColor blackColor] CGColor];
listButton2.layer.cornerRadius = calenderbtn.bounds.size.width / 3.4;// this value vary as per your desire
listButton2.clipsToBounds = YES;
UIFont *font = [UIFont fontWithName:#"FontAwesome" size:15.0];
UIColor *color = [UIColor whiteColor];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,color,NSForegroundColorAttributeName, nil];
NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:lab.text attributes:attrsDictionary];
// get the image size and apply it to the button frame
CGRect listButton2Frame = listButton2.frame;
listButton2Frame.size = listImage2.size;
listButton2.frame = listButton2Frame;
[listButton2 setAttributedTitle:attributedStr forState:UIControlStateNormal];
[listButton2 addTarget:self
action:#selector(LogoutClick:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton2 =
[[UIBarButtonItem alloc] initWithCustomView:listButton2];
And it output look like this
But it in back colour but I write a code for white colour.How to change its colour. Thanks in Advace!
Assign attrsDictionary to the second NSDictionary, not to the first one.
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,color,NSForegroundColorAttributeName, nil];
or
// using literals
NSDictionary *attrsDictionary = #{NSFontAttributeName:font,NSForegroundColorAttributeName:color}; //
You have only set the font property of the label with the first NSDictionary.
I am trying to add a stroke to a text and showing in UITextView.
Here is what I want.
In the picture (CREATED FROM PHOTOSHOP) there are same font used, first one has Postion "OUTSIDE" and bottom text has position INSIDE.
I am using following code in Objective-C
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(0, 0);
NSDictionary * textAttributes =
#{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSShadowAttributeName : shadow,
NSStrokeColorAttributeName : [UIColor blackColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-3.0],
NSFontAttributeName : [UIFont fontWithName:#"UbuntuCondensed-Regular" size:40] };
textTitleResult.attributedText = [[NSAttributedString alloc] initWithString:textTitle.text
attributes:textAttributes];
I am achieving INSIDE effect. How can I set position to OUTSIDE
I don't think there's a property that can do exactly when you want. However, I have an idea which will possibly provide you with a way to "fake" it.
You can add another label at the exact position of the original one. The new label would have half the size of the stroke but the stroke color would be the same color as the text. Since the stroke "sits" on the border (half part inside the text and half part outside), it would block half of the original stroke, making it look as if it's on the outside.
Without making the code too pretty, it would look something like this:
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 40)];
infoLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:infoLabel];
UILabel *secondInfoLabel = [[UILabel alloc] initWithFrame:infoLabel.frame];
secondInfoLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:secondInfoLabel];
NSDictionary * secondTextAttributes =
#{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSStrokeColorAttributeName : [UIColor whiteColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-3.0],
NSFontAttributeName : [UIFont systemFontOfSize:40] };
secondInfoLabel.attributedText = [[NSAttributedString alloc] initWithString:#"Welcome To" attributes:secondTextAttributes];
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(0, 0);
shadow.shadowBlurRadius = 2;
NSDictionary * textAttributes =
#{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSShadowAttributeName : shadow,
NSStrokeColorAttributeName : [UIColor blackColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-6.0],
NSFontAttributeName : [UIFont systemFontOfSize:40] };
infoLabel.attributedText = [[NSAttributedString alloc] initWithString:#"Welcome To" attributes:textAttributes];
Another option would be to create your own custom view and draw it yourself just the way you want it (in drawRect).
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:
I use the drawTextInRect: method in the UITextField class to create outlined text (text with a black border/store effect).
But I need something similar to create the same outlined text in a UITextView, but the drawTextInRect: method is not included here. How can I do this for a textView?
This is the code I use in the UITextField class:
- (void)drawTextInRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 1);
CGContextSetLineJoin(c, kCGLineJoinRound);
CGContextSetTextDrawingMode(c, kCGTextFill);
self.textColor = [UIColor whiteColor];
[super drawTextInRect:rect];
CGContextSetTextDrawingMode(c, kCGTextStroke);
self.textColor = [UIColor blackColor];
[super drawTextInRect:rect];
}
How can I create a similar solution for a textView in a UITextView class?
Are you targeting iOS 6.0+?
Something like this should work, perhaps:
NSAttributedString *yourString = [[NSAttributedString alloc] initWithString:#"Your text" attributes:#{
NSStrokeColorAttributeName:[UIColor redColor],
NSStrokeWidthAttributeName:[NSNumber numberWithFloat:1.0],
NSFontAttributeName:[UIFont systemFontOfSize:24.0f]
}];
yourTextField.attributedText = yourString;
Enjoy
Peter, use negative value for NSStrokeWidthAttributeName. In this case you can set any fill color for text.
NSAttributedString *yourString = [[NSAttributedString alloc] initWithString:#"Your text" attributes:#{
NSStrokeColorAttributeName:[UIColor redColor],
NSStrokeWidthAttributeName:[NSNumber numberWithFloat:-1.0],
NSFontAttributeName:[UIFont systemFontOfSize:24.0f],
NSForegroundColorAttributeName:[UIColor greenColor]
}];
yourTextField.attributedText = yourString;
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);