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
Related
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
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 a non-scrollable UITextView embedded in a UIScrollView and add text to the UITextView dynamically. The UIScrollView adjust it's contentSize accordingly based on the TextView's frame. However, once the UITextView exceeds a height of 8192, the text will become invisible (but still there, because you can use the magnifying glass to highlight text and even see parts of the text through the magnifying glass).
CGRect textviewFrame = self.TextView.frame;
textviewFrame.size.height = [self textViewHeightForAttributedText:self.TextView.attributedText andWidth:320.0];
self.TextView.frame = textviewFrame;
self.ScrollView.contentSize = self.TextView.frame.size;
Helper function to size UITextView accordingly:
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
UITextView *textView = [[UITextView alloc] init];
[textView setAttributedText:text];
CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;
}
Didn't realize it was the same exact problem that was unsolved here until I tested it out explicitly by forcing the max size to 8193 and the problem occurred (while a max size of 8192 still had the text showing correctly). Anyone run into this problem before and know of a work around? Thanks
I was recently hit by this problem and have worked out an effective way around it. Although it seems like an iOS bug IMHO it's really not... there are practical limits to CALayer sizes, plus drawing an 8K high piece of text takes a long time. Much better to do as Apple intended and to only render the bit of text that's visible... that's why UITextView extends UIScrollView after all.
The problem is that UITextView isn't terribly easy to integrate with other bits of UI. In my case I am working on a news app where a single UITextView is used to render the article, plus there's some separate UI (titles and buttons etc) above and below it, all hosted in a single scrollable container.
The solution I've found is to split the UITextView into two views... a dedicated UITextView container whose frame is the full text size (i.e. the same size your UITextView's contentSize) and which is the superview of your UITextView. Your child UITextView's frame should be set to the bounds of the outer scrollable container.
Then all you have to do is use key-value observation to monitor the contentOffset property of your outer scrollable container view (in my case this is a UICollectionView). When its contentOffset changes you update (1) the contentOffset of your UITextView, and (2) the transform property of the UITextView's layer. By updating that transform property the UITextView is fixed to fill the currently-visible part of it's superview. But because you're also updating the UITextView's contentOffset, this trickery is totally invisible to the user... it looks and behaves as if the UITextView is simply very large.
Here's a fully functional solution, for anyone who'd like it!
** Assuming your content size will not exceed the limits of two text views **
This solution works by adding two UITextViews to your view, and splitting your text between them. It looks complicated, but it's actually very simple! I've just written a very verbose description :)
Step 1 - Add two UITextViews to your view:
I added mine in my storyboard. Place them so that one is directly above the other, with no space between them. Don't worry about setting the height (we will set that later).
Set constraints on the views so that they are tied to each other from the top and bottom, and the surrounding edges of the container from all other sides, including your desired padding. i.e. tie the first text view to the container from the top, left, and right, and to the second text view from the bottom. Tie the second text view to the container from the bottom, left, and right, and to the first text view from the top. This will ensure that they stretch appropriately when the content is set.
Don't set any constraints on the height of the views, or if you must (to avoid warnings from the constraints inspector), set the height of one of the views to be >= 20, or some similarly small number.
Disable scrolling, bouncing, and scrolling indicators for both your text views. This solution relies on the views being a fixed, non-scrollable height, so if you'd like your content to scroll, you should use a UIScrollView or UITableViewCell as a container.
Create outlets for your two new text views in your view controller file, naming them something like textView1 and textView2.
Step 2 - Set textContainerInsets to zero:
Set the textContainerInset property on both text views to zero, either using User Defined Runtime Attributes:
or code:
self.textView1.textContainerInset = UIEdgeInsetsZero;
self.textView2.textContainerInset = UIEdgeInsetsZero;
This will ensure that the no visible space will appear between the two views when the content is set, and should not affect the other spacing around your views.
Step 3 - Split your content, set it, and update the view heights:
Simply copy the following code into your view controller file (viewDidLoad), and set the contentString variable to your content.
/* Content is split across two UITextViews to avoid max drawing height */
NSString *contentString = #"Some very long piece of text...";
// Set text
NSArray *components = [contentString componentsSeparatedByString:#"\n"];
NSInteger halfLength = [components count] / 2;
NSArray *firstHalf = [components subarrayWithRange:NSMakeRange(0, halfLength)];
NSArray *secondHalf = [components subarrayWithRange:NSMakeRange(halfLength, [components count] - halfLength)];
NSString *contentString1 = [firstHalf componentsJoinedByString:#"\n"];
NSString *contentString2 = [secondHalf componentsJoinedByString:#"\n"];
self.textView1.text = contentString1;
self.textView2.text = contentString2;
// Set text view heights
CGFloat fixedWidth1 = self.textView1.frame.size.width;
CGFloat fixedWidth2 = self.textView2.frame.size.width;
CGSize newSize1 = [self.textView1 sizeThatFits:CGSizeMake(fixedWidth1, CGFLOAT_MAX)];
CGSize newSize2 = [self.textView2 sizeThatFits:CGSizeMake(fixedWidth2, CGFLOAT_MAX)];
CGRect newFrame1 = self.textView1.frame;
CGRect newFrame2 = self.textView2.frame;
newFrame1.size = CGSizeMake(fmaxf(newSize1.width, fixedWidth1), MIN(newSize1.height, 8192));
newFrame2.size = CGSizeMake(fmaxf(newSize2.width, fixedWidth2), MIN(newSize2.height, 8192));
self.textView1.frame = newFrame1;
self.textView2.frame = newFrame2;
This code splits the contentString roughly in the middle, looking for the nearest newline. If you'd like to split your content on a different character, simply change all occurrences of \n above to whatever you'd like to split on.
Step 4 - Set your container view height:
Set your container view (scrollView, tableViewCell, whatever else) to the height of your two text views, plus whatever additional space you've set above and below them.
CGRect viewFrame = self.myView.frame;
viewFrame.size.height = MIN(self.textView1.frame.size.height, 8192) + MIN(self.textView2.frame.size.height, 8192) + kTextViewContainerPadding;
[self.myView setFrame:viewFrame];
(In my code, kTextViewContainerPadding is a macro I've set to the sum of the space above and below my two text views, within their container).
That's it! Good luck.
Try enabling the scroll for the scrollView.
Keep the height of the textView > height of the content, so that in reality there will be no scroll, but scrollEnabled should be = YES
It solved the problem for me.
Hello I think am not late to answer. I got the same problem like you. This is my solution:
textView.scrollEnabled = YES;
contentTextView.attributedText = finalAttrString;
// contentTextView.text = [attrString string];
contentTextView.font = kFont(contentTextFS + [valueOfStepper intValue]);
[contentTextView sizeToFit];
contentTextView.height += 1;//This is the key code
//contentTextView.height = 8192.0f
Them I solved the trouble and I can change size dynamic.Successfull on iOS 8
i'm currently making an app where the suer selects an MKMapView annotation and then the title of the annotation(pin) is set to a detailtextLabel in a Right Detail UITableViewCell.
My Problem is that when the text is large, the detailTextLabel becomes multiple lines. When this happens the TextLabel of the cell(the one the left) shifts up. Heres What I've tried:
In the cellForRowAtIndexPath Method, I tried adjusting the frame through the following code:
CGRect frame = cell.textLabel.frame;
frame.origin.y = cell.frame.size.height/2;
cell.textLabel.frame = frame;
Where cell is a UITableViewCell that is set to right detail
Subclass the cell and tun try to adjust the frame in the -(void)layoutSubviews
How do I stop it from going up and keep it at the center of the cell?
If you want to do a custom layout of UITableViewCell, then you need to add your custom UI elements to its -[UITableViewCell contentView] instead of trying to modify geometry of standard UI elements that are created by default.
So, in your case, you need to add two UILabels and set their position so that:
Title label will not move at all
Detail text label will be also multiline
In this way you'll be able to solve this problem!
Please try to make the font size adjustable amount to the text.
I think you can use,
cell.detailTextLabel.adjustFontSizeToWidth = Yes;
And also set the numberOfLines to 0.
Hope that solves the purpose.
I have an NSMutableAttributedString that lives inside a UIScrollView. I highlight a portion of the string using the addAttributes:range: function.
When a lot of text is present, I currently have to manually scroll quite a ways to get to the highlighted part. I'd like to come up with a way to have the view automatically scroll to the highlighted portion when the view is loaded - sort of how you can use anchors to link to a specific part of a webpage.
I'm guessing there is a function that, given some kind of numbers will allow me to scroll to a specific part of my page? How might I come up with such numbers to provide to such a function? Using the NSRange component from the attributed string?
Perhaps there is better way to accomplish this?
UIScrollView has the method setContentOffset:animated: which would allow you to scroll to a particular point in your content. To figure out what the appropriate offset is, you would have to determine the width of the portion of your attributed string prior to the highlighted part. The documentation for the methods you use to do this can be found here. You could do this using the size method of NSAttributedString.
It would look something like this:
#interface SomeViewController : UIViewController
#end
#implementation SomeViewController {
UIScrollView* _scrollView;
}
- (void)scrollToOffset:(NSInteger)offset inAttributedString:(NSAttributedString*)attributedString {
NSAttributedString* attributedSubstring = [attributedString attributedSubstringFromRange:NSMakeRange(0, offset)];
CGFloat width = attributedSubstring.size.width;
[_scrollView setContentOffset:CGPointMake(width, 0.0f) animated:YES];
}
#end
The above code assumes that we're talking about a single line of text and that scrolls horizontally. Alternatively to do this for a fixed width that wraps to an arbitrary height you would need to calculate height with the following code (rather than attributedSubstring.size.height) and then possibly subtract a little to account for wanting to show the last line that of the substring:
CGFloat height = [attributedSubstring boundingRectWithSize:CGSizeMake(<#fixed width to wrap at#>, HUGE_VALF) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
[_scrollView setContentOffset:CGPointMake(0.0f, height) animated:YES];
This is similar to code I use when determining the height of table or collection view cells that have dynamic text that I need to accommodate.
I did not specify it in my original question, but I will address it here. The accepted answer above provides a solution when one wants to scroll horizontally to a specified portion of text. However, in order to scroll vertically a solution similar to that of below is required - other methods may exist but this is what worked for me.
Notice that instead of getting the size component from the attributed sub-string itself, we are getting the size from the view AFTER we've added the sub-string to it. We then insert the full string and force the scroll:
NSAttributedString* att_string = [[NSMutableAttributedString alloc] initWithString:mystring];
NSAttributedString* sub_string = [att_string attributedSubstringFromRange:NSMakeRange(0, highlight_begin_index)];
[the_view setAttributedText:attributedSubstring];
CGFloat jump_height = the_view.contentSize.height;
[the_view setAttributedText:att_string];
[the_view setContentOffset:CGPointMake(the_view.contentOffset.x,jump_height) animated:FALSE];