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.
Related
I have created a static table view in Storyboard with one cell that contains a UITextField for text entry. The text field has constraints to bind it to the cell for top, bottom, trailing, and leading so that it will have the right width on all resolutions.
In viewDidLoad, I enter some placeholder text for an empty state.
NSMutableAttributedString *placeholderString =
[[NSMutableAttributedString alloc]
initWithString:NSLocalizedString(#"Note", #"")];
[placeholderString beginEditing];
[placeholderString addAttribute:NSForegroundColorAttributeName value:
[PulseColor tableViewEmptyCellBackgroundColor] range:NSMakeRange(0,
placeholderString.length)];
[placeholderString endEditing];
self.txtNote.attributedPlaceholder = placeholderString;
self.txtNote.delegate = self;
The problem is that rather that sticking to the constraints, the width of the UITextField shrinks to the size of the placeholder text. As I type and the message reaches the end of the smaller text field, the text expands to the left and disappears before expanding to the right. (If I had the reputation, I'd post images)
Is there a setting that I am missing or some other way that will either cause the UITextField to keep its width across the cell, or cause the text to not disappear to the left as it reaches the initial length?
I wish to make a horizontally scrollable uitextview in which a user can type text that is much longer than the width of the textview.
But currently, when i have implement a textfield, the text stops at the end of the textview, even when i continue to type and setting textContainer.maximumnumberoflines = 1.
I wish it to have the same vertical scrolling function where the height textview expands while users types more text onto the textview. But in my case, i want the width of textview to expand to accommodate words.
At the end of the long text, i wish to be able to scroll the textview horizontally to view the full text.
The current failed attempt by me looks like this.
currently, the word stops at "hahahh", no matter how much i type, nothing changes.
textContainer.maximumnumberoflines = 1 ----> This is not required (By default textField has scrolling property). You can initialize textField like this and it works. Here specify your textField frame, superView to which this textField has to be added, color and font.
UITextField *myTextField = [[UITextField alloc] initWithFrame:frame];
myTextField.font = giveFont;
myTextField.textColor = giveColor;
[superview addSubView:myTextField];
You can use TextView instead of TextField.
TextView is located just above the ScrollView in Object Library.
Check this out
While typing in a UITextView sometimes it scrolls down to current line(case a) but it doesn't the other times(case b).
There's another problem which is:
The same UITextView sometimes show all the text in it (case 1) but other times it doesn't show the last line of text(case 2).
Whenever case 1 happens case a follows.
and Whenever case 2 happens case b follows.
This is the hierarchy of the view:
Size(variable height-fixed width) of these UITextViews as well as UICollectionViewCells are calculated using sizeWithFont:constrainedToSize:lineBreakMode:
Limits of height are set from 43 to 120.
if height>43 then enableScrolling is set to YES, otherwise to NO(Logic X).
Scrolling is enabled when textViewBeginEditing and Logic X is applied when textViewEnded Editing.
There is no scrolling in case 2.
Please suggest cause and workarounds.
On iOS7, I think that you could leave the UITextView's scrollEnabled property set to YES in all cases. If it contains less text it will just not scroll. If you set the property while configuring the cell, you might get this kind of weird behavior, because UIKit is reusing those cells and probably the UITextView too.
For making the last line visible, I'm guessing you need to calculate the text view size more accurately. Try using the attributedText to set the text, with all the formatting you need. Then, in order to calculate the size, you can do it like this:
NSAttributedString *text = self.yourCellsTextView.attributedText;
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:text];
CGSize size = [calculationView sizeThatFits:CGSizeMake(self.yourCellsTextView.frame.size.width, FLT_MAX)];
CGFloat finalMessageHeight = size.height;
Hope this helps :).
At the moment in my IB I have a View Controller which is covered by a UIScroll View. Within the scroll view I have a UIImageView at the top, a UILableView in the middile and a MKMapView at the bottom. The UILableView number of lines is set to 0 (infinite) and word wrap allowing me display as much content as I want.
I want to be able to tap telephone numbers and website url's for the content in my UILableView. The best way I've found so far is to change it to a UITextView which handles all of this for you. However... I can not get the same behaviour with the scrolling.
Before the image, label and map used to scroll as a block. Now, only the textView scrolls. Any advice appreciated.
the displaying part is correct, that is calculate the frame of the textView based on the size of its text for scrolling add this [textView setScrollEnabled:NO];
You can try using this project:
https://github.com/mattt/TTTAttributedLabel
label.dataDetectorTypes = NSTextCheckingTypeLink; // Automatically detect links when the label text is subsequently changed
label.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol)
label.text = #"Fork me on GitHub! (http://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked
NSRange range = [label.text rangeOfString:#"me"];
[label addLinkToURL:[NSURL URLWithString:#"http://github.com/mattt/"] withRange:range]; // Embedding a custom link in a substring
I have a cell with Right Detail style and when the label text is too big the detail label turns into ... but I need the detail text always to be on screen and the label main text should be cut if it is too big. How can I implement this without changing the style of the cell?
I need to make the detail value to be shown fully in the cell - and on the screenshot is the present situation.
You could override the UILabel yourself, to force it to be a fixed size ? The default code apple provide seems to be resizing it automagically.
CGFrame oldLabelFrame = cell.textLabel.frame;
cell.textLabel.frame = CGRectMake(oldLabelFrame.origin.x, oldLabelFrame.origin.y,400, oldLabelFrame.size.height); <--- change this
Of course making a custom cell would be better.
Set the character length needed for the detail text.Check the text length of the detail text. and limits the text length count of the the label text according to your needs.
NSString *labeltext=#"hai it's a beautiful place";
NSString * detailTextLabel=#"This is a test text for checking the detail text label";
if (detailTextLabel.length>14) {
cell.textLabel.text=[NSString stringWithFormat:#"%#...",[labeltext substringToIndex:3]];
cell.detailTextLabel.text=detailTextLabel;
}
else{
///write what to do if detail text label is greater than 14 characters..
}