UITextview clipping with three dots? - ios

I have a UITextView showing some dynamic content that can vary in size.
The textview has no scrolling allowed and its size is independent from the content.
Given its auto-layout constraints, the text view has a different horizontal size on iphone5 and iPhone6plus for example.
What I would like is my text to be clipped when necessary with 3 dots at the end like that “…”. (there is a "More" UIButton launching safari below the UITextView)
I’m not sure if there is a UITextView property or if i should consider some code that checks how many characters the textview can display in the current circumstances and modify my string to be shown accordingly (cutting and appending #“…”).
Thank you.

Try setting the UITextView line break mode. It works just like UILabel and should use "..." at the end.
self.textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
See https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSTextContainer_Class_TextKit/index.html#//apple_ref/occ/instp/NSTextContainer/lineBreakMode

If you don't need to edit the content of the text view from the user and you are only displaying content, then use UILabel.
above white background it the text view and the text with 4 lines is the UILabel, i assume you are trying to implement something like this.
Only change the line break mode to "Truncate Tail" and set the no of lines to value you desire.

Related

ios. UILabel resize issue

I have some issue with labels layout using storyboard.
Left view snapped to labels trailing constraint, and centered according to text, and nothing more. So problem appears when I try to set different text to label, (when text contains number of characters greater then in text on story board). After text has been setting label does't resize it's frame.
Here, how it looks in runtime:
I have the same issue with other all labels in project, they are don't resize in runtime, but They should. Here is how it should looks like :
I'v already tried to add layoutIfNeeded() and sizeToFit() to those labels, but it has no effect. I made another project, with pretty same label and constraints, and there it works fine, as should. But not in this project.
What problem could be in?
It could also depend on when you're setting the text of the label.
If you're setting the text in viewDidLoad try doing it in viewDidAppear.

Customize iOS UITextView text

In my iOS application i have an UITextView in which i insert some text downloaded from the web. This text is not editable/selectable by the user and it's a sort of preview of the whole text downloaded.
So, i want to show only the first two line of the text and i want to have always a vertical centered alignment. The text can also have only one line and, if there are more than two lines or there is a very long line i want to put these ".." at the end of the visible text.
I want something like this:
I hope i explained myself.
If it is only a preview of the entire text, you can use a UILabel instead. The UILabel will have a fixed size and it will automatically truncate the text at the end and add the "..." you want. Don't forget to specify the numberOfLines property of UILabel to be 2 and the textAlignment property to NSTextAlignmentCenter if you want it centered.

how to get uitextview text in particular area

i need to get text from uitextview. The text i need is in particular area after scrolling the textview, not all the text in uitextview.
i'll describe what i need in some images.
first, i have this textview
then, i want to get some text, like in this image :
but i dont know how to do that, can somebody help me ?
thank you
NSRange range = NSMakeRange(desired_position, 0);
[myTextview setSelectedRange:range];
Interesting and unusual question.
[Rewrite for UITextView, originally written for UITextField]
A UITextView is basically a text input control wrapped in a UIScrollView ( using inheritance). A UIScrollView works by moving it's contents around using the co-ordinates of it's view around relative to it's frame.
Look at the contentOffset of the UITextView to get the amount of vertical scrolling that has occurred ( the text has scrolled up, so the y value will be negative.)
Once you have the amount of scrolling, you need to get the font for the UITextView. Use NSString sizeWithFont to get the height of a line. Divide the y-coordinate by the line height and that will give you how many newlines to skip.
The end of the visible text is a little different. Depending on your controller structure, the UITextView may have been resized, or not. If you're using a UITableViewController, for example, it will typically resize the tableView automatically. I'm going to assume that you have resized the UITextView, in which case the end 'y' is the start 'y' + the textView.frame.size.height. If you haven't resized the UITextView to fit the space above the keyboard, then you change the design to do so, or subtract the size of the on screen keyboard.
Hope this helps

Reposition images within view based on size of text - iOS

I have been recently getting into iOS development, and I'm trying to build something that looks (very roughly) like this: http://falkendev.com/downloads/ios-sample.png
Basically, it's a page that shows simple text -- large header text that may span multiple lines, a separator line, and then smaller descriptive text that may be a variable length. This text does not need to be editable. I'm working using interface builder, but I imagine that what I want done may need to be done programmatically?
Two questions:
-- How do I go about creating these text fields so that they adjust their height based on the content? I'm assuming I would be using a standard "text" field for each, make them not editable, and then programmatically change their height? And then based on the height of the various text fields, I would need to adjust the positioning of the text fields and the divider line between them?
-- How do I go about making the page scrollable? It's possible that the descriptive text will be long and would extend off the edge of the screen. I would want the whole page to be scrollable, not just the descriptive text section. I'm assuming I would place all my elements within a scroll view... but currently when I do that and view it, the view just gets centered (cutting off both the top and the bottom) and I can't scroll it at all.
Thanks for any help!
set the scrollview content size to greater than its actual size to
make it scrollable like this :
scrollView.contentSize = CGSizeMake(YourWidth ,YourHEight ); // Here you can change either of height and width to make it more scrollable in that direction.
You can use UITextView object to have a scrollable text field...
which can scroll to show additional text..just set its editing
property to NO.
Otherwise to dynamically update label height yourself...use
NSString sizeWithFont method

iOS: UILabel & UIButton in a table cell

I am trying to display a Label and a button side-by-side in a UITableViewCell. The button sits at the right end & the Label sits at the left end. The label can host a long text and should get truncated if the text would flow out of bounds (I do not want to reduce the font size, etc.). Needless to say, I want this to work for both the orientations.
If I use UITableViewCellStyleDefault (without adding a button) & set a long text for the default label, I observe that the label auto-resizes perfectly when the orientation changes. Most probably because its autoresizingMask is set to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin. Now, the way I see it, I could insert my button, and make the label truncate properly, if I could configure the value of the right margin that is being used by default (to accommodate the button). I essentially want it to auto-resize within the bounds that I specify. Is there a way this can be accomplished?
I would not prefer listening to each orientation change and setting the bounds of the label's frame. Any other feasible solution?
I finally went with subclassing UITableViewCell & overriding layoutSubViews. Thanks Mark!

Resources