IOS Uitextview content not fully displayed

Source: Internet
Author: User

When we use Uitextview as a uilabel for some needs (in order to use Uitextview's own copy, paste, select function, automatic phone identification, email, link, etc.), we only need to disable the Uitextview of a few properties on the line

Textview.editable = no;//Not editable
textview.scrollenabled = no;//Not scrollable

Then calculate the size of the text and set the Uitextview frame, which I am here is the xib inside the TextView,

nsmutableattributedstring *attributedstring = [[nsmutableattributedstring alloc]initwithstring: Notification.content];
Nsmutableparagraphstyle *paragraphstyle = [[Nsmutableparagraphstyle alloc]init];
[Paragraphstyle Setlinespacing:5];

Nsrange range = Nsmakerange (0,notification.content.length);

[Attributedstring addattribute:nsparagraphstyleattributename Value:paragraphstyle Range:range];
[Attributedstring addattribute:nsforegroundcolorattributename Value:[uicolor colorwithhex:0x333333] range:range];
[Attributedstring addattribute:nsfontattributename Value:[uifont systemfontofsize:15.0] range:range];

Self.textView.attributedText = nil;
Self.textView.attributedText = attributedstring;

Cgsize size = [Self.textView.attributedText boundingrectwithsize:cgsizemake (screenwidth-2*16, maxfloat) options: Nsstringdrawinguseslinefragmentorigin context:nil].size;

Self.textViewH.constant = Size.Height;

There is no problem with using this method on the Uilabel, but the text display is incomplete, or some of the calculated height is larger than the actual height of the text, so you need to set some properties:

 [Self.textview Setcontentinset:uiedgeinsetsmake (-10,-5,-15,-5)];//sets Uitextview's inner margin [self.textview
settextalignment:nstextalignmentleft];//and set left-aligned self.textView.layoutManager.allowsNonContiguousLayout = NO;  self.textView.scrollEnabled = YES; If Scrollenabled=no, the computed is not correct, although this is set to Yes, but TextView does not actually scroll and display the content correctly 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.