can any one help to change the font size in UITextView.
[textView setFont:[UIFont fontWithName:#"ArialMT" size:30]];
I use this one but it doesn't work.
Have you bind textView with xib or allocated textView?
If yes,
You can use following code to check whether this works
[textView setFont:[UIFont boldSystemFontOfSize:15]];
or
[textView setFont:[UIFont systemFontOfSize:15]];
Check, if "Selectable" is checked in the xib/Storyboard attribute inspector. If it's not selectable, the size wont be changed
Swift 4.0
textField.font = UIFont.systemFont(ofSize: 17.0)
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[textView setFont: [UIFont fontWithName:#"ArialMT" size:30]];
textView.text = #"test my font size";
[self.view addSubview:textView];
[textView release];
Your code is correct. The problem is from other part of your code. Provide more details about your textView
Set custom font and size in Swift:
textView.font = UIFont(name: "ArialMT", size: 20)
Use this:
UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
myTextView.font = [UIFont fontWithName:#"arial" size:14];
Here is for swift 3.0
textview.font = UIFont(name: textview.font.fontName, size: 18)
I tried it the same way like you with
[textView setFont:[UIFont fontWithName:kFontName size:kFontSize]];
I set the UITextView inside ViewDidLoad. My problem was, I transformed my view:
[_viewEnd setTransform:CGAffineTransformMakeScale(0.0, 0.0)];
My problem was solved with setting the properties of the UITextView after transforming my UIView which includes the UITextView.
Related
I just want to highlight only text in UILabel, I have tried by giving backgroundColor for label, but it is highlighting the empty spaces also looks not good. So Is there any way to highlight text without resizing UILabel.
Please check the image, here labels are bigger than the text (center aligned)
Thanx.
Most of the other solutions don't consider text that spans multiple lines while still only highlighting the text, and they are all pretty hacky involving extra subviews.
An iOS 6 and later solution is to use attributed strings:
NSMutableAttributedString *s =
[[NSMutableAttributedString alloc] initWithString:yourString];
[s addAttribute:NSBackgroundColorAttributeName
value:[UIColor greenColor]
range:NSMakeRange(0, s.length)];
label.attributedText = s;
This will add a subview behind the text, with the correct size:
CGSize size= [[label text] sizeWithFont:[UIFont systemFontOfSize:18.0]];
NSLog(#"%.1f | %.1f", size.width, size.height);
NSLog(#"%.1f | %.1f", label.frame.size.width, label.frame.size.height);
UIView *highlightView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
[highlightView setBackgroundColor:[UIColor greenColor]];
[self.view insertSubview:highlightView belowSubview:label];
[highlightView setCenter:label.center];
And don't forget: [label setBackgroundColor:[UIColor clearColor]];
try this
MTLabel
MTLabel *label4 = [MTLabel labelWithFrame:CGRectMake(20, 270, 250, 60)
andText:#"This label is highlighted, has a custom line height, and adjusts font size to fit inside the label."];
[label4 setLineHeight:22];
[label4 setFont:[UIFont fontWithName:#"Helvetica" size:30]];
[label4 setAdjustSizeToFit:YES];
[label4 setMinimumFontSize:15];
[label4 setFontHighlightColor:[[UIColor orangeColor] colorWithAlphaComponent:0.5]];
[self.view addSubview:label4];
You can have a UIImageView, set its background color of your choice then place your UIlabel on it.
That will surely solve your problem.
Here is Workaround code :
UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
[imgView setBackgroundColor:[UIColor brownColor]];
[self.view addSubview:imgView];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentCenter];
label.text = #"My Label";
[imgView addSubview:label];
[label release];
[imgView release];
You can also achieve the same using Interface Builder.
I am using a subclassed UITextField to use a custom font. When I set a placeholder for that textfield it gets shifted up a little, but the when I start entering the text, the frame of text has no problem. Is there a way to fix the placeholder shifting issue.
self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.txtFirstName.autocapitalizationType = UITextAutocapitalizationTypeSentences;
self.txtFirstName.placeholder = #"Enter First Name here";
self.txtFirstName.textAlignment = UITextAlignmentLeft;
I had same issue and I tried setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter which solved my issue.
UITextField *txtPassword = [[UITextField alloc] initWithFrame:CGRectMake(15,55, 255, 25)];
[txtPassword setPlaceholder:#"Password..."];
[txtPassword setBackgroundColor:[UIColor whiteColor]];
[txtPassword setFont:[UIFont fontWithName:#"Helvetica" size:15]];
[txtPassword setSecureTextEntry:YES];
[txtPassword setTextAlignment:NSTextAlignmentCenter];
[txtPassword setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[txtPassword becomeFirstResponder];
Hope this helps.
In my iphone app. I want to change the text size in a label when clicking a button
Create UILabel
self.lbl=[[UILabel alloc] initWithFrame:CGRectMake(135, 290,200,35)];
self.lbl.backgroundColor=[UIColor clearColor];
NSString *str=[NSString stringWithFormat:#"%.f",[self.slider value]];
[self.lbl setFont:[UIFont fontWithName:#"Arial-BoldMT" size:16]];
self.lbl.text=str;
[self.View addSubview:self.lbl];
Change size of text of UILabel when tapped UIButton
-(IBAction)ButtonPressed:(id)sender
{
[self.lbl setFont:[UIFont fontWithName:#"Arial-BoldMT" size:25]];
}
Here is how to change the font size at runtime....
yourLabel.font=[UIFont systemFontOfSize:14];
-(IBAction)changeFontOfLabel:(id)sender
{
[self.lebelObject setFont:[UIFont fontWithName:#"American Typewriter" size:18];
}
and also write the proper font name . check here for a list of fonts
I'm trying to replicate an iphone style navbar in my mac app. But I just can't figure out what font (name and size) that the title in the navbar should be. Does anybody know what it is or have a way to figure it out? Thanks in advance.
Update: Here's a picture. I'm referring to the "Navigation bar" pointed out on top.
http://www.funkyspacemonkey.com/wp-content/uploads/2010/01/iPhone_UI_elements_sizes.jpg
Oh, I just figured it out! Helvetica Neue, as Ben pointed out, and then I used size 20. But then, to complete the look, you need to give it an embossed look. I did this by using this code:
[[myTextField cell] setBackgroundStyle:NSBackgroundStyleLowered];
I found that here: http://whomwah.com/2009/03/11/replicating-apples-embossed-text-in-a-cocoa-app/
Now it looks right.
Prior to iPhone 4, the font is Helvetica. The iPhone 4 uses Helvetica Neue.
The point-size you want will depend on what you mean by 'navbar'.
Replicate title label manually:
UINavigationItem(Category)
- (void)setTitle:(NSString *)title
{
UIFont* font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:20.0f];
CGSize textSize = [title sizeWithFont:font];
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width, textSize.height)];
label.textColor =[UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.text = title;
label.font = font;
label.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5f];
label.shadowOffset = CGSizeMake(0,-1);
label.textAlignment = UITextAlignmentCenter;
[self setTitleView:label];
[label release];
}
I have a simple navigation based application for the iphone/objective-c
within various UIViewControllers that are pushed into view, I can set the text in the title bar using something like
self.title = #"blah blah blah"
Is there a way to control the font and font-size of the title in the title bar text?
thanks!
the proper way to resize the title text of a navcontroller is to set the titleView property of the navigationItem
like this (in viewDidLoad)
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
tlabel.text=self.navigationItem.title;
tlabel.textColor=[UIColor whiteColor];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=tlabel;
You may want to emboss the label so it doesn't look fuzzy and flat:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:18.0];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = self.navigationItem.title;
// emboss so that the label looks OK
[label setShadowColor:[UIColor darkGrayColor]];
[label setShadowOffset:CGSizeMake(0, -0.5)];
self.navigationItem.titleView = label;
}
IF you want this to work both on iphone and ipad, and also want to get the title centered then use the following code.
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
label.text=self.navigationItem.title;
label.textColor=[UIColor whiteColor];
label.backgroundColor =[UIColor clearColor];
label.adjustsFontSizeToFitWidth=YES;
label.font = [AppHelper titleFont];
label.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView=label;
}
You can assign any UIView to a navcontroller's title area.
Create a UILabel and set its font and size anyway you want, then assign it to the UIViewController's navigationItem.titleView property. Make sure the UILabel's backgroundColor is set to clearColor.
This only works on the top-level nav-view. As the user drills down into the view controller hierarchy and the "back" button is shown the alternate titleView is ignored and the regular text label is shown.