Is there a way to edit a UITextField's font size with a UIStepper? i.e, tap the add button and the UITextField's font size increases by 1 or 5? So far I have tried [textField adjustsFontSizeToFitWidth:value]; with no luck. How could I do this? Also, if I can't control the font size with a UIStepper, how else can I edit the font size? Thanks
I think you should be using:
[textField setFont:[UIFont systemFontOfSize:value]];
UIFont *textFont = [UIFont fontWithName:#"Helvetica-Bold" size:8.0];
[textField setFont:textFont];
Try This...
Related
I have a large text file that I am trying to display on screen. For example:
Line 1
Line 2
Line 3
...
Line 138
I have a UIScrollview, and dynamically add a subview of either a UILabel or UITextView with the texts. I am able to calculate the width/height of the text with this:
[passedTextInFile boundingRectWithSize:CGSizeMake(20000.0f, 20000.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName: [UIFont fontWithName:#"Times New Roman" size:textSize]} context:Nil].size;
Then I create my UILabel and add to subview like this:
CGRect test = CGRectMake(5.0, 5.0, maxWidthSize, maxHeightSize);
CGSize maxSize = CGSizeMake(maxWidthSize, maxHeightSize);
UILabel *lblResults = [[UILabel alloc] initWithFrame:test];
[lblResults setFrame:test];
[lblResults setText:passedTextInFile];
[lblResults setFont:[UIFont fontWithName:#"Times New Roman" size:textSize]];
[lblResults setTextColor:[UIColor blackColor]];
[lblResults setNumberOfLines:0];
[svResults addSubview:lblResults];
[svResults setShowsHorizontalScrollIndicator:true];
[svResults setScrollEnabled:true];
[svResults setContentSize:maxSize];
The above code works great. I am able to see Line 1, and scroll to the bottom and see Line 138.
However, when I change the UILabel to UITextView, I am only able to scroll to Line 131.
UITextView *lblResults = [[UITextView alloc] initWithFrame:test];
[lblResults setFrame:test];
[lblResults setText:passedTextInFile];
[lblResults setFont:[UIFont fontWithName:#"Times New Roman" size:textSize]];
[lblResults setTextColor:[UIColor blackColor]];
[lblResults setScrollEnabled:false];
[lblResults setEditable:false];
It seems to cut off some of the text at the end. I have double checked that the Height Size is both 2139.539062 for UILabel and UITextView.
Now, when I enable scroll for the UITextView, I am able to scroll to Line 131, let the scrolling stop, then can scroll to Line 138. In other words, I am scrolling to Line 131 in the UIScrollView, and then to Line 138 in the UITextView. I do no want this behavior, which is why I disabled scrolling for the UITextView.
My question is why does the scrolling stop early in a UITextView compared to the UILabel. Is there a setting in the UITextView I can change to make it display just like a UILabel?
Thanks in advance
UPDATE:
Here are the lines I used to make this work:
[[lblResults textContainer] setLineFragmentPadding:0.0];
[lblResults setTextContainerInset:UIEdgeInsetsZero];
[lblResults sizeToFit];
Thanks again for everyone's quick response and help.
A UITextView has padding, but a UILabel does not.
You can remove the padding like this on iOS 7+:
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;
See this question for solutions pre-iOS 7.
UIlabel is a view UITextView is a view too, the method boundingRectWithSize is not the same thing as a view frame is just the bounding rect to contain that text. As suggested by Aaron UITextView contains padding while UILabel not. To know the exact frame call -sizeToFit on the text container after setting its string and later ask for its frame.
You could try setting lblResults.textContainer.maximumNumberOfLines to be a large number. (iOS 7)
I am changing my UISearchBar's font with:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.leftBarButtonItem = self.leftButton;
self.navigationItem.titleView = _searchBar;
//Change font size
UITextField *txfSearchField = [_searchBar valueForKey:#"_searchField"];
[txfSearchField setDefaultTextAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"Montserrat-Regular" size:30],
}];
}
but it has a side effect of preventing the text from scrolling when the text is longer that the textfield's size.
In the following screenshot I have written "This is a test":
However if I use "Helvetica" instead of "Montserrat-Regular" the result if fine:
Edit: Turns out this is a font size issue. The problem happens with any font (even Helvetica) if you set it above a "certain" size. This size is different for every font. I think when the font's height is larger than the textfield's height so that it gets "cropped" this is when the problem happens.
As a temporary workaround I have used a smaller font size, using Montserrat-Regular. As an alternative solution I want to try and increase the textfield's height and see if that works.
I have a label in center of my screen and would like for the user to be able to change the font of this label. Can I access the font feature of the label programmatically, so that I can define and specify a function that would enable the user to do so?
UIFont *myFont = [UIFont fontWithName:#"HelveticaNeue" size:15];
[mylabel setFont:myFont];
I have UITextFields with custom font and size. Everything worked fine till I changed to Xcode 5 to fix all the changes with new iOS/Xcode. Now when I check my UITextFields they have the right font on placeholder and while editing, but when I stop editing the font size gets bigger. So why now with Xcode 5 it doesn't work?
Screenshots: link
Code to set font hasn't changed:
[_eventName setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:16]];
Have you made changes to the appearance proxy for UILabels? I'm having a similar issue where it appears as if UITextField's text is rendered as a UILabel after editing is completed. Overriding my appearance proxy settings on UILabel fixed this issue for me.
I too had an issue when inserting rows in a table - the text field in the table cell would use the UILabel proxy font, not the font set on its font property. Scrolling off the screen and back on would then show the correct font.
The only way I was able to fix this was to override UITextField, add a custom drawRect - in here I had to set self.font to nil, then reset self.font to the value I desired.
Chris
Now you need to use sizeWithAttributes: instead, which now takes an NSDictionary. Pass in the pair with key UITextAttributeFont and your font object like this :
[_eventName.text sizeWithAttributes:#{NSFontAttributeName:[UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:16]}]
I had this problem, and just like described by chrisoneiota the problem is that the UITextField uses a UILabel for some situations. I solved it also by overriding the UITextField, but instead of implementing drawInRect: I do:
- (void)addSubview:(UIView *)view {
if ([view isKindOfClass:[UILabel class]]) {
UILabel *label = (UILabel *)view;
label.font = self.font;
}
[super addSubview:view];
}
This appears to work well.
I am trying to implement a function that can change uilabel font size, I can zoom uilabel size(is uilabel.size) , then automatic change the uilabel's font size, the example app is InstaText!
label.font = [UIFont fontWithName:#"Your desired font" size:newSize];
or you could do it even easier:
By looking here UILabel - set property adjustsFontSizeToFitWidth to YES