I am trying to set custom font.
Font is working with UILabel.
When I tries to use for UITextView, its not working. UITextView is taking default font.
Any idea how to use custom font for UITextView.
Code I am using is as below.
attributedString = [[NSMutableAttributedString alloc] initWithString:text
attributes:#{ NSFontAttributeName : [UIFont fontWithName:#"GEDinarOne-Medium"
size:15], NSLigatureAttributeName: #2}];
abtUsText.attributedText = attributedString;
abtUsText.textAlignment = NSTextAlignmentRight;
abtUsText.textColor = cb60756;
I am using attributedString to get working with iOS 6.
iOS 7 is giving proper font, but iOS 6 is using default font for UITextView only.
Edit 1
I am using arabic font as GEDinarOne-Medium
Edit 2
This is not duplicate of what is mentioned above as my case is with arabic font and not english font. Mentioned in duplicate works with english custom fonts only.
After I investigate custom font (especially arabic) is not working UITextView using attributedString, below is what I did.
I just wanted to display the text in UITextView (for About Us in my app) as it has long text and I wanted scrolling option.
Below is what I did.
Add UIScrollView
Add UILabel inside UIScrollView
Assign text to UILabel.
abtUsLabel.attributedText = attributedString;
Make label sizeToFit
[abtUsLabel sizeToFit];
This is very important step, else it won't work.
Set scrollview contentSize as per label height.
[abtUsScrollView setContentSize:CGSizeMake(abtUsScrollView.frame.size.width, abtUsLabel.frame.size.height)];
Done... now if the Label is longer, we can scroll it.
Related
I'm making an application for iOS 7 and iOS 8 and I would like to disable word wrapping in my UITextView. I have some ASCII tables and I don't want them broken.
I didn't find the option in the StoryBoard and I tried to do it programatically. I have tried:
[textView setContentSize:[[textView attributedText] size]];
and
[textView.textContainer setSize:[[textView attributedText] size]];
Nothing seems to be working.
The UITextView was created for this exact functionality that you are trying to remove. It might be that the UITextView is not the best option based on your use case. There are many ways to accomplish this without the use of UITextView. Here are a few:
1) If the text is not going to be editable, you could use a UILabel. Set the Number Of Lines = 0 and appropriate settings for Line Breaks and Autoshrink.
2) If the text needs to be editable, you should use a UITextField inside of a UIScrollView.
3) Make a custom UIView that calculates the space needed and draw the text yourself. (This option is the most robust, but usually is not necessary and can introduce and handful of issues. The afore mentioned controls will have this type of logic already worked out for you by Apple).
Hope this helps.
I'm not sure exactly what your interface looks like, but here are a few options:
You could either try this answer to detect a word wrap and go from there.
Word wrap detecting in UITextView
It might work for you. Or try using a bezier path to draw the line how you want it. Or NSTextContainer like here Adding exclusion paths to multiple text views
CGSize txtSize = [editor sizeThatFits:CGSizeMake(0, 0)];
editor.frame = CGRectMake(0, 0, txtSize.width, self.frame.size.height);
self.contentSize = CGSizeMake(txtSize.width, self.frame.size.height);
Here "self" is a UIScrollView and will scroll the UITextView horizontally. "editor" is a UITextView and will scroll itself vertically.
You can also "disable" word-wrap in UITextView by putting it inside UIScrollView and setting
textView.isScrollEnabled = false
The question specifies the need for horizontal scrolling, but if you want to simply disable word wrapping and cut off the text that doesn't fit, you can do this:
let attributedString = NSMutableAttributedString(string: your_string)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byClipping // .byTruncatingTail also works
attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))
your_text_view.attributedString = attributedString
My iOS app is not showing long attributed strings. I have a cell in a tableview which contains this textView. When the text is very long the tableview is unresponsive for a while but when it loads the text is not shown. All other cells are displayed fine. And the textView works fine with small text strings.
Here's the code:
descriptionCell = [tableView dequeueReusableCellWithIdentifier:#"CellAdDetailDescription"];
descriptionCell.bodyTextView.delegate = self;
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.ad.body];
UIFont *cellFont;
cellFont = [UIFont fontWithName:#"HelveticaNeue" size:16.0];
NSDictionary *attributesDictionary;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;
attributesDictionary = #{NSParagraphStyleAttributeName : paragraphStyle , NSFontAttributeName: cellFont};
[str addAttributes:attributesDictionary range:NSMakeRange(0, str.length)];
descriptionCell.bodyTextView.attributedText = str;
I pasted the long string here. I debugged and str is being loaded fine containing the desired text.
Whats wrong here?
What is the max allowed string length in UITextView?
EDIT: very odd, when trying selection in the textView, the text is being shown in the magnifying glass. I posted a video here.
Is it a bug in UITextView?
Here is the screenshot. The blank white at the bottom is the textView.
It could have something to do with the scrolling. If you are showing all the text (i.e. the text view is expanded to be as high as it needs to be, and so is the table view cell), the scrolling is done by the table view. If the text view is smaller, you have to scroll to see all the text - this might cause a conflict with the table view, which is also a scroll view.
It has been suggested that you disable the scrolling of the text view before adding the attributed text and reenable it afterwards. If you are showing the whole text in the table view, you can leave the text view scrolling disabled. In some cases, it will only work if scrolling is enabled. You should check this possibility as well.
I also had this issue (= very long attributed text didn't show up in the UITextView within an autosized UITableViewCell). I tried all accepted answers from here and from this question: UITextView not loading/showing the large text?. None did work.
However I then found out that - in my case - it just works fine on the device. So if you're still struggling with this kind of problem, don't rely on the iOS Simulator.
The reason that you UITextView not show all text because it Frame is too small so it truncates the text to fit with its frame. you can do follow step to show all text:
In your CustomUITableCell, override layoutSubView:
Use this function to calculate size of TextView that fit it content
[textView sizeThatFits:CGSizeMake(textView.frame.size.width, CGFLOAT_MAX)].height
After that, calculate and set new frame for TExtView (in uitableCell custom)
In the tableView:heightForCellAtIndexPath, calculate new size of textView (also height of cell) similar like above and return right height for cell.
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
I'm currently developing an iPad application and want to apply a custom font to the UIButtons on a certain screen. I have noticed similar problems with other screens, namely that the text on some (seemingly random) UIButtons disappears. In this case, the custom font is being applied to some buttons but not others, again there doesn't seem to be any pattern as to which buttons work and which don't. I've attached a screenshot below to try to give you an idea of what exactly I mean.
As I've mentioned, on some other screens I have noticed text completely disappearing from some buttons and have had to replace these with images featuring the text instead.
All buttons are created in Interface Builder. They use attributed text to allow multiple lines and centred alignment. Any help would be much appreciated.
edit - my code is like the following:
for (UIView *sub in view.subviews) {
UIButton *btn = (UIButton *) sub;
UILabel *lbl = [btn titleLabel];
[lbl setFont: myFont size: mySize];
}
To use attributed insure the IB items are set to use attributed text, not plain text.
To set the attributed title for a NSButton use:
- (NSAttributedString *)attributedTitleForState:(UIControlState)state
for a NSLabel use:
#property(nonatomic,copy) NSAttributedString *attributedText
Of course you may not need attributed text is all you are doing is just setting a font and alignment.
For multiple lines of text set:
#property(nonatomic) NSInteger numberOfLines
as appropriate.
From the comments:
You can achieve centered and multiline title labels in UIButtons without using NSAttributedStrings by adding the following lines to your for loop:
lbl.textAlignment = UITextAlignmentCenter;
lbl.numberOfLines = 0;